Deployment of Web application using Docker

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

Containerization is a sort of virtualization wherein every one of the parts of an application are packaged into a one container image and can be run in different and isolated user space on the same device or the same operating system. These containers are lightweight, convenient, and are very easy to deploy on the client side as well.

Docker is an open source containerization platform. It provides the ability to run applications in an isolated environment known as a container.

Containers work in basically the same manner to extremely light virtual machines and can be run straightforwardly on the part of our host operating system. Thus, we can run various containers at the same time .

Overview :

One important reason using docker containerization is that before deployment of the application one can easily test these application inside docker containers and the same container can be deployed for production thus the developers need not to worry about the configuration differences between the testing environment and the production environment.

In this article at OpenGenus, we will be performing the deployment of an already made application using docker hub. Steps involved are:

  1. Set up docker registry and repository.
  2. Initiate Docker build to create your Docker Image.
  3. Push image in docker hub.

What is Docker Hub and why is it used?

Docker hub is a cloud repository that is used to build, test and deploy container images of applications. It is open source and allows a user to access other public repositories and import images from them to use and also it can also be used to make our own private repositories.

Docker hub is known for its flexiblity as if one needs a custom made software or application we can create our own images and save them in our own private repository.

Docker Desktop

Docker Desktop is an application in Windows , mac-os and linux which can be used for operations such as building and sharing the containerized applications. It is used to avoid the use command line interface (CLI)(which some people find difficult to use) by providing a simple user interface that improves the convenience of using docker.

Deploy a Web application on Docker

  1. At first set up a registry and make repository at Docker hub(While a container repository is a collection of related container images used to manage, pull and push images, a container registry is a collection of repositories made to store container images.)
    To make a repository:
    a. Sign up or sign-in on docker hub by making a user account.
    b. Click on the Create Repository button.
    c. Write a repository name and then click on the create button.

  2. To build a docker image docker file is necessary , A Docker file contains all the commands needed to create your Docker Image . By reading the instructions from a Docker file, a text file containing all the commands required to build a specific image, Docker generates a image automatically.

INSTRUCTION arguments

Example of Docker File

# syntax=docker/dockerfile:1
FROM ubuntu:18.04
COPY . /app
RUN make /app
CMD python /app/app.py

A Docker file is executed sequentially by Docker. A FROM command must appear first in a Docker file. This could also come after comments, global scoped ARGs, and parser directives.

  1. Build a Docker image

But what is a docker image?:
An image is a file that depicts a bundled application along with every dependency required for the application to function properly. In other words, we could say that a Java class and a Docker image are similar. Layers are used to create images. The layers are put together one on top of the other. Docker image is used to define the template or construct of the container whereas the container actually starts the application as it contains all the dependencies in it.

Container needs to run an image to exist so to make an image. A Docker image is a file used to execute code in a Docker container. Docker images act as a set of instructions to build a Docker container, like a template. Docker images also act as the starting point when using Docker.

To build an image on docker

docker build [OPTIONS] PATH | URL | -

Example:

docker build -t myfirstapp

The command docker build is used to make docker images from a docker file and a context. The context of the build is located at a specific path or URL. The files in the context can be referred using the build process.The URL mentioned can be of any resources such as complete text file or Git repositories.

  1. Push image in the docker hub.
    To push the image in the docker hub first login into your docker hub account using the command
docker login -u YOUR-USER-NAME

Use the docker tag command to give the image a new name.

docker tag repo_name YOUR-USER-NAME/ repo_name

Now push the image using the command

docker push YOUR-USER-NAME/repo_name : tag_name

Now your image has been successfully deployed on docker hub and can be pulled and run on a new instance.

Conclusion

Here it is important to note that this the most generalised method for deployment of an application and there are various other methods also, according to the requirement and platform for deployment.

Using Docker Deployment time can be cut to seconds . It can establish a container for each process and even does not boot an OS, which is why. Data can therefore be created as well as deleted, even if we ignore the expense of bringing it up again, which would be larger than what is reasonable. This article focused on deployment of web applications by pushing them into docker hub using docker registry and repository created by you.

References

For exploring more about docker file you can visit here
For information about docker images you can refer at this website
More on docker build command

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.