Skip to content

Instantly share code, notes, and snippets.

@syntaqx
Last active August 2, 2022 20:38
Show Gist options
  • Save syntaqx/921d6cd0d389d3bfa9798ffa809cca46 to your computer and use it in GitHub Desktop.
Save syntaqx/921d6cd0d389d3bfa9798ffa809cca46 to your computer and use it in GitHub Desktop.
Node 18 Optimized Docker
services:
app:
build:
context: .
target: development
command: npm run start:dev
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
environment:
NODE_ENV: "${NODE_ENV:-development}"
DATABASE_DSN: "${DATABASE_DSN:-postgres://postgres@postgres:5432/app}"
ports:
- '3000'
postgres:
image: postgres
environment:
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: app
volumes:
- postgres-data:/var/lib/postgresql/data
ports:
- '5432'
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
volumes:
postgres-data:
FROM node:18 AS development
RUN apt-get update && apt-get install -y openssl
WORKDIR /usr/src/app
COPY --chown=node:node package.json package-lock.json ./
RUN npm ci
COPY --chown=node:node . .
RUN npm run build --if-present
USER node
CMD [ "npm", "start" ]
FROM node:18-alpine AS build
WORKDIR /usr/src/app
COPY --chown=node:node package*.json ./
COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules
COPY --chown=node:node . .
RUN npm run build --if-present
ENV NODE_ENV production
# Running `npm ci` removes the existing node_modules directory.
# Passing in --only=production ensures only production dependencies are
# installed, optimizing node_modules for production environments.
RUN npm ci --only=production && npm cache clean --force
USER node
FROM node:18-alpine AS production
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist
CMD [ "node", "dist/main.js" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment