Skip to content

Instantly share code, notes, and snippets.

@malcam
Forked from nzvtrk/Dockerfile
Created May 4, 2022 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malcam/6b02c20c7a03292234df6e3917b8a567 to your computer and use it in GitHub Desktop.
Save malcam/6b02c20c7a03292234df6e3917b8a567 to your computer and use it in GitHub Desktop.
Nest.js multi-stage build dockerfile + docker-compose with postgres & migrations
version: "3"
services:
server:
container_name: myapp-server
image: my-docker-registry/server-image
env_file:
- .env
depends_on:
- postgres
links:
- postgres
ports:
- 3000:3000
# separate service for run migrations while deploy
migration:
image: my-docker-registry/server-image
env_file:
- .env
command: './wait-for-it.sh myappname-postgres:5432 -- npm run migrations:up:prod'
depends_on:
- postgres
postgres:
# container name using like a host in other services
container_name: myappname-postgres
image: postgres:11
restart: always
# volume with database data
volumes:
- ./volumes/postgres-prod:/var/lib/postgresql/data
env_file:
- .env
ports:
- 5432:5432
FROM node:12.14.1-alpine AS build
# If you have troubles with node-gyp use should install these dependencies
RUN apk add g++ make python
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . ./
# build js & remove devDependencies from node_modules
RUN npm run build && npm prune --production
FROM node:12.14.1-alpine
ENV PORT=3000
ENV NODE_ENV=production
WORKDIR /app
COPY --from=build /app/dist /app/dist
COPY --from=build /app/node_modules /app/node_modules
# Migrations compiled while npm run build was call
RUN rm -rf /app/dist/migrations/*.d.ts /app/dist/migrations/*.map
COPY --from=build /app/package.json /app/package.json
COPY --from=build /app/scripts/wait-for-it.sh /app/wait-for-it.sh
RUN chmod +x wait-for.sh
EXPOSE 3000
ENTRYPOINT [ "node" ]
CMD [ "dist/main.js" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment