Dockerizing a nest app and deploying to Dockerhub (with Docker compose)
Published: 2022-08-28 14:59:45

---
title: Dockerizing a nest app and deploying to Dockerhub (with Docker compose)
published: true
date: 2022-07-03 20:42:08 UTC
tags: 
canonical_url: 
---

Hello peeps,

In this article, we will go through how to deploy a nest app to dockerhub. The prerequisites needed are a dockerhub account and having docker and docker compose locally installed. We will be using this [project](https://github.com/manulangat1/nest-bookmark-api) as our starting point

#### 1. Clone the application.

Navigate into your desired folder and clone the app using the following commands

```
cd Desktop 
git clone https://github.com/manulangat1/nest-bookmark-api

```

Once done, navigate into the cloned project

### 2. Adding a docker file.

A docker file is a text document that contains all the commands that will be used to build a docker image. At the root of your project, create a new file Dockerfile

```
touch Dockerfile

```

We can now start adding our commands.

```
FROM node:13-alphine # this is the base image that our docker image will be based on
WORKDIR /usr/src/app # this sets the working directory on our docker container 
COPY package*.json . #this command copies the package.json and package-lock.json from our host to the docker container 
RUN npm install # this installs all the packages defined in our package.json file. 
EXPOSE 3000 # exposes the port on our container 
COPY . . #copies everything from our host to our docker container
CMD ["npm" , "start:dev"] # this is the entry point of our application

```

### 3. Setting up our docker-compose file.

At the root of the project, create a new file called `docker-compose.yml`

```
touch docker-compose.yml

```

We can now proceed to add our docker-compose commands. Note that we will not spin up and database for this part, a more detailed post on docker-compose is in the works.

```
services:'3' # this tells the version of docker-compose that we are using. 
  dev-api: #this is the name of our container 
      build: . This tells docker-compose to look for the Dockerfile at the root (path where Dockerfile lives)
      environment:
      ports: # this binds the port on our local machine to our docker container
          - "3000:3000"  
      volumes: # creating a volume enables us to have a way of persisting our data. More about this soon. (we are using a named volume here)
          - db-data:/var/lib/postgresql/data
      restart: always # tells the behaviour of our application once the docker container fails

```

### 4. Fire up our docker container.

Moment of truth, we are going to fire up our container using the command:

```
 docker-compose up dev-api -d

```

`-d` tells the container to run in a detached mode.

To confirm whether our container is running, use the command below:

```
docker ps

```

and your output should be in the form of : ![Screenshot from 2022-07-03 23-31-55.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1656880352238/-ZBKdpkT3.png)

To check the logs of the container run

```
docker logs $container_id

```

To enter into our docker container use the following command.

```
docker exec -it $container_id /bin/sh

```

###. 5 Deploy to dockerhub.

To deploy this image to docker hub, we have to run the following commands.

```
docker build -t node-nest:ndj-1.0 
docker login 
docker push node-nest:ndj-1.0

```

We have successfully dockerized and deployed our application to Dockerhub.

Follow for more articles on Docker, Jenkins, AWS and DevOps in general.