Skip to content

Commit 3434051

Browse files
committed
Port dev dockerfiles into stages
1 parent 7010a74 commit 3434051

10 files changed

Lines changed: 148 additions & 41 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
**/node_modules
2+
**/.npm
23
*.un~
34
*.json~
45

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
.npm
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
.npm
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Pin specific version for stability
2+
# Use separate stage for building image
3+
# Use debian for easier build utilities
4+
FROM golang:1.19-bullseye AS base-builder
5+
6+
# Add non root user
7+
RUN useradd -u 1001 nonroot
8+
9+
WORKDIR /app
10+
11+
# Copy only files required to install dependencies (better layer caching)
12+
COPY go.mod go.sum ./
13+
14+
# Use cache mount to speed up install of existing dependencies
15+
RUN --mount=type=cache,target=/go/pkg/mod \
16+
--mount=type=cache,target=/root/.cache/go-build \
17+
go mod download
18+
19+
# Dev stage with additional dev dependencies installed
20+
FROM base-builder AS dev
21+
22+
# Install air for hot reload & delve for debugging
23+
RUN go install github.com/cosmtrek/air@latest && \
24+
go install github.com/go-delve/delve/cmd/dlv@latest
25+
26+
COPY . .
27+
28+
CMD ["air", "-c", ".air.toml"]
29+
30+
# Production builder stage to produce the static binaries
31+
FROM base-builder AS production-builder
32+
33+
COPY . .
34+
35+
# Compile healthcheck
36+
RUN go build \
37+
-ldflags="-linkmode external -extldflags -static" \
38+
-tags netgo \
39+
-o healthcheck \
40+
./healthcheck/healthcheck.go
41+
42+
# Compile application during build rather than at runtime
43+
# Add flags to statically link binary
44+
RUN go build \
45+
-ldflags="-linkmode external -extldflags -static" \
46+
-tags netgo \
47+
-o api-golang
48+
49+
# Use separate stage for deployable image
50+
FROM scratch AS production
51+
52+
# Set gin mode
53+
ENV GIN_MODE=release
54+
55+
WORKDIR /
56+
57+
# Copy the passwd file
58+
COPY --from=production-builder /etc/passwd /etc/passwd
59+
60+
# Copy the healthcheck binary from the build stage
61+
COPY --from=production-builder /app/healthcheck/healthcheck healthcheck
62+
63+
# Copy the app binary from the build stage
64+
COPY --from=production-builder /app/api-golang api-golang
65+
66+
# Use nonroot user
67+
USER nonroot
68+
69+
# Indicate expected port
70+
EXPOSE 8080
71+
72+
CMD ["/api-golang"]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Pin specific version for stability
2+
# Use alpine for reduced image size
3+
FROM node:19.6-alpine AS base
4+
5+
# Set NODE_ENV
6+
ENV NODE_ENV production
7+
8+
# Specify working directory other than /
9+
WORKDIR /usr/src/app
10+
11+
# Copy only files required to install
12+
# dependencies (better layer caching)
13+
COPY package*.json ./
14+
15+
# Development stage to include dev + production dependencies
16+
FROM base AS dev
17+
18+
# Install all dependencies (including development ones)
19+
# For the dev image
20+
RUN --mount=type=cache,target=/usr/src/app/.npm \
21+
npm set cache /usr/src/app/.npm && \
22+
npm install
23+
24+
COPY . .
25+
26+
CMD [ "npm", "run", "dev" ]
27+
28+
# Production stage optimzed for deployment
29+
FROM base AS production
30+
31+
# Install only production dependencies
32+
# Use cache mount to speed up install of existing dependencies
33+
RUN --mount=type=cache,target=/usr/src/app/.npm \
34+
npm set cache /usr/src/app/.npm && \
35+
npm ci --only=production
36+
37+
# Use non-root user
38+
# Use --chown on COPY commands to set file permissions
39+
USER node
40+
41+
# Copy the healthcheck script
42+
COPY --chown=node:node ./healthcheck/ .
43+
44+
# Copy remaining source code AFTER installing dependencies.
45+
# Again, copy only the necessary files
46+
COPY --chown=node:node ./src/ .
47+
48+
# Indicate expected port
49+
EXPOSE 3000
50+
51+
CMD [ "node", "index.js" ]

08-running-containers/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ docker-run-all:
8484
docker run -d \
8585
--name client-react-nginx \
8686
--network my-network \
87-
-p 5174:80 \
87+
-p 80:8080 \
8888
--restart unless-stopped \
8989
--link=api-node \
9090
--link=api-golang \

08-running-containers/docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
image: client-react-vite
44
build:
55
context: ../05-example-web-application/client-react/
6-
dockerfile: ../../06-building-container-images/client-react/Dockerfile.4
6+
dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
77
init: true
88
volumes:
99
- ${PWD}/client-react/vite.config.js:/usr/src/app/vite.config.js
@@ -26,7 +26,7 @@ services:
2626
image: api-node
2727
build:
2828
context: ../05-example-web-application/api-node/
29-
dockerfile: ../../06-building-container-images/api-node/Dockerfile.8
29+
dockerfile: ../../06-building-container-images/api-node/Dockerfile.7
3030
init: true
3131
depends_on:
3232
- db

11-development-workflow/api-golang/Dockerfile.dev

Lines changed: 0 additions & 18 deletions
This file was deleted.

11-development-workflow/api-node/Dockerfile.dev

Lines changed: 0 additions & 15 deletions
This file was deleted.

11-development-workflow/docker-compose-dev.yml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ services:
33
build:
44
context: ../05-example-web-application/client-react/
55
dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
6+
networks:
7+
- frontend
68
ports:
79
- 5173:5173
810
volumes:
@@ -18,7 +20,7 @@ services:
1820
api-node:
1921
build:
2022
context: ../05-example-web-application/api-node/
21-
dockerfile: ../../11-development-workflow/api-node/Dockerfile.dev
23+
dockerfile: ../../06-building-container-images/api-node/Dockerfile.9
2224
target: dev
2325
volumes:
2426
- type: bind
@@ -31,13 +33,17 @@ services:
3133
- db
3234
environment:
3335
- DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres
36+
networks:
37+
- frontend
38+
- backend
3439
ports:
3540
- "3000:3000"
3641
restart: unless-stopped
3742
api-golang:
3843
build:
3944
context: ../05-example-web-application/api-golang/
40-
dockerfile: ../../11-development-workflow/api-golang/Dockerfile.dev
45+
dockerfile: ../../06-building-container-images/api-golang/Dockerfile.8
46+
target: dev
4147
volumes:
4248
- type: bind
4349
source: ../05-example-web-application/api-golang/
@@ -47,6 +53,9 @@ services:
4753
- db
4854
environment:
4955
- DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres
56+
networks:
57+
- frontend
58+
- backend
5059
ports:
5160
- "8080:8080"
5261
restart: unless-stopped
@@ -56,7 +65,12 @@ services:
5665
- pgdata:/var/lib/postgresql/data
5766
environment:
5867
- POSTGRES_PASSWORD=foobarbaz
68+
networks:
69+
- backend
5970
ports:
6071
- 5432:5432
6172
volumes:
62-
pgdata:
73+
pgdata:
74+
networks:
75+
frontend:
76+
backend:

0 commit comments

Comments
 (0)