How to Dockerize an application and push it to dockerhub


Hey guys in this post I will explain you how to Dockerize your application, and also push it to the dockerhub. In the recent years, docker has managed to grow pretty well and now a lot of people and organizations are getting inclined to it. The main reason for its ever growing popularity is the environment docker containers offer. Inside a docker container all the dependencies of your app can be made available and these dependencies will be independent of the host environment on which you are running the container. And the best thing is you can easily share these containers with others and they will be able to run it in their own environment without any compatibility issues.

Just think of the scenario that you made an application which is having many dependencies say git, npm etc. and in order to build this in your development environment without using docker, you must have installed these dependencies on your machine. Now you build your app and want to share this with somebody else then your application won't be compatible with the other person's machine unless that person also installs all the dependencies. This indeed was a big headache for everybody.

The above-mentioned workload can easily be avoided if we choose docker. Docker will take care of all the required dependencies and will provide us with an independent environment so that once build your application can be shared and run on the different environments.

Docker has their own registry called docker hub where you can push your container images and commits to make any changes and from there other people can pull it.

Before we can start with this tutorial let me give you a slight idea about docker images and how it is different from the Docker containers. So as we discussed above docker container is something which gives us an isolated environment on which we can install all the dependencies required by our application. And Docker image is where those containers are contained. So basically in order to run a container first thing that you need to do is pull the appropriate image. Which will provide you with the required container with an environment suitable for your application.

You can have some images with some dependencies pre-installed on them. Like if you want to develop a nodejs application then instead of using an ubuntu image and installing node yourself in that container you can use a nodejs image itself which will give you a nodejs environment for your application.

Now let's get started on dockerizing our application. I assume you have docker installed in your machine if not then first you need to install docker here is a quick link which might help you in installing docker. For this tutorial, I will be using a simple node application which will just show some welcome message on the screen. You can clone the sample project from here.

As our sample project is using nodejs so we will be using a nodejs images. So first step is to pull that image. There are a lot of nodejs images available but it is advisable to use the official images.
So you can use this command to pull that image

docker pull library/node

This will pull the node image in your local.
Now next step is adding a Dockerfile in our sample project directory where we will add all the instructions which docker will parse in order to run our application in the container.
Now let me explain you this.
FROM basically means which image you want to use and as we pulled node image so we mentioned node:latest here latest is the tag of the image.
EXPOSE as in our sample application we are using port 3000 to give the output so in Dockerfile we need to mention which port we will be using.
ADD  as in order to run our application inside the container we need to transfer our application inside the continder so ADD . /app will basically copy everything from the root directory to the container /app folder.
ENTRYPOINT here we mention the command that we want to execute in order to run our application.

So now as our Dockerfile is ready we need to build an image with our Dockerfile.

docker build -t node:latest . 



As you can see docker build one by one executed all the instructions from the Dockerfile and build an image with our application in it.
So now next step is running the image

docker run -it node:latest





So now as you can see your application is running. But one thing to be cautious is that this wont show up in your machines 3000 port as this will be running on your containers 3000 port so you can access the application in two ways now either by using containers IP address and 3000 port or you can map containers 3000 port to your machines 3000 port.

You can easily find out the containers IP address by inspecting the container you can use this command

docker inspect <container-name/container-id>

And then you can use container-ip:3000 to access the application.
For the second way i.e. mapping you can use this command

docker run -it -p 3000:3000 node:latest

Here we are mapping our machines port 3000 to the containers port 3000 so now we can access the application on our machine 3000 (localhost:3000).

So this was all how you can dockerize your application. We can summarize the steps as:

  1. Pull the images 
  2. Create Dockerfile
  3. Build the image with Dockerfile
  4. Run the image

Now in order to distribute this images with your application you need to push it to some registries.
Docker hub is one of the registry where you can push this images. In order to push the image first you need to create an account on docker hub. After you have created your account you can push the image to your docker hub by following these commands:

docker tag node:latest riturajcse/node:latest

As we were using an official image library/node so we can't push there so in order to push it to our registry we need to tag it with our docker username so tag command will do this. This will create a new copy of the image with the name you suggested.

docker push riturajcse/node:latest




Now the above command will push the image to your registry.  You can verify this by logging into your registry and look for the node repo.

So now anybody can use this image (if access is given or public image) by pulling this image from your registry by using this command. And running the image.

docker pull riturajcse/node:latest 
docker run -it riturajcse/node:latest

Docker pull is optional while running the image if image is not present docker automatically pulls the image for you and then runs it.

So this was all how you can dockerize your application and push it to docker hub. If you have any queries feel free to comment below.




Comments

Post a Comment

Popular posts from this blog

All about NIT Agartala

How to integrate a payment gateway into your WordPress website for donations without using woocommerce

Implementing a Self Organizing List in Go