Skip to content

Instantly share code, notes, and snippets.

@tswaters
Last active October 10, 2022 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tswaters/27676f14d02132ad929a21fbbccae16e to your computer and use it in GitHub Desktop.
Save tswaters/27676f14d02132ad929a21fbbccae16e to your computer and use it in GitHub Desktop.
node dockerfile
.git
node_modules
dist
public
# syntax = docker/dockerfile:1.4
FROM node:18-bullseye-slim AS base
WORKDIR /app
COPY --link package.json package-lock.json /app/
FROM base AS dependencies
RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev
FROM base AS build
RUN --mount=type=cache,target=/root/.npm npm ci
COPY --link . /app
RUN npm run build
FROM gcr.io/distroless/nodejs:18
COPY --link --from=build /app/dist /app/dist/
COPY --link --from=dependencies /app/node_modules /app/node_modules
COPY --link . /app
EXPOSE 3000
EXPOSE 9229
CMD ["/app/dist/index.js"]
#!/bin/bash
set -euxo pipefail
export DOCKER_BUILDKIT=1
docker build . --target build --tag test-image:test
docker run --rm --entrypoint node test-image:test --version
docker run --rm --entrypoint npm test-image:test --version
docker run --rm --entrypoint npm test-image:test run test
docker run --rm --entrypoint npm test-image:test run lint
docker rmi test-image:test
docker build . --tag test-image:release
# push tags to remote
@tswaters
Copy link
Author

Turns out when you start FROM an image, you don't need to pull in node_modules -- it's basically just copying over itself which is unnecessary FS churn. I've amended the gist to reflect this.

@tswaters
Copy link
Author

Updated with using docker buildkit, distroless container for the release, and a few different caching options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment