DEV Community

Kipchirchir Langat Emmanuel
Kipchirchir Langat Emmanuel

Posted on • Originally published at blog.kipchirchirlangat.com on

Dockerizing a nest app and deploying to Dockerhub (with Docker compose)

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 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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

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

docker ps

Enter fullscreen mode Exit fullscreen mode

and your output should be in the form of : Screenshot from 2022-07-03 23-31-55.png

To check the logs of the container run

docker logs $container_id

Enter fullscreen mode Exit fullscreen mode

To enter into our docker container use the following command.

docker exec -it $container_id /bin/sh

Enter fullscreen mode Exit fullscreen mode

. 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

Enter fullscreen mode Exit fullscreen mode

We have successfully dockerized and deployed our application to Dockerhub.

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

Top comments (0)