From ad4ba4a4c29b75fe188781ac5901da2c09595dfa Mon Sep 17 00:00:00 2001 From: Niall Thomson Date: Sat, 13 Jul 2024 19:54:33 -0600 Subject: [PATCH] chore: Move docker images to component directories (#583) --- src/cart/Dockerfile | 67 ++++++++++++++++++++++ src/cart/docker-compose.yml | 12 ++-- src/cart/scripts/build.source | 2 - {images/go => src/catalog}/Dockerfile | 4 +- src/catalog/docker-compose.yml | 3 - src/catalog/scripts/build.source | 1 - {images/nodejs => src/checkout}/Dockerfile | 0 src/checkout/docker-compose.yml | 11 ++-- src/checkout/scripts/build.source | 1 - src/orders/Dockerfile | 67 ++++++++++++++++++++++ src/orders/docker-compose.yml | 3 - src/orders/scripts/build.source | 2 - {images/java17 => src/ui}/Dockerfile | 4 +- src/ui/docker-compose.yml | 25 +------- src/ui/scripts/build.source | 2 - 15 files changed, 149 insertions(+), 55 deletions(-) create mode 100644 src/cart/Dockerfile delete mode 100755 src/cart/scripts/build.source rename {images/go => src/catalog}/Dockerfile (96%) mode change 100755 => 100644 src/catalog/scripts/build.source rename {images/nodejs => src/checkout}/Dockerfile (100%) delete mode 100644 src/checkout/scripts/build.source create mode 100644 src/orders/Dockerfile delete mode 100755 src/orders/scripts/build.source rename {images/java17 => src/ui}/Dockerfile (97%) delete mode 100755 src/ui/scripts/build.source diff --git a/src/cart/Dockerfile b/src/cart/Dockerfile new file mode 100644 index 00000000..51987866 --- /dev/null +++ b/src/cart/Dockerfile @@ -0,0 +1,67 @@ +# Build Stage +FROM public.ecr.aws/amazonlinux/amazonlinux:2023 as build-env + +# We tell DNF not to install Recommends and Suggests packages, keeping our +# installed set of packages as minimal as possible. +RUN dnf --setopt=install_weak_deps=False install -q -y \ + maven \ + java-17-amazon-corretto-headless \ + which \ + && \ + dnf clean all + +ARG JAR_PATH + +VOLUME /tmp +WORKDIR / + +COPY .mvn .mvn +COPY mvnw . +COPY pom.xml . + +RUN ./mvnw dependency:go-offline -B -q + +COPY ./src ./src + +RUN ./mvnw -DskipTests package -q && \ + mv /target/carts-0.0.1-SNAPSHOT.jar /app.jar + +# Package Stage +FROM public.ecr.aws/amazonlinux/amazonlinux:2023 + +# We tell DNF not to install Recommends and Suggests packages, which are +# weak dependencies in DNF terminology, thus keeping our installed set of +# packages as minimal as possible. +RUN dnf --setopt=install_weak_deps=False install -q -y \ + java-17-amazon-corretto-headless \ + shadow-utils \ + && \ + dnf clean all + +ARG aws_opentelemetry_agent_version=1.24.0 + +ENV APPUSER=appuser +ENV APPUID=1000 +ENV APPGID=1000 + +RUN useradd \ + --home "/app" \ + --create-home \ + --user-group \ + --uid "$APPUID" \ + "$APPUSER" + +RUN curl -L https://github.com/aws-observability/aws-otel-java-instrumentation/releases/download/v${aws_opentelemetry_agent_version}/aws-opentelemetry-agent.jar -o /opt/aws-opentelemetry-agent.jar && \ + curl -L https://raw.githubusercontent.com/aws-observability/aws-otel-java-instrumentation/v${aws_opentelemetry_agent_version}/licenses/licenses.md -o aws-opentelemetry-agent-licenses.md +ENV JAVA_TOOL_OPTIONS=-javaagent:/opt/aws-opentelemetry-agent.jar +ENV OTEL_JAVAAGENT_ENABLED=false + +WORKDIR /app +USER appuser + +COPY ./ATTRIBUTION.md ./LICENSES.md +COPY --chown=appuser:appuser --from=build-env /app.jar . + +EXPOSE 8080 + +ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar /app/app.jar"] diff --git a/src/cart/docker-compose.yml b/src/cart/docker-compose.yml index a2ddd127..bc35eebe 100644 --- a/src/cart/docker-compose.yml +++ b/src/cart/docker-compose.yml @@ -1,12 +1,9 @@ -version: '2.3' +version: "2.3" services: carts: build: context: . - dockerfile: ../../images/java17/Dockerfile - args: - JAR_PATH: target/carts-0.0.1-SNAPSHOT.jar image: microservices-demo/carts hostname: carts restart: always @@ -29,7 +26,8 @@ services: ports: - "8082:8080" healthcheck: - test: ["CMD-SHELL", "curl -f http://localhost:8080/actuator/health || exit 1"] + test: + ["CMD-SHELL", "curl -f http://localhost:8080/actuator/health || exit 1"] interval: 10s timeout: 10s retries: 3 @@ -61,7 +59,7 @@ services: waiter: image: busybox:1.33.0 - restart: 'no' + restart: "no" cap_drop: - all read_only: true @@ -70,4 +68,4 @@ services: command: echo 'Ready' depends_on: carts: - condition: service_healthy \ No newline at end of file + condition: service_healthy diff --git a/src/cart/scripts/build.source b/src/cart/scripts/build.source deleted file mode 100755 index f61023eb..00000000 --- a/src/cart/scripts/build.source +++ /dev/null @@ -1,2 +0,0 @@ -dockerfile='../../images/java17/Dockerfile' -docker_build_args='--build-arg JAR_PATH=target/carts-0.0.1-SNAPSHOT.jar' \ No newline at end of file diff --git a/images/go/Dockerfile b/src/catalog/Dockerfile similarity index 96% rename from images/go/Dockerfile rename to src/catalog/Dockerfile index 2d22da3e..1d41b75f 100644 --- a/images/go/Dockerfile +++ b/src/catalog/Dockerfile @@ -10,8 +10,6 @@ RUN dnf --setopt=install_weak_deps=False install -q -y \ && \ dnf clean all -ARG MAIN_PATH - RUN mkdir -p "${GOPATH}/src" "${GOPATH}/bin" /appsrc WORKDIR /appsrc @@ -19,7 +17,7 @@ WORKDIR /appsrc COPY . . ENV GOPROXY=direct -RUN go build -o main $MAIN_PATH +RUN go build -o main main.go # Final stage FROM public.ecr.aws/amazonlinux/amazonlinux:2023 diff --git a/src/catalog/docker-compose.yml b/src/catalog/docker-compose.yml index c920305e..ab774eda 100644 --- a/src/catalog/docker-compose.yml +++ b/src/catalog/docker-compose.yml @@ -4,9 +4,6 @@ services: catalog: build: context: . - dockerfile: ../../images/go/Dockerfile - args: - MAIN_PATH: main.go image: microservices-demo/catalog hostname: catalog depends_on: diff --git a/src/catalog/scripts/build.source b/src/catalog/scripts/build.source old mode 100755 new mode 100644 index fa859f37..d9159a55 --- a/src/catalog/scripts/build.source +++ b/src/catalog/scripts/build.source @@ -1,2 +1 @@ -dockerfile='../../images/go/Dockerfile' pack_args='--env BP_KEEP_FILES=db/*' \ No newline at end of file diff --git a/images/nodejs/Dockerfile b/src/checkout/Dockerfile similarity index 100% rename from images/nodejs/Dockerfile rename to src/checkout/Dockerfile diff --git a/src/checkout/docker-compose.yml b/src/checkout/docker-compose.yml index 64a7b82d..89dbc246 100644 --- a/src/checkout/docker-compose.yml +++ b/src/checkout/docker-compose.yml @@ -4,7 +4,6 @@ services: checkout: build: context: . - dockerfile: ../../images/nodejs/Dockerfile image: microservices-demo/checkout hostname: checkout restart: always @@ -18,9 +17,9 @@ services: environment: - REDIS_URL=redis://checkout-redis:6379 ports: - - "8085:8080" + - '8085:8080' healthcheck: - test: ["CMD-SHELL", "curl -f http://localhost:8080/health || exit 1"] + test: ['CMD-SHELL', 'curl -f http://localhost:8080/health || exit 1'] interval: 10s timeout: 10s retries: 3 @@ -33,9 +32,9 @@ services: hostname: checkout-redis restart: always ports: - - "6379:6379" + - '6379:6379' healthcheck: - test: ["CMD", "redis-cli", "ping"] + test: ['CMD', 'redis-cli', 'ping'] interval: 1s timeout: 3s retries: 30 @@ -48,4 +47,4 @@ services: command: echo 'Ready' depends_on: checkout: - condition: service_healthy \ No newline at end of file + condition: service_healthy diff --git a/src/checkout/scripts/build.source b/src/checkout/scripts/build.source deleted file mode 100644 index fd6ddb96..00000000 --- a/src/checkout/scripts/build.source +++ /dev/null @@ -1 +0,0 @@ -dockerfile='../../images/nodejs/Dockerfile' \ No newline at end of file diff --git a/src/orders/Dockerfile b/src/orders/Dockerfile new file mode 100644 index 00000000..0935e50f --- /dev/null +++ b/src/orders/Dockerfile @@ -0,0 +1,67 @@ +# Build Stage +FROM public.ecr.aws/amazonlinux/amazonlinux:2023 as build-env + +# We tell DNF not to install Recommends and Suggests packages, keeping our +# installed set of packages as minimal as possible. +RUN dnf --setopt=install_weak_deps=False install -q -y \ + maven \ + java-17-amazon-corretto-headless \ + which \ + && \ + dnf clean all + +ARG JAR_PATH + +VOLUME /tmp +WORKDIR / + +COPY .mvn .mvn +COPY mvnw . +COPY pom.xml . + +RUN ./mvnw dependency:go-offline -B -q + +COPY ./src ./src + +RUN ./mvnw -DskipTests package -q && \ + mv /target/orders-0.0.1-SNAPSHOT.jar /app.jar + +# Package Stage +FROM public.ecr.aws/amazonlinux/amazonlinux:2023 + +# We tell DNF not to install Recommends and Suggests packages, which are +# weak dependencies in DNF terminology, thus keeping our installed set of +# packages as minimal as possible. +RUN dnf --setopt=install_weak_deps=False install -q -y \ + java-17-amazon-corretto-headless \ + shadow-utils \ + && \ + dnf clean all + +ARG aws_opentelemetry_agent_version=1.24.0 + +ENV APPUSER=appuser +ENV APPUID=1000 +ENV APPGID=1000 + +RUN useradd \ + --home "/app" \ + --create-home \ + --user-group \ + --uid "$APPUID" \ + "$APPUSER" + +RUN curl -L https://github.com/aws-observability/aws-otel-java-instrumentation/releases/download/v${aws_opentelemetry_agent_version}/aws-opentelemetry-agent.jar -o /opt/aws-opentelemetry-agent.jar && \ + curl -L https://raw.githubusercontent.com/aws-observability/aws-otel-java-instrumentation/v${aws_opentelemetry_agent_version}/licenses/licenses.md -o aws-opentelemetry-agent-licenses.md +ENV JAVA_TOOL_OPTIONS=-javaagent:/opt/aws-opentelemetry-agent.jar +ENV OTEL_JAVAAGENT_ENABLED=false + +WORKDIR /app +USER appuser + +COPY ./ATTRIBUTION.md ./LICENSES.md +COPY --chown=appuser:appuser --from=build-env /app.jar . + +EXPOSE 8080 + +ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar /app/app.jar"] diff --git a/src/orders/docker-compose.yml b/src/orders/docker-compose.yml index a2cdf83f..a43e7aaa 100644 --- a/src/orders/docker-compose.yml +++ b/src/orders/docker-compose.yml @@ -4,9 +4,6 @@ services: orders: build: context: . - dockerfile: ../../images/java17/Dockerfile - args: - JAR_PATH: target/orders-0.0.1-SNAPSHOT.jar image: microservices-demo/orders hostname: orders restart: always diff --git a/src/orders/scripts/build.source b/src/orders/scripts/build.source deleted file mode 100755 index a2bbb26d..00000000 --- a/src/orders/scripts/build.source +++ /dev/null @@ -1,2 +0,0 @@ -dockerfile='../../images/java17/Dockerfile' -docker_build_args='--build-arg JAR_PATH=target/orders-0.0.1-SNAPSHOT.jar' \ No newline at end of file diff --git a/images/java17/Dockerfile b/src/ui/Dockerfile similarity index 97% rename from images/java17/Dockerfile rename to src/ui/Dockerfile index ecccc97c..2874965e 100644 --- a/images/java17/Dockerfile +++ b/src/ui/Dockerfile @@ -10,8 +10,6 @@ RUN dnf --setopt=install_weak_deps=False install -q -y \ && \ dnf clean all -ARG JAR_PATH - VOLUME /tmp WORKDIR / @@ -24,7 +22,7 @@ RUN ./mvnw dependency:go-offline -B -q COPY ./src ./src RUN ./mvnw -DskipTests package -q && \ - mv /$JAR_PATH /app.jar + mv /target/ui-0.0.1-SNAPSHOT.jar /app.jar # Package Stage FROM public.ecr.aws/amazonlinux/amazonlinux:2023 diff --git a/src/ui/docker-compose.yml b/src/ui/docker-compose.yml index 2cc0b9d2..3c37b788 100644 --- a/src/ui/docker-compose.yml +++ b/src/ui/docker-compose.yml @@ -1,36 +1,17 @@ -# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -# SPDX-License-Identifier: MIT-0 -# -# Permission is hereby granted, free of charge, to any person obtaining a copy of this -# software and associated documentation files (the "Software"), to deal in the Software -# without restriction, including without limitation the rights to use, copy, modify, -# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to -# permit persons to whom the Software is furnished to do so. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, -# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -version: '2' +version: "2" services: ui: ports: - - 8080 + - 8080 environment: - JAVA_OPTS=-XX:MaxRAMPercentage=75.0 -Djava.security.egd=file:/dev/urandom - SERVER_TOMCAT_ACCESSLOG_ENABLED=true hostname: ui build: context: . - dockerfile: ../../images/java17/Dockerfile - args: - JAR_PATH: target/ui-0.0.1-SNAPSHOT.jar image: microservices-demo/ui restart: always mem_limit: 256m cap_drop: - - ALL \ No newline at end of file + - ALL diff --git a/src/ui/scripts/build.source b/src/ui/scripts/build.source deleted file mode 100755 index f0215c5e..00000000 --- a/src/ui/scripts/build.source +++ /dev/null @@ -1,2 +0,0 @@ -dockerfile='../../images/java17/Dockerfile' -docker_build_args='--build-arg JAR_PATH=target/ui-0.0.1-SNAPSHOT.jar' \ No newline at end of file