Revolutionize Your Nest.js Development: 4 Steps to Dockerize Your Nest.js App
Tags: node, docker, nestjs, typescript
Published: 2023-10-25 12:04:34

## Introduction

In this tutorial, we will walk you through the process of cloning a "Nest.js demo" project, setting up a development environment, creating a Docker image for your project, and finally pushing the image to Docker Hub. By following these steps, you'll be able to containerize your Nest.js application and share it with others.

---


### Step 1: Clone the Nest TypeScript Starter Project

First, let's get started by cloning the Nest TypeScript Starter project to your local computer. Open your terminal and run the following command:

```bash
git clone https://github.com/nestjs/typescript-starter.git nest-typescript-starter
```

Once the repository is cloned, navigate into the project directory:

```bash
cd nest-typescript-starter
```

Now, let's install the project dependencies and run the application to ensure it's working as expected:

```bash
npm install
npm run start
```

You should see your Nest.js application up and running locally. You can access it in your browser by navigating to `http://localhost:3000`.

---

### Step 2: Create a Docker Image

To containerize your Nest.js application, you'll need to create a Docker image. To get started, make sure you have Docker installed on your machine. Create a file named `Dockerfile` in your project directory. You can use a text editor or run the following command to create it:

```bash
touch Dockerfile
```

Now, open the `Dockerfile` in your text editor and add the following content:

```Dockerfile
# Use the official Node.js 16 image as the base image
FROM node:16

# Set the working directory inside the container
WORKDIR /app

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

# Install project dependencies
RUN npm install

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

# Expose the port your Nest.js application is listening on
EXPOSE 3000

# Command to start your Nest.js application
CMD [ "npm", "run", "start:prod" ]
```

Next, create a `.dockerignore` file in your project directory to specify which files and directories should be ignored when copying files to the Docker image. You can use a text editor or run the following command to create it:

```bash
touch .dockerignore
```

Add the following content to your `.dockerignore` file:

```
node_modules
npm-debug.log
```

---

### Step 3: Create and Test the Docker Image

Before pushing the Docker image to Docker Hub, let's test it locally to ensure everything works as expected. Build the Docker image by running the following command in your project directory:

```bash
docker build -t nest-app .
```

This command builds the Docker image with the tag `nest-app`. After the build is complete, you can run the Docker container using the following command:

```bash
docker run -p 3000:3000 nest-app
```

You should see your Nest.js application running inside a Docker container. You can access it in your browser by navigating to `http://localhost:3000`.

---

### Step 4: Push the Docker Image to Docker Hub

To share your Docker image with others, you can push it to Docker Hub. First, log in to your Docker Hub account using the `docker login` command:

```bash
docker login
```

Enter your Docker Hub username and password when prompted.

Now, tag your Docker image with your Docker Hub username and the repository name you want to use. Replace `your-username` and `your-repo-name` with your actual Docker Hub username and repository name:

```bash
docker tag nest-app your-username/your-repo-name:latest
```

Finally, push the Docker image to Docker Hub:

```bash
docker push your-username/your-repo-name:latest
```

Your Nest.js Docker image is now available on Docker Hub and can be pulled by others using the `docker pull` command.


**Conclusion**
In this tutorial, we've successfully cloned a Nest TypeScript Starter project, created a Docker image for the application, tested it locally, and pushed it to Docker Hub. This allows you to easily share your Nest.js application as a containerized image with others.


**Part 2: [Deploy your application on AWS EC2 servers.](https://dev.to/mrrishimeena/nestjs-docker-aws-ec2-the-ultimate-combo-1ohd)**

---
**Extra:** [NestJS Developers: Upgrade Your Logging with This Simple Guide](https://dev.to/mrrishimeena/nestjs-developers-upgrade-your-logging-with-this-simple-guide-3hp2)