3 min read

Nest.js | Dockerfile

Nest.js | Dockerfile

Below is an example of a Dockerfile for a Nest.js framework application. This assumes you have a basic understanding of Docker and its concepts.

!!!WE URGE TO STOP DOING THIS!!!

# Use a base image with Node.js installed
FROM node:latest

# Create and set the working directory inside the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the container
COPY package*.json ./

# Install application dependencies from package-lock
RUN npm install

# Copy the application source code to the container
COPY . .

# Expose the port that the application runs on (change as needed)
EXPOSE 3000

# Command to start the Nest.js application
CMD ["npm", "run", "start:dev"]
  1. FROM node:latest: This line specifies the base image for the container, using the latest Node.js image available from the official Docker Hub repository.
  2. WORKDIR /usr/src/app: Sets the working directory within the container where the application code will be placed.
  3. COPY package*.json ./: Copies the package.json and package-lock.json files to the container. This allows Docker to take advantage of layer caching for faster builds by only rebuilding the necessary layers when dependencies change.
  4. RUN npm install: Installs the application dependencies defined in the package.json file within the container.
  5. COPY . .: Copies the entire application source code to the container. This assumes that your Nest.js application code is in the same directory as the Dockerfile.
  6. EXPOSE 3000: Informs Docker that the application running inside the container will be accessible on port 3000. Modify this according to your Nest.js application's specified port.
  7. CMD ["npm", "run", "start:dev"]: Defines the command to start the Nest.js application. Modify this command as necessary based on how you start your Nest.js application (e.g., npm run start, npm run start:dev, etc.).

To build your Docker image, navigate to the directory containing this Dockerfile and run:

docker build -t your-app-name .

Replace your-app-name with the name you want to give to your Docker image. After the build, you can run a container based on this image using docker run with appropriate port mapping and additional configurations as needed.

Result:

IMAGE ID      SIZE
452c9f7218ae  598MB

Multi-stage builds are useful to anyone who has struggled to optimize Dockerfiles while keeping them easy to read and maintain.

!!!DO THIS!!!

# ---- Base Node ----
FROM node:20.8.1-bullseye-slim AS base

WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./

RUN npm install

COPY . .

# ---- Build ----
FROM base AS build

RUN npm run build

# ---- Production ----
FROM node:20.8.1-bullseye-slim AS production

WORKDIR /usr/src/app

COPY --from=build /usr/src/app/dist ./dist

COPY package*.json ./

RUN npm ci --only=production

CMD ["node", "dist/main"]
  1. Base Node (base):
    • Sets up a base stage with Node.js and defines a working directory.
    • Copies the package.json and package-lock.json files to install app dependencies.
    • Copies the application source code into the container.
  2. Build (build):
    • Inherits from the base stage.
    • Executes the build process for the application. For a Nest.js app, this might involve compiling TypeScript code into JavaScript (or any other build tasks required).
  3. Production (production):
    • Uses a smaller image (node:20.8.1-bullseye-slim) to reduce the final image size.
    • Sets the working directory and copies only the necessary files for production (the built output from the previous stage).
    • Installs only production dependencies using npm install --only=production.
    • Specifies the command to start the application in production mode (node dist/main assuming the main entry point file is main.js in the dist directory after building).

To build your Docker image, navigate to the directory containing this Dockerfile and run:

docker build -t your-app-name .

Result:

IMAGE ID      SIZE
732d8ffb3f3b  261MB

That's not finally solution, but it's a good start point for your project.

Please check our story about choosing Docker image for Node.js:

Selecting A Node.js Image for Docker
Docker builds images automatically by reading the instructions from a Dockerfile - a text file that contains all commands, in order, needed…