Skip to content

Instantly share code, notes, and snippets.

@RafaRochaS91
Created February 3, 2020 21:09
Show Gist options
  • Save RafaRochaS91/781de1cb1c6805837ad13852381e73d4 to your computer and use it in GitHub Desktop.
Save RafaRochaS91/781de1cb1c6805837ad13852381e73d4 to your computer and use it in GitHub Desktop.
Dockerizing a NestJS + MongoDB service

To dockerize and run this service

Requirements:

  • docker;
  • docker-compose;
  • bash/zsh/fish based terminal.
# script alias for docker-compose up
$ npm run init

version: "3"
services:
app:
container_name: app
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
FROM node:latest
# RafaRochaS91
RUN mkdir -p /usr/src/app
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# wildcard for both package.json and package-lock.json
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
EXPOSE 3000
# If running for production uncomment
# CMD [ "npm", "run","init" ]
CMD [ "npm", "run","start:dev" ]
{
...
"scripts": {
"init": "docker-compose up",
"prestart:prod": "rimraf dist && npm run build",
"start:prod": "node dist/",
"start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" "
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment