Dockerizing NestJS
Dockerizing NestJS
To run an existing Nest project on Docker, you need to create a Docker image.
First, you need to install Docker. On Linux-based systems, installing the Docker Engine includes Docker.
If you are on Windows or Mac, you can install Docker Desktop to easily manage Docker in a GUI environment.
After installing Docker, navigate to the root folder of the project you want to dockerize and create the Docker configuration files.
DockerFile: Configuration file for the Docker image.dockerignore: List of files to exclude from the image
In the .dockerignore file, exclude git-related files, environment variable files, node_modules, and the Dockerfile itself.
I configured my Dockerfile as follows:
FROM node:20-alpine
RUN mkdir -p /var/app
WORKDIR /var/app
COPY . .
RUN npm install
RUN npm run build
EXPOSE 3000
CMD ["npm","start"]To briefly explain the process:
- Use the node-20-alpine (minimized version).
- Create a folder named /var/app and copy the contents of the current root folder into it.
- Then run npm install to install the specified dependencies and build the project.
- Set the port that the Docker container will use to 3000.
- Start the server.
Now, let's build the Docker image.
docker build -t (image-name) .Once the build is complete, you can check the Docker images stored locally with the following command:
docker imagesTo run this Docker image in a container, you can execute the following command.
(Here, we connected port 3000 of the host to port 3000 of the container.)
docker run -p 3000:3000 (docker-image-ID)There are various other commands available, so feel free to apply them as needed.