From e79626721839b0223d9fbdc73d10ed410fc9475f Mon Sep 17 00:00:00 2001 From: sid palas Date: Tue, 21 Feb 2023 20:35:58 -0500 Subject: [PATCH] update dev dockerfiles --- .../api-golang/Dockerfile.8 | 22 ++++++------- .../api-node/Dockerfile.8 | 20 ++++++++--- .../api-node/Dockerfile.9 | 16 ++++----- .../docker-compose-debug.yml | 1 + .../docker-compose-dev.yml | 33 ++++++++++++++----- .../docker-compose-test.yml | 3 +- 6 files changed, 59 insertions(+), 36 deletions(-) diff --git a/06-building-container-images/api-golang/Dockerfile.8 b/06-building-container-images/api-golang/Dockerfile.8 index ed976f0..d2b3d2c 100644 --- a/06-building-container-images/api-golang/Dockerfile.8 +++ b/06-building-container-images/api-golang/Dockerfile.8 @@ -1,10 +1,7 @@ # Pin specific version for stability # Use separate stage for building image # Use debian for easier build utilities -FROM golang:1.19-bullseye AS base-builder - -# Add non root user -RUN useradd -u 1001 nonroot +FROM golang:1.19-bullseye AS build-base WORKDIR /app @@ -16,8 +13,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ go mod download -# Dev stage with additional dev dependencies installed -FROM base-builder AS dev +FROM build-base AS dev # Install air for hot reload & delve for debugging RUN go install github.com/cosmtrek/air@latest && \ @@ -27,8 +23,10 @@ COPY . . CMD ["air", "-c", ".air.toml"] -# Production builder stage to produce the static binaries -FROM base-builder AS production-builder +FROM build-base AS build-production + +# Add non root user +RUN useradd -u 1001 nonroot COPY . . @@ -47,7 +45,7 @@ RUN go build \ -o api-golang # Use separate stage for deployable image -FROM scratch AS production +FROM scratch # Set gin mode ENV GIN_MODE=release @@ -55,13 +53,13 @@ ENV GIN_MODE=release WORKDIR / # Copy the passwd file -COPY --from=production-builder /etc/passwd /etc/passwd +COPY --from=build-production /etc/passwd /etc/passwd # Copy the healthcheck binary from the build stage -COPY --from=production-builder /app/healthcheck/healthcheck healthcheck +COPY --from=build-production /app/healthcheck/healthcheck healthcheck # Copy the app binary from the build stage -COPY --from=production-builder /app/api-golang api-golang +COPY --from=build-production /app/api-golang api-golang # Use nonroot user USER nonroot diff --git a/06-building-container-images/api-node/Dockerfile.8 b/06-building-container-images/api-node/Dockerfile.8 index 3076c14..3930047 100644 --- a/06-building-container-images/api-node/Dockerfile.8 +++ b/06-building-container-images/api-node/Dockerfile.8 @@ -1,9 +1,6 @@ # Pin specific version for stability # Use alpine for reduced image size -FROM node:19.6-alpine - -# Set NODE_ENV -ENV NODE_ENV production +FROM node:19.6-alpine AS base # Specify working directory other than / WORKDIR /usr/src/app @@ -12,6 +9,21 @@ WORKDIR /usr/src/app # dependencies (better layer caching) COPY package*.json ./ +FROM base as dev + +RUN --mount=type=cache,target=/usr/src/app/.npm \ + npm set cache /usr/src/app/.npm && \ + npm install + +COPY . . + +CMD ["npm", "run", "dev"] + +FROM base as production + +# Set NODE_ENV +ENV NODE_ENV production + # Install only production dependencies # Use cache mount to speed up install of existing dependencies RUN --mount=type=cache,target=/usr/src/app/.npm \ diff --git a/06-building-container-images/api-node/Dockerfile.9 b/06-building-container-images/api-node/Dockerfile.9 index a6b2989..3930047 100644 --- a/06-building-container-images/api-node/Dockerfile.9 +++ b/06-building-container-images/api-node/Dockerfile.9 @@ -2,9 +2,6 @@ # Use alpine for reduced image size FROM node:19.6-alpine AS base -# Set NODE_ENV -ENV NODE_ENV production - # Specify working directory other than / WORKDIR /usr/src/app @@ -12,21 +9,20 @@ WORKDIR /usr/src/app # dependencies (better layer caching) COPY package*.json ./ -# Development stage to include dev + production dependencies -FROM base AS dev +FROM base as dev -# Install all dependencies (including development ones) -# For the dev image RUN --mount=type=cache,target=/usr/src/app/.npm \ npm set cache /usr/src/app/.npm && \ npm install COPY . . -CMD [ "npm", "run", "dev" ] +CMD ["npm", "run", "dev"] -# Production stage optimzed for deployment -FROM base AS production +FROM base as production + +# Set NODE_ENV +ENV NODE_ENV production # Install only production dependencies # Use cache mount to speed up install of existing dependencies diff --git a/11-development-workflow/docker-compose-debug.yml b/11-development-workflow/docker-compose-debug.yml index 0e4b910..4ca1ec4 100644 --- a/11-development-workflow/docker-compose-debug.yml +++ b/11-development-workflow/docker-compose-debug.yml @@ -1,4 +1,5 @@ # Overlay configuration to enable debuggers +version: "3.9" services: api-node: command: diff --git a/11-development-workflow/docker-compose-dev.yml b/11-development-workflow/docker-compose-dev.yml index 05258cd..98523f5 100644 --- a/11-development-workflow/docker-compose-dev.yml +++ b/11-development-workflow/docker-compose-dev.yml @@ -1,12 +1,11 @@ +version: "3.9" services: client-react-vite: + image: client-react-vite build: context: ../05-example-web-application/client-react/ dockerfile: ../../06-building-container-images/client-react/Dockerfile.3 - networks: - - frontend - ports: - - 5173:5173 + init: true volumes: - type: bind source: ../05-example-web-application/client-react/ @@ -16,39 +15,55 @@ services: - type: bind source: ../08-running-containers/client-react/vite.config.js target: /usr/src/app/vite.config.js + networks: + - frontend + ports: + - 5173:5173 + client-react-nginx: + image: client-react-nginx + build: + context: ../05-example-web-application/client-react/ + dockerfile: ../../06-building-container-images/client-react/Dockerfile.5 + init: true + networks: + - frontend + ports: + - 80:8080 restart: unless-stopped api-node: + image: api-node build: context: ../05-example-web-application/api-node/ dockerfile: ../../06-building-container-images/api-node/Dockerfile.9 target: dev + init: true volumes: - type: bind source: ../05-example-web-application/api-node/ target: /usr/src/app/ - type: volume target: /usr/src/app/node_modules - init: true depends_on: - db environment: - DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres networks: - frontend - - backend + - backend ports: - - "3000:3000" + - 3000:3000 restart: unless-stopped api-golang: + image: api-golang build: context: ../05-example-web-application/api-golang/ dockerfile: ../../06-building-container-images/api-golang/Dockerfile.8 target: dev + init: true volumes: - type: bind source: ../05-example-web-application/api-golang/ target: /app/ - init: true depends_on: - db environment: @@ -57,7 +72,7 @@ services: - frontend - backend ports: - - "8080:8080" + - 8080:8080 restart: unless-stopped db: image: postgres:15.1-alpine diff --git a/11-development-workflow/docker-compose-test.yml b/11-development-workflow/docker-compose-test.yml index 22fb0c9..eeefba0 100644 --- a/11-development-workflow/docker-compose-test.yml +++ b/11-development-workflow/docker-compose-test.yml @@ -1,4 +1,5 @@ # Overlay configuration to run tests +version: "3.9" services: api-node: command: @@ -10,4 +11,4 @@ services: - "go" - "test" - "-v" - - "./..." \ No newline at end of file + - "./..."