Skip to content

Instantly share code, notes, and snippets.

@smaeda-ks
Created June 13, 2022 14:28
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 smaeda-ks/a605a092f5c46360be7bef22deacd25c to your computer and use it in GitHub Desktop.
Save smaeda-ks/a605a092f5c46360be7bef22deacd25c to your computer and use it in GitHub Desktop.
distroless + slim
#### base ####
FROM gcr.io/distroless/nodejs-debian11:16@sha256:ed96d9578e2d4d15aa18bd832ea0926f23e440634407c41882a26a84b2810b9e as distroless
FROM node:16-bullseye-slim as base
ENV NODE_ENV=production
# Avoid running nodejs process as PID 1
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
tini \
&& rm -rf /var/lib/apt/lists/*
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
#### dev ####
# no source to be added, and assumes bind mount
FROM base as dev
ENV NODE_ENV=development
ENV PATH=/app/node_modules/.bin:$PATH
RUN npm install --only=development && npm cache clean --force
CMD ["nodemon", "index.js"]
#### source ####
FROM base as source
COPY --chown=node:node . .
#### test (as needed) ####
# FROM source as test
# ENV NODE_ENV=development
# ENV PATH=/app/node_modules/.bin:$PATH
# COPY --from=dev /app/node_modules /app/node_modules
# RUN npx eslint .
# RUN npm test
# CMD ["npm", "run", "test"]
#### prod ####
# switch to distroless for prod
FROM distroless as prod
COPY --from=source --chown=1000:1000 /app /app
COPY --from=base /usr/bin/tini /usr/bin/tini
USER 1000
EXPOSE 3000
ENV NODE_ENV=production
ENV PATH=/app/node_modules/.bin:$PATH
WORKDIR /app
ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/nodejs/bin/node", "index.js"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment