Port dev dockerfiles into stages

This commit is contained in:
sid palas
2023-02-21 16:18:47 -05:00
parent 7010a748e0
commit 3434051029
10 changed files with 148 additions and 41 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
**/node_modules **/node_modules
**/.npm
*.un~ *.un~
*.json~ *.json~

View File

@ -1 +1,2 @@
node_modules node_modules
.npm

View File

@ -1 +1,2 @@
node_modules node_modules
.npm

View File

@ -0,0 +1,72 @@
# 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
WORKDIR /app
# Copy only files required to install dependencies (better layer caching)
COPY go.mod go.sum ./
# Use cache mount to speed up install of existing dependencies
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
# Install air for hot reload & delve for debugging
RUN go install github.com/cosmtrek/air@latest && \
go install github.com/go-delve/delve/cmd/dlv@latest
COPY . .
CMD ["air", "-c", ".air.toml"]
# Production builder stage to produce the static binaries
FROM base-builder AS production-builder
COPY . .
# Compile healthcheck
RUN go build \
-ldflags="-linkmode external -extldflags -static" \
-tags netgo \
-o healthcheck \
./healthcheck/healthcheck.go
# Compile application during build rather than at runtime
# Add flags to statically link binary
RUN go build \
-ldflags="-linkmode external -extldflags -static" \
-tags netgo \
-o api-golang
# Use separate stage for deployable image
FROM scratch AS production
# Set gin mode
ENV GIN_MODE=release
WORKDIR /
# Copy the passwd file
COPY --from=production-builder /etc/passwd /etc/passwd
# Copy the healthcheck binary from the build stage
COPY --from=production-builder /app/healthcheck/healthcheck healthcheck
# Copy the app binary from the build stage
COPY --from=production-builder /app/api-golang api-golang
# Use nonroot user
USER nonroot
# Indicate expected port
EXPOSE 8080
CMD ["/api-golang"]

View File

@ -0,0 +1,51 @@
# Pin specific version for stability
# 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
# Copy only files required to install
# dependencies (better layer caching)
COPY package*.json ./
# Development stage to include dev + production dependencies
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" ]
# Production stage optimzed for deployment
FROM base AS production
# Install only production dependencies
# Use cache mount to speed up install of existing dependencies
RUN --mount=type=cache,target=/usr/src/app/.npm \
npm set cache /usr/src/app/.npm && \
npm ci --only=production
# Use non-root user
# Use --chown on COPY commands to set file permissions
USER node
# Copy the healthcheck script
COPY --chown=node:node ./healthcheck/ .
# Copy remaining source code AFTER installing dependencies.
# Again, copy only the necessary files
COPY --chown=node:node ./src/ .
# Indicate expected port
EXPOSE 3000
CMD [ "node", "index.js" ]

View File

@ -84,7 +84,7 @@ docker-run-all:
docker run -d \ docker run -d \
--name client-react-nginx \ --name client-react-nginx \
--network my-network \ --network my-network \
-p 5174:80 \ -p 80:8080 \
--restart unless-stopped \ --restart unless-stopped \
--link=api-node \ --link=api-node \
--link=api-golang \ --link=api-golang \

View File

@ -3,7 +3,7 @@ services:
image: client-react-vite image: client-react-vite
build: build:
context: ../05-example-web-application/client-react/ context: ../05-example-web-application/client-react/
dockerfile: ../../06-building-container-images/client-react/Dockerfile.4 dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
init: true init: true
volumes: volumes:
- ${PWD}/client-react/vite.config.js:/usr/src/app/vite.config.js - ${PWD}/client-react/vite.config.js:/usr/src/app/vite.config.js
@ -26,7 +26,7 @@ services:
image: api-node image: api-node
build: build:
context: ../05-example-web-application/api-node/ context: ../05-example-web-application/api-node/
dockerfile: ../../06-building-container-images/api-node/Dockerfile.8 dockerfile: ../../06-building-container-images/api-node/Dockerfile.7
init: true init: true
depends_on: depends_on:
- db - db

View File

@ -1,18 +0,0 @@
# Using bullseye instead of alpine because debugger didnt work in alpine
FROM golang:1.19-bullseye
WORKDIR /app
# Install air for hot reload
RUN go install github.com/cosmtrek/air@latest
# Install delve for debugging
RUN go install github.com/go-delve/delve/cmd/dlv@latest
COPY go.mod go.sum ./
RUN go mod download
COPY . .
CMD ["air", "-c", ".air.toml"]

View File

@ -1,15 +0,0 @@
FROM node:19.4-alpine as dev
WORKDIR /usr/src/app
COPY package*.json ./
RUN --mount=type=cache,target=/usr/src/app/.npm \
npm set cache /usr/src/app/.npm && \
npm install
COPY . .
EXPOSE 3000
CMD [ "npm", "run", "dev" ]

View File

@ -3,6 +3,8 @@ services:
build: build:
context: ../05-example-web-application/client-react/ context: ../05-example-web-application/client-react/
dockerfile: ../../06-building-container-images/client-react/Dockerfile.3 dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
networks:
- frontend
ports: ports:
- 5173:5173 - 5173:5173
volumes: volumes:
@ -18,7 +20,7 @@ services:
api-node: api-node:
build: build:
context: ../05-example-web-application/api-node/ context: ../05-example-web-application/api-node/
dockerfile: ../../11-development-workflow/api-node/Dockerfile.dev dockerfile: ../../06-building-container-images/api-node/Dockerfile.9
target: dev target: dev
volumes: volumes:
- type: bind - type: bind
@ -31,13 +33,17 @@ services:
- db - db
environment: environment:
- DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres - DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres
networks:
- frontend
- backend
ports: ports:
- "3000:3000" - "3000:3000"
restart: unless-stopped restart: unless-stopped
api-golang: api-golang:
build: build:
context: ../05-example-web-application/api-golang/ context: ../05-example-web-application/api-golang/
dockerfile: ../../11-development-workflow/api-golang/Dockerfile.dev dockerfile: ../../06-building-container-images/api-golang/Dockerfile.8
target: dev
volumes: volumes:
- type: bind - type: bind
source: ../05-example-web-application/api-golang/ source: ../05-example-web-application/api-golang/
@ -47,6 +53,9 @@ services:
- db - db
environment: environment:
- DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres - DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres
networks:
- frontend
- backend
ports: ports:
- "8080:8080" - "8080:8080"
restart: unless-stopped restart: unless-stopped
@ -56,7 +65,12 @@ services:
- pgdata:/var/lib/postgresql/data - pgdata:/var/lib/postgresql/data
environment: environment:
- POSTGRES_PASSWORD=foobarbaz - POSTGRES_PASSWORD=foobarbaz
networks:
- backend
ports: ports:
- 5432:5432 - 5432:5432
volumes: volumes:
pgdata: pgdata:
networks:
frontend:
backend: