Dockerizing a Nest js application and deploying to Dockerhub (without docker-compose)

#docker #nest #dockerhub

Hi Peeps, Today we will look at how to dockerize a node application and deploy it to docker hub. We will be using this project for demo purposes.

1. Clone application

Navigate to your preferred folder and clone the application

cd Desktop 
git clone https://github.com/manulangat1/nest-docker-jenkins.git

2. Create your docker file.

Once you have cloned your project, on the root of the project create a file Dockerfile.
A Dockerfile is a text document that contains all the commands that a user would call on the command line for an image to be built.

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

3. Build our docker image.

We are now going to build our image using docker

docker build -t nest-docker:nd-1.0 .

The -t commands tells docker to tag our image with the name nest-docker and the . context tells it to look for the Dockerfile at the root of the project.

4. Run the docker image

We will now run our docker image

docker run -p 3000:3000 --name nest-service  nest-docker:nd-1.0

-p binds our port to the port we had earlier exposed and --name gives a name to the current container that we are running.

5. Deploying to docker hub.

Head over to Dockerhub and create an account if you do not have one. Once you log in, create a repository and we are set to deploy our docker image to docker hub.

Run the following command to log in to dockerhub.

docker login

To push our docker image to docker hub, run the following command.

docker push nest-docker:nd-1.0

You have now built and deployed a dockerized nest application to dockerhub.

Follow me for more articles on docker, Jenkins and DevOps in general.