Created
November 12, 2022 12:55
-
-
Save wodCZ/2c55fa15ae71469c1fd3cb9ff6f1b5ba to your computer and use it in GitHub Desktop.
Node.js Dockerfiles
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
################################## | |
# 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 base as development | |
# Prepare the app directory and node user | |
RUN mkdir /app && chown -R node:node /app | |
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 ["npm", "start:dev"] |
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
FROM node:18-slim as base | |
################################## | |
# Dev dependencies stage, only prepares node_modules, that are later reused | |
################################## | |
FROM base as devdependencies | |
# Prepare the app directory and node user | |
RUN mkdir /app && chown -R node:node /app | |
USER node | |
WORKDIR /app | |
COPY --chown=node:node package.json package-lock.json ./ | |
RUN npm install | |
################################## | |
# 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 base as build | |
# Prepare the app directory and node user | |
RUN mkdir /app && chown -R node:node /app | |
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 npm run build && ls -lah dist | |
################################## | |
# Production stage | |
# - installs only production dependencies (no devDependencies) | |
# - this is achieved by setting NODE_ENV=production | |
# - copies the dist directory from build stage | |
# - sets default NODE_ENV to production | |
# - exposes $PORT (3000) | |
# - sets propper PATH | |
################################## | |
FROM base as production | |
ARG NODE_ENV=production | |
ENV NODE_ENV $NODE_ENV | |
ARG PORT=3000 | |
ENV PORT $PORT | |
EXPOSE $PORT | |
# Prepare the app directory and node user | |
RUN mkdir /app && chown -R node:node /app | |
USER node | |
WORKDIR /app | |
COPY --chown=node:node package.json package-lock.json ./ | |
# Install only production dependencies | |
RUN npm install --cache /tmp/npm && (rm -rf /tmp/* || true) | |
ENV PATH /app/node_modules/.bin:$PATH | |
COPY --chown=node:node --from=build /app/dist ./dist | |
CMD [ "npm", "start:prod" ] | |
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
FROM node:18-slim as base | |
################################## | |
# Dev dependencies stage, only prepares node_modules, that are later reused | |
################################## | |
FROM base as devdependencies | |
# Prepare the app directory and node user | |
RUN mkdir /app && chown -R node:node /app | |
USER node | |
WORKDIR /app | |
COPY --chown=node:node package.json yarn.lock ./ | |
RUN yarn install | |
################################## | |
# 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 base as build | |
# Prepare the app directory and node user | |
RUN mkdir /app && chown -R node:node /app | |
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 dist | |
################################## | |
# Production stage | |
# - installs only production dependencies (no devDependencies) | |
# - this is achieved by setting NODE_ENV=production | |
# - copies the dist directory from build stage | |
# - sets default NODE_ENV to production | |
# - exposes $PORT (3000) | |
# - sets propper PATH | |
################################## | |
FROM base as production | |
ARG NODE_ENV=production | |
ENV NODE_ENV $NODE_ENV | |
ARG PORT=3000 | |
ENV PORT $PORT | |
EXPOSE $PORT | |
# Prepare the app directory and node user | |
RUN mkdir /app && chown -R node:node /app | |
USER node | |
WORKDIR /app | |
COPY --chown=node:node package.json yarn.lock ./ | |
# Install only production dependencies | |
RUN yarn install && yarn cache clean && (rm -rf /tmp/* || true) | |
ENV PATH /app/node_modules/.bin:$PATH | |
COPY --chown=node:node --from=build /app/dist ./dist | |
CMD [ "yarn", "start:prod" ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment