Skip to content

Commit e796267

Browse files
committed
update dev dockerfiles
1 parent d2a8e3a commit e796267

6 files changed

Lines changed: 59 additions & 36 deletions

File tree

06-building-container-images/api-golang/Dockerfile.8

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# Pin specific version for stability
22
# Use separate stage for building image
33
# 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
4+
FROM golang:1.19-bullseye AS build-base
85

96
WORKDIR /app
107

@@ -16,8 +13,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \
1613
--mount=type=cache,target=/root/.cache/go-build \
1714
go mod download
1815

19-
# Dev stage with additional dev dependencies installed
20-
FROM base-builder AS dev
16+
FROM build-base AS dev
2117

2218
# Install air for hot reload & delve for debugging
2319
RUN go install github.com/cosmtrek/air@latest && \
@@ -27,8 +23,10 @@ COPY . .
2723

2824
CMD ["air", "-c", ".air.toml"]
2925

30-
# Production builder stage to produce the static binaries
31-
FROM base-builder AS production-builder
26+
FROM build-base AS build-production
27+
28+
# Add non root user
29+
RUN useradd -u 1001 nonroot
3230

3331
COPY . .
3432

@@ -47,21 +45,21 @@ RUN go build \
4745
-o api-golang
4846

4947
# Use separate stage for deployable image
50-
FROM scratch AS production
48+
FROM scratch
5149

5250
# Set gin mode
5351
ENV GIN_MODE=release
5452

5553
WORKDIR /
5654

5755
# Copy the passwd file
58-
COPY --from=production-builder /etc/passwd /etc/passwd
56+
COPY --from=build-production /etc/passwd /etc/passwd
5957

6058
# Copy the healthcheck binary from the build stage
61-
COPY --from=production-builder /app/healthcheck/healthcheck healthcheck
59+
COPY --from=build-production /app/healthcheck/healthcheck healthcheck
6260

6361
# Copy the app binary from the build stage
64-
COPY --from=production-builder /app/api-golang api-golang
62+
COPY --from=build-production /app/api-golang api-golang
6563

6664
# Use nonroot user
6765
USER nonroot

06-building-container-images/api-node/Dockerfile.8

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Pin specific version for stability
22
# Use alpine for reduced image size
3-
FROM node:19.6-alpine
4-
5-
# Set NODE_ENV
6-
ENV NODE_ENV production
3+
FROM node:19.6-alpine AS base
74

85
# Specify working directory other than /
96
WORKDIR /usr/src/app
@@ -12,6 +9,21 @@ WORKDIR /usr/src/app
129
# dependencies (better layer caching)
1310
COPY package*.json ./
1411

12+
FROM base as dev
13+
14+
RUN --mount=type=cache,target=/usr/src/app/.npm \
15+
npm set cache /usr/src/app/.npm && \
16+
npm install
17+
18+
COPY . .
19+
20+
CMD ["npm", "run", "dev"]
21+
22+
FROM base as production
23+
24+
# Set NODE_ENV
25+
ENV NODE_ENV production
26+
1527
# Install only production dependencies
1628
# Use cache mount to speed up install of existing dependencies
1729
RUN --mount=type=cache,target=/usr/src/app/.npm \

06-building-container-images/api-node/Dockerfile.9

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,27 @@
22
# Use alpine for reduced image size
33
FROM node:19.6-alpine AS base
44

5-
# Set NODE_ENV
6-
ENV NODE_ENV production
7-
85
# Specify working directory other than /
96
WORKDIR /usr/src/app
107

118
# Copy only files required to install
129
# dependencies (better layer caching)
1310
COPY package*.json ./
1411

15-
# Development stage to include dev + production dependencies
16-
FROM base AS dev
12+
FROM base as dev
1713

18-
# Install all dependencies (including development ones)
19-
# For the dev image
2014
RUN --mount=type=cache,target=/usr/src/app/.npm \
2115
npm set cache /usr/src/app/.npm && \
2216
npm install
2317

2418
COPY . .
2519

26-
CMD [ "npm", "run", "dev" ]
20+
CMD ["npm", "run", "dev"]
2721

28-
# Production stage optimzed for deployment
29-
FROM base AS production
22+
FROM base as production
23+
24+
# Set NODE_ENV
25+
ENV NODE_ENV production
3026

3127
# Install only production dependencies
3228
# Use cache mount to speed up install of existing dependencies

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Overlay configuration to enable debuggers
2+
version: "3.9"
23
services:
34
api-node:
45
command:

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
version: "3.9"
12
services:
23
client-react-vite:
4+
image: client-react-vite
35
build:
46
context: ../05-example-web-application/client-react/
57
dockerfile: ../../06-building-container-images/client-react/Dockerfile.3
6-
networks:
7-
- frontend
8-
ports:
9-
- 5173:5173
8+
init: true
109
volumes:
1110
- type: bind
1211
source: ../05-example-web-application/client-react/
@@ -16,39 +15,55 @@ services:
1615
- type: bind
1716
source: ../08-running-containers/client-react/vite.config.js
1817
target: /usr/src/app/vite.config.js
18+
networks:
19+
- frontend
20+
ports:
21+
- 5173:5173
22+
client-react-nginx:
23+
image: client-react-nginx
24+
build:
25+
context: ../05-example-web-application/client-react/
26+
dockerfile: ../../06-building-container-images/client-react/Dockerfile.5
27+
init: true
28+
networks:
29+
- frontend
30+
ports:
31+
- 80:8080
1932
restart: unless-stopped
2033
api-node:
34+
image: api-node
2135
build:
2236
context: ../05-example-web-application/api-node/
2337
dockerfile: ../../06-building-container-images/api-node/Dockerfile.9
2438
target: dev
39+
init: true
2540
volumes:
2641
- type: bind
2742
source: ../05-example-web-application/api-node/
2843
target: /usr/src/app/
2944
- type: volume
3045
target: /usr/src/app/node_modules
31-
init: true
3246
depends_on:
3347
- db
3448
environment:
3549
- DATABASE_URL=postgres://postgres:foobarbaz@db:5432/postgres
3650
networks:
3751
- frontend
38-
- backend
52+
- backend
3953
ports:
40-
- "3000:3000"
54+
- 3000:3000
4155
restart: unless-stopped
4256
api-golang:
57+
image: api-golang
4358
build:
4459
context: ../05-example-web-application/api-golang/
4560
dockerfile: ../../06-building-container-images/api-golang/Dockerfile.8
4661
target: dev
62+
init: true
4763
volumes:
4864
- type: bind
4965
source: ../05-example-web-application/api-golang/
5066
target: /app/
51-
init: true
5267
depends_on:
5368
- db
5469
environment:
@@ -57,7 +72,7 @@ services:
5772
- frontend
5873
- backend
5974
ports:
60-
- "8080:8080"
75+
- 8080:8080
6176
restart: unless-stopped
6277
db:
6378
image: postgres:15.1-alpine

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Overlay configuration to run tests
2+
version: "3.9"
23
services:
34
api-node:
45
command:
@@ -10,4 +11,4 @@ services:
1011
- "go"
1112
- "test"
1213
- "-v"
13-
- "./..."
14+
- "./..."

0 commit comments

Comments
 (0)