Created
October 15, 2021 11:34
-
-
Save wodCZ/3ad40e5fda854e695d23518e6dc27b76 to your computer and use it in GitHub Desktop.
Sample node.js Dockerfile
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
################################## | |
# Dev dependencies stage, only prepares node_modules, that are later reused | |
################################## | |
FROM node:14-slim as devdependencies | |
USER node | |
WORKDIR /app | |
COPY --chown=node:node package.json yarn.lock ./ | |
# Use docker mount cache, with locked sharing to avoid concurrency issues. | |
# Sets uid & gid of the node user for correct permissions | |
RUN --mount=type=cache,target=/home/node/.cache/yarn,uid=1000,gid=1000,sharing=locked yarn install --ignore-optional | |
################################## | |
# Build stage, creates a /app/dist directory with the compiled app. | |
# We don't use this stage as production, because it needs dev dependencies for the build, but we don't | |
# need these to actually run the compiled app | |
################################## | |
FROM node:14-slim as build | |
USER node | |
WORKDIR /app | |
ARG NODE_ENV=production | |
ENV NODE_ENV $NODE_ENV | |
COPY --chown=node:node --from=devdependencies /app . | |
COPY --chown=node:node . . | |
RUN yarn run build && ls -lah | |
################################## | |
# Production stage | |
# - installs only production dependencies (no devDependencies) | |
# - copies the dist directory from build stage | |
# - sets default NODE_ENV to production | |
# - exposes $PORT (3000) | |
# - sets propper PATH | |
################################## | |
FROM node:14-slim as production | |
ARG NODE_ENV=production | |
ENV NODE_ENV $NODE_ENV | |
ARG PORT=3000 | |
ENV PORT $PORT | |
EXPOSE $PORT | |
USER node | |
WORKDIR /app | |
COPY --chown=node:node package.json yarn.lock ./ | |
# Use docker mount cache, with locked sharing to avoid concurrency issues. | |
# Sets uid & gid of the node user for correct permissions | |
RUN --mount=type=cache,target=/home/node/.cache/yarn,uid=1000,gid=1000,sharing=locked yarn install --ignore-optional && (rm -rf /tmp/* || true) | |
ENV PATH /app/node_modules/.bin:$PATH | |
COPY --chown=node:node --from=build /app/dist ./ | |
CMD [ "node", "src/main.js" ] | |
################################## | |
# Development stage | |
# This is not used at the moment, since the node_modules synchronization with host is complicated. | |
# Left here for future reference if we decide to run the app in docker during development. | |
################################## | |
FROM node:14-slim as development | |
USER node | |
WORKDIR /app | |
ARG NODE_ENV=development | |
ENV NODE_ENV $NODE_ENV | |
# default to port 3000 for node, and 9229 and 9230 (tests) for debug | |
ARG PORT=3000 | |
ENV PORT $PORT | |
EXPOSE $PORT 9229 9230 | |
ENV PATH /app/node_modules/.bin:$PATH | |
COPY --chown=node:node --from=devdependencies /app . | |
COPY --chown=node:node . . | |
CMD ["yarn", "start:dev"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment