-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DockerCon 2023 π³β
ππ³β
π
- Loading branch information
1 parent
d88b115
commit bf6de8d
Showing
14 changed files
with
222 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
### | ||
## Example: run as non-root user | ||
## Example: The most basic, CORRECT, Dockerfile for Node.js | ||
### | ||
|
||
# alwyas use slim and the lastest debian distro offered | ||
FROM node:16-bullseye-slim | ||
FROM node:20-bookworm-slim@sha256:8d26608b65edb3b0a0e1958a0a5a45209524c4df54bbe21a4ca53548bc97a3a5 | ||
|
||
EXPOSE 3000 | ||
|
||
# change permissions to non-root user | ||
RUN mkdir /app && chown -R node:node /app | ||
# add user first, then set WORKDIR to set permissions | ||
USER node | ||
|
||
WORKDIR /app | ||
|
||
USER node | ||
|
||
# copy in with correct permissions. Using * prevents errors if file is missing | ||
COPY --chown=node:node package*.json yarn*.lock ./ | ||
COPY --chown=node:node package*.json ./ | ||
|
||
# use ci to only install packages from lock files | ||
# we don't have a dev image/stage yet (in future example) | ||
RUN npm ci --only=production && npm cache clean --force | ||
RUN npm ci --omit=dev && npm cache clean --force | ||
|
||
# copy files with correct permissions | ||
COPY --chown=node:node . . | ||
|
||
# we haven't fixed CMD yet (in future example) | ||
CMD ["npm", "start"] | ||
# change command to run node directly | ||
CMD ["node", "./bin/www"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
FROM node:16-bullseye-slim | ||
# syntax=docker/dockerfile:1 | ||
|
||
### | ||
## Example: run tini first, as PID 1 | ||
### | ||
|
||
FROM node:20-bookworm-slim@sha256:8d26608b65edb3b0a0e1958a0a5a45209524c4df54bbe21a4ca53548bc97a3a5 | ||
|
||
# replace npm in CMD with tini for better kernel signal handling | ||
RUN apt-get update \ | ||
&& apt-get install -y --no-install-recommends \ | ||
tini \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
ENV NODE_ENV=production | ||
ENV TINI_VERSION=v0.19.0 | ||
ADD --chmod=755 https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/local/bin/tini | ||
|
||
# set entrypoint to always run commands with tini | ||
ENTRYPOINT ["/usr/bin/tini", "--"] | ||
ENTRYPOINT ["/usr/local/bin/tini", "--"] | ||
|
||
EXPOSE 3000 | ||
|
||
RUN mkdir /app && chown -R node:node /app | ||
USER node | ||
|
||
WORKDIR /app | ||
|
||
USER node | ||
|
||
COPY --chown=node:node package*.json yarn*.lock ./ | ||
COPY --chown=node:node package*.json ./ | ||
|
||
RUN npm ci --only=production && npm cache clean --force | ||
RUN npm ci --omit=dev && npm cache clean --force | ||
|
||
COPY --chown=node:node . . | ||
|
||
# change command to run node directly | ||
CMD ["node", "./bin/www"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
### | ||
## Adding stages for dev and prod | ||
### | ||
FROM node:16-bullseye-slim as base | ||
|
||
FROM node:20-bookworm-slim@sha256:8d26608b65edb3b0a0e1958a0a5a45209524c4df54bbe21a4ca53548bc97a3a5 as base | ||
ENV NODE_ENV=production | ||
RUN apt-get update \ | ||
&& apt-get install -y --no-install-recommends \ | ||
tini \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
ENV TINI_VERSION=v0.19.0 | ||
ADD --chmod=755 https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/local/bin/tini | ||
EXPOSE 3000 | ||
RUN mkdir /app && chown -R node:node /app | ||
WORKDIR /app | ||
USER node | ||
COPY --chown=node:node package*.json yarn*.lock ./ | ||
RUN npm ci --only=production && npm cache clean --force | ||
COPY --chown=node:node . . | ||
CMD ["node", "./bin/www"] | ||
WORKDIR /app | ||
COPY --chown=node:node package*.json ./ | ||
RUN npm ci --omit=dev && npm cache clean --force | ||
ENV PATH=/app/node_modules/.bin:$PATH | ||
|
||
# dev stage | ||
FROM base as dev | ||
ENV NODE_ENV=development | ||
ENV PATH=/app/node_modules/.bin:$PATH | ||
RUN npm install | ||
COPY --chown=node:node . . | ||
CMD ["nodemon", "./bin/www", "--inspect=0.0.0.0:9229"] | ||
|
||
# prod stage | ||
FROM base as prod | ||
ENTRYPOINT ["/usr/bin/tini", "--"] | ||
CMD ["node", "./bin/www"] | ||
COPY --chown=node:node . . | ||
ENTRYPOINT ["/usr/local/bin/tini", "--"] | ||
CMD ["node", "./bin/www"] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
|
||
echo "[" > summary.json | ||
for image in $(cat tags.txt); do | ||
image_file=$(echo ${image} | tr '/' '-' | tr ':' '-') | ||
tag=$(echo ${image} | cut -f 2 -d '/' | cut -f 2 -d ':') | ||
echo "Testing ${image}..." | ||
|
||
if [[ "$1" == "--no-cache" || ! -f snyk.${image_file}.json ]]; then | ||
DOCKER_CLI_HINTS=false docker pull ${image} | ||
snyk container test ${image} --exclude-app-vulns --json-file-output=snyk.${image_file}.json --group-issues > snyk.${image_file}.log | ||
fi | ||
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) | ||
echo " ${summary}," >> summary.json | ||
done | ||
echo "]" >> summary.json | ||
|
||
cat summary.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
node:20 | ||
node:20-slim | ||
node:20-alpine | ||
node:18 | ||
node:18-slim | ||
node:18-alpine | ||
debian:12 | ||
debian:12-slim | ||
ubuntu:22.04 | ||
bretfisher/node:ubuntu-22.04-nodesource18 | ||
bretfisher/node:ubuntu-22.04-nodesource20 | ||
bretfisher/node:ubuntu-22.04-node20-copy | ||
gcr.io/distroless/nodejs20-debian12 | ||
cgr.dev/chainguard/node:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.