Last active
June 6, 2024 08:56
-
-
Save nzvtrk/cba2970b1df9091b520811e521d9bd44 to your computer and use it in GitHub Desktop.
Nest.js multi-stage build dockerfile + docker-compose with postgres & migrations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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