Skip to content

Instantly share code, notes, and snippets.

@teyfix
Last active May 25, 2021 01:36
Show Gist options
  • Save teyfix/cfb6923be8663217bf353a5b38570610 to your computer and use it in GitHub Desktop.
Save teyfix/cfb6923be8663217bf353a5b38570610 to your computer and use it in GitHub Desktop.
Docker setup for NestJS applications
# just adding the .env.local file for
# my local development purposes and
# I do not really need to specify
# every single file to be ignored
# because I am not copying everything
# with "COPY . ." command
# you can see what I mean below
# in "Dockerfile"
.env.local*
PORT=8080
version: "3.7"
services:
app:
build:
# referencing to ./Dockerfile
context: .
volumes:
# mirroring only my local ./src directory
# into the container's so every change
# I have made locally will be applied
# inside the container too
- ./src:/app/src
ports:
# exposing node.js debugging port
# so I can debug the application with
# my IDE inside container
- 9229:9229
# exposing http port so I can access the
# application outside of the container
- ${PORT}:${PORT}
command: npm run start:dev
# fetching LTS node.js image
FROM node:lts-alpine
# setting cwd to /app
# we will mirror the project structure to
# the /app directory inside container
WORKDIR /app
# copying package files before npm install
# this part should be seperated to help
# docker to cache "npm i" command
COPY package*.json ./
# no need to explain i guess
RUN npm i
# copying the rest of the sources
# that is required by the application
# instead of this, you can simply run:
# COPY . .
# but, I just do not want to fill either
# the container and the .dockerignore
# with redundant stuff
COPY src ./src
COPY .env* ./
COPY nest-cli.json ./
COPY tsconfig*.json ./
# finally running build before
# starting the application
RUN npm run build
# again, no need to explain
CMD ['npm', 'start']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment