Skip to content

Instantly share code, notes, and snippets.

@neverovski
Last active November 27, 2023 20:04
Show Gist options
  • Save neverovski/c50d61bc11b3cdca23a71d8bcf9caee7 to your computer and use it in GitHub Desktop.
Save neverovski/c50d61bc11b3cdca23a71d8bcf9caee7 to your computer and use it in GitHub Desktop.
Dockerfile for Node.js

Dockerfile for Node.js

This Gist contains a sample Dockerfile for building and running a Node.js application within a Docker container.

Usage:

Clone or download this Gist into your Node.js project directory. Customize the Dockerfile based on your project's requirements. Build the Docker image using the command

$ docker build -t your-image-name ..

Run a container from the built image (adjust the port numbers as needed).

$ docker run -p 5656:5656 your-image-name

Notes:

The provided Dockerfile assumes a basic Node.js application structure. Modify it according to your specific project setup. Ensure that Docker is installed on your system before building and running the container.

Contributions and Issues:

Contributions and feedback are welcome! Feel free to open issues or pull requests for improvements, bug fixes, or additional features related to this Node.js Dockerfile.

# Step 1 - Install dependencies
FROM node:20-alpine3.17 AS deps
LABEL author="Dmitry Neverovski <dmitryneverovski@gmail.com>"
WORKDIR /app
COPY --chown=node:node package*.json ./
RUN npm ci
USER node
# Step 2 - Build the source code
FROM node:20-alpine3.17 AS build
LABEL author="Dmitry Neverovski <dmitryneverovski@gmail.com>"
ARG NODE_ENV
ENV NODE_ENV=${NODE_ENV:-development}
WORKDIR /app
COPY --chown=node:node package*.json ./
COPY --chown=node:node --from=deps /app/node_modules ./node_modules
COPY --chown=node:node . .
RUN NODE_ENV=${NODE_ENV} npm run build
USER node
# Step 3 - Instal dependencies without devDependencies
FROM node:20-alpine3.17 AS modules
LABEL author="Dmitry Neverovski <dmitryneverovski@gmail.com>"
ARG NODE_ENV
ENV NODE_ENV=${NODE_ENV:-development}
WORKDIR /app
COPY --chown=node:node package*.json ./
RUN npm ci --omit=dev --ignore-scripts
USER node
# Step - 5 Production image, copy all the files and run next
FROM node:20-alpine3.17 AS runner
LABEL author="Dmitry Neverovski <dmitryneverovski@gmail.com>"
ARG APP_PORT
ENV APP_PORT=${APP_PORT:-5656}
WORKDIR /app
USER node
COPY --chown=node:node package*.json ./
COPY --chown=node:node --from=modules /app/node_modules ./node_modules
COPY --chown=node:node --from=build /app/dist ./dist
COPY --chown=node:node swagger ./swagger
COPY --chown=node:node templates ./templates
EXPOSE ${APP_PORT}
CMD [ "node", "dist/main.js" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment