Skip to content

Commit bf6de8d

Browse files
authored
DockerCon 2023 πŸ³βœ…πŸŽ‰πŸ³βœ…πŸŽ‰
1 parent d88b115 commit bf6de8d

14 files changed

Lines changed: 222 additions & 214 deletions

β€ŽREADME.mdβ€Ž

Lines changed: 53 additions & 38 deletions
Large diffs are not rendered by default.
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,31 @@
99
services:
1010
node:
1111
build:
12-
dockerfile: dockerfiles/5.Dockerfile
12+
dockerfile: dockerfiles/3.Dockerfile
1313
context: .
1414
# build to the stage named dev
1515
target: dev
16-
volumes:
17-
- .:/app
16+
# Not needed when `develop: watch` is used
17+
# volumes:
18+
# - .:/app
1819
ports:
1920
# use docker compose ps to see which host port is used
2021
- "3000"
2122
depends_on:
2223
db:
2324
condition: service_healthy
25+
develop:
26+
watch:
27+
- action: sync
28+
path: ./
29+
target: /app
30+
- action: rebuild
31+
path: package.json
32+
- action: rebuild
33+
path: package-lock.json
2434

2535
db:
26-
image: postgres
36+
image: postgres:alpine
2737
environment:
2838
POSTGRES_USER: postgres
2939
POSTGRES_PASSWORD: postgres

β€Ždockerfiles/1.Dockerfileβ€Ž

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1+
# syntax=docker/dockerfile:1
2+
13
###
2-
## Example: run as non-root user
4+
## Example: The most basic, CORRECT, Dockerfile for Node.js
35
###
46

57
# alwyas use slim and the lastest debian distro offered
6-
FROM node:16-bullseye-slim
8+
FROM node:20-bookworm-slim@sha256:8d26608b65edb3b0a0e1958a0a5a45209524c4df54bbe21a4ca53548bc97a3a5
79

810
EXPOSE 3000
911

10-
# change permissions to non-root user
11-
RUN mkdir /app && chown -R node:node /app
12+
# add user first, then set WORKDIR to set permissions
13+
USER node
1214

1315
WORKDIR /app
1416

15-
USER node
16-
1717
# copy in with correct permissions. Using * prevents errors if file is missing
18-
COPY --chown=node:node package*.json yarn*.lock ./
18+
COPY --chown=node:node package*.json ./
1919

2020
# use ci to only install packages from lock files
21-
# we don't have a dev image/stage yet (in future example)
22-
RUN npm ci --only=production && npm cache clean --force
21+
RUN npm ci --omit=dev && npm cache clean --force
2322

2423
# copy files with correct permissions
2524
COPY --chown=node:node . .
2625

27-
# we haven't fixed CMD yet (in future example)
28-
CMD ["npm", "start"]
26+
# change command to run node directly
27+
CMD ["node", "./bin/www"]

β€Ždockerfiles/2.Dockerfileβ€Ž

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
FROM node:16-bullseye-slim
1+
# syntax=docker/dockerfile:1
2+
23
###
34
## Example: run tini first, as PID 1
45
###
56

7+
FROM node:20-bookworm-slim@sha256:8d26608b65edb3b0a0e1958a0a5a45209524c4df54bbe21a4ca53548bc97a3a5
8+
69
# replace npm in CMD with tini for better kernel signal handling
7-
RUN apt-get update \
8-
&& apt-get install -y --no-install-recommends \
9-
tini \
10-
&& rm -rf /var/lib/apt/lists/*
10+
ENV NODE_ENV=production
11+
ENV TINI_VERSION=v0.19.0
12+
ADD --chmod=755 https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/local/bin/tini
13+
1114
# set entrypoint to always run commands with tini
12-
ENTRYPOINT ["/usr/bin/tini", "--"]
15+
ENTRYPOINT ["/usr/local/bin/tini", "--"]
1316

1417
EXPOSE 3000
1518

16-
RUN mkdir /app && chown -R node:node /app
19+
USER node
1720

1821
WORKDIR /app
1922

20-
USER node
21-
22-
COPY --chown=node:node package*.json yarn*.lock ./
23+
COPY --chown=node:node package*.json ./
2324

24-
RUN npm ci --only=production && npm cache clean --force
25+
RUN npm ci --omit=dev && npm cache clean --force
2526

2627
COPY --chown=node:node . .
2728

28-
# change command to run node directly
2929
CMD ["node", "./bin/www"]

β€Ždockerfiles/3.Dockerfileβ€Ž

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1+
# syntax=docker/dockerfile:1
2+
13
###
24
## Adding stages for dev and prod
35
###
4-
FROM node:16-bullseye-slim as base
6+
7+
FROM node:20-bookworm-slim@sha256:8d26608b65edb3b0a0e1958a0a5a45209524c4df54bbe21a4ca53548bc97a3a5 as base
58
ENV NODE_ENV=production
6-
RUN apt-get update \
7-
&& apt-get install -y --no-install-recommends \
8-
tini \
9-
&& rm -rf /var/lib/apt/lists/*
9+
ENV TINI_VERSION=v0.19.0
10+
ADD --chmod=755 https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/local/bin/tini
1011
EXPOSE 3000
11-
RUN mkdir /app && chown -R node:node /app
12-
WORKDIR /app
1312
USER node
14-
COPY --chown=node:node package*.json yarn*.lock ./
15-
RUN npm ci --only=production && npm cache clean --force
16-
COPY --chown=node:node . .
17-
CMD ["node", "./bin/www"]
13+
WORKDIR /app
14+
COPY --chown=node:node package*.json ./
15+
RUN npm ci --omit=dev && npm cache clean --force
16+
ENV PATH=/app/node_modules/.bin:$PATH
1817

1918
# dev stage
2019
FROM base as dev
2120
ENV NODE_ENV=development
22-
ENV PATH=/app/node_modules/.bin:$PATH
2321
RUN npm install
22+
COPY --chown=node:node . .
2423
CMD ["nodemon", "./bin/www", "--inspect=0.0.0.0:9229"]
2524

2625
# prod stage
2726
FROM base as prod
28-
ENTRYPOINT ["/usr/bin/tini", "--"]
29-
CMD ["node", "./bin/www"]
27+
COPY --chown=node:node . .
28+
ENTRYPOINT ["/usr/local/bin/tini", "--"]
29+
CMD ["node", "./bin/www"]

β€Ždockerfiles/4.Dockerfileβ€Ž

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

β€Ždockerfiles/5.Dockerfileβ€Ž

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

β€Ždockerfiles/distroless.Dockerfileβ€Ž

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
# syntax=docker/dockerfile:1
2+
13
###
24
## Distroless in Prod. Multi-stage dev/test/prod with distroless
35
###
4-
FROM gcr.io/distroless/nodejs@sha256:794e26246770ff28d285d7f800ce1982883cf4105662845689efa33f04ec4340 as distroless
5-
FROM node:16-bullseye-slim as base
6+
7+
FROM gcr.io/distroless/nodejs20-debian12:latest@sha256:6499c05db574451eeddda4d3ddb374ac1aba412d6b2f5d215cc5e23c40c0e4d3 as distroless
8+
FROM node:20-slim as base
69
ENV NODE_ENV=production
710
RUN apt-get update \
811
&& apt-get install -y --no-install-recommends \
@@ -24,14 +27,6 @@ CMD ["nodemon", "./bin/www", "--inspect=0.0.0.0:9229"]
2427
FROM base as source
2528
COPY --chown=node:node . .
2629

27-
FROM source as test
28-
ENV NODE_ENV=development
29-
ENV PATH=/app/node_modules/.bin:$PATH
30-
COPY --from=dev /app/node_modules /app/node_modules
31-
RUN npx eslint .
32-
RUN npm test
33-
CMD ["npm", "run", "test"]
34-
3530
# switch to distroless for prod
3631
# use version tags for always building with latest
3732
# (more risky for stability, but likely more secure)

β€Ždockerfiles/snyk.shβ€Ž

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
echo "[" > summary.json
4+
for image in $(cat tags.txt); do
5+
image_file=$(echo ${image} | tr '/' '-' | tr ':' '-')
6+
tag=$(echo ${image} | cut -f 2 -d '/' | cut -f 2 -d ':')
7+
echo "Testing ${image}..."
8+
9+
if [[ "$1" == "--no-cache" || ! -f snyk.${image_file}.json ]]; then
10+
DOCKER_CLI_HINTS=false docker pull ${image}
11+
snyk container test ${image} --exclude-app-vulns --json-file-output=snyk.${image_file}.json --group-issues > snyk.${image_file}.log
12+
fi
13+
summary=$(jq -c '[ .vulnerabilities[].severity] | reduce .[] as $sev ({}; .[$sev] +=1) | { image: "'${image}'", low: (.low // 0), medium: (.medium // 0), high: (.high // 0), critical: (.critical // 0)} | .total = .low + .medium + .high + .critical ' snyk.${image_file}.json)
14+
echo " ${summary}," >> summary.json
15+
done
16+
echo "]" >> summary.json
17+
18+
cat summary.json

β€Ždockerfiles/tags.txtβ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
node:20
2+
node:20-slim
3+
node:20-alpine
4+
node:18
5+
node:18-slim
6+
node:18-alpine
7+
debian:12
8+
debian:12-slim
9+
ubuntu:22.04
10+
bretfisher/node:ubuntu-22.04-nodesource18
11+
bretfisher/node:ubuntu-22.04-nodesource20
12+
bretfisher/node:ubuntu-22.04-node20-copy
13+
gcr.io/distroless/nodejs20-debian12
14+
cgr.dev/chainguard/node:latest

0 commit comments

Comments
Β (0)