Skip to content

Instantly share code, notes, and snippets.

@syntholly
Last active December 12, 2022 04:08
Show Gist options
  • Save syntholly/9463b1f68cfa8c63c191537856e617ad to your computer and use it in GitHub Desktop.
Save syntholly/9463b1f68cfa8c63c191537856e617ad to your computer and use it in GitHub Desktop.
Docker for Node.js
version: '3.8'
services:
# Development version of the app
cra-test-dev:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- '3000:3000'
volumes:
- '/usr/src/app/node_modules'
- '.:/usr/src/app'
environment:
- CHOKIDAR_USEPOLLING=true
- WATCHPACK_POLLING=true
# Production version of the app
cra-test-prod:
build:
context: .
dockerfile: Dockerfile.prod
ports:
- '3000:3000'
# Use the node:18 image as the base for the Docker image
FROM node:18
LABEL name="app-dev"
# Set the working directory for the app
WORKDIR /usr/src/app
# Copy the entire project directory to the Docker image
COPY . .
# Update packages to latest minor versions
RUN npx npm-check-updates --target minor -u
# Install the app's dependencies
RUN npm install
# Expose port 3000 so it can be accessed from the host
EXPOSE 3000
# Define a healthcheck for the app
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:3000/health || exit 1
# Start the app
CMD ["npm", "start"]
# Use the node:18 image as the base for the Docker image
FROM node:18
LABEL name="app-prod"
# Set the working directory for the app
WORKDIR /usr/src/app
# Copy the entire project directory to the Docker image
COPY . .
# Update packages to latest minor versions
RUN npx npm-check-updates --target minor -u
# Install the app's dependencies
RUN npm ci --only=production
# Expose port 3000 so it can be accessed from the host
EXPOSE 3000
# Define a healthcheck for the app
HEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:3000/health || exit 1
# Start the app
CMD ["npm", "build"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment