Dockerizing NestJS application
This article will briefly describe how to make a Docker image from your NestJS application.

1. Generating NestJS app
I recommend you to use powerful nest-cli to generate the skeleton for our app:
$ nest new nestjs-docker
...
? Which package manager would you ❤️ to use?
> npm
2. Test run from source code (optional)
Let’s run our app from the source code:
$ npm start$ curl http://localhost:3000/
Hello World!
3. Creating Dockerfile
We will use a multi-stage Docker build in order to make our production image concise. Create the following Dockerfile in the root of the project directory:
4. Building the image
$ docker build -t "nestjs-docker:0.0.1" .
...
naming to docker.io/library/nestjs-docker:0.0.1
5. Test run from Docker image
Next, let’s run our application as a container from the image created in the previous step:
$ docker run --rm -p 3000:3000 nestjs-docker:0.0.1
...
LOG [NestApplication] Nest application successfully started
What options have we specified?
--rm
— Automatically remove the container when it exits. Basically, to avoid polluting your host machine.-p 3000:3000
— Publishes a container’s port to the host. Forwards port 3000 from your host machine (network) to your container. If you would like to use another port on your host, e.g. 5000, use5000:3000
.
Let’s send a test request to our app:
$ curl http://localhost:3000/
Hello World!
And that’s it! All that’s left is to publish your image to some repository and use it in the production environment, e.g. Kubernetes.
Find the complete source code at this repo.
Happy coding!
Also, you might like my other stories: