Skip to content

Instantly share code, notes, and snippets.

@malcam
Forked from nzvtrk/Dockerfile
Created May 4, 2022
Embed
What would you like to do?
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