How to Dockerize nginx: 5 steps

How to Dockerize Nginx or How to run Nginx web server in a container or How to containerize Nginx
What does Dockerize an application mean? It simply means to create a container of your application that you can deploy on an Operating system. And what exactly is a container - you can consider a "Container" to be a packaged software which contains all its dependencies wrapped into this package, so that it can run in an Operating system. This container is a complete package that includes everything, such as code, system libraries, configuration etc. Hence this container can be easily transferred or moved to any other environment. If your container or the containerized application has been tested to work in one environment, say Development environment, now this container is guaranteed to work and behave the same way when moved to another new environment, say a QA or Test environment. In practice, one creates an image for a particular containerized application, and this image can be transferred or moved to any other environment which is running the same Operating System version.

Since this container or the containerized application runs in its own isolated user process, one can run multiple such containers. Hence you could run several instances of your application (nginx as an example application) on the same Operating System. Compare this with the Virtual Machine (VM) approach- it would require a different Virtual Machine for each instance of nginx or your application. 

This post provides five easy steps to create a container or rather Docker container. In this example we are creating an nginx application container, but this could be applied to creating any other application container. The container technology used is the Docker container which means you need a Docker engine to be installed on your Operating System. Docker is now available for both Linux and Windows. You can follow these below five steps to create your application container.
With a dockerized Nginx you will be able to run nginx web server in its own environment (similar to having nginx on a Virtual Machine). Hence one can run several versions of nginx now on the same host Operating system. This is possible because each dockerized nginx is now running in an isolated container, which sees its own instance of the Operating system.

Prerequisite before you begin: The only prerequisite is that Docker or rather "Docker engine" should already be installed on your host. Check this link to get docker up and running on your linux host. Once you have docker ready you can follow these five steps to get nginx container ready.


Step 1    Get nginx image to your host 
Step 2    Check image is available on your host
Step 3    Create a container from this image
Step 4    Check container is available on your host
Step 5    Access nginx homepage

Pre-requisite before you follow the 5 steps: 
Ensure you have docker installed on your host. Run the command docker ps -a, it should give you an output as shown below. The output below shows that docker service is enabled and showing you the current containers (currently there are no containers yet, that's why there are no rows showing available containers. Once you have nginx container installed it will show up as one of the rows- as shown in the screenshot of Step 4)


docker ps -a


Assuming you have Docker installed on your host, proceed with the following 5 steps to get nginx as a container on your host.

Step 1.  Get nginx image to your host
sudo docker pull nginx    
(This command will get the nginx image from the docker repo to your host)


Step 2.  Check if the image is now available on your local host
sudo docker images
This command lists the nginx image that has been download from the docker repository.

REPOSITORY           TAG             IMAGE ID           CREATED         SIZE
docker.io/nginx        latest     9e988ed19567     38 hours ago      108.5 MB


Step 3.  Create a container from the above image
sudo docker run --name My_nginx -p 8080:80 nginx
Note: This command does not return the prompt

This command will create a new container, the "run" causes creation of new container, "--name" provides the name of the container, here we are providing it name as My_nginx,  "-p" provides the port that local machine and the enclosed container ports (ie map port 8080 of localhost with port 80 of the nginx server), and from image named "nginx"
Note: After the above command was entered by pressing the Enter key, the prompt was not returned. In fact, after accessing the default nginx homepage, you can see output of access attempt from a client web browser (see Step 5(a))

Step 4.   Check container is available on your host
sudo docker ps -a 
This command checks if your container called as "My_nginx" is available
output of  docker ps -a




Here is another screenshot (similar to above), showing the nginx container is available now








At this point, after the above four steps, your nginx container is now ready to be tested. In fact, Step 5 is actually testing your application or nginx in our example is up and running

Step 5.  Access nginx default home page

Check nginx host is running on port 8080 (http://your_Host_IP:8080)
This completes the objective of installing nginx as a container on your host or in other words, dockerize nginx. Now you can access nginx web pages just like any other nginx server.
Also back to the docker command (in Step 1) which ran the container, you can see the access attempt for the nginx homepage made from a browser.
Step 5(a): Check access to nginx homepage from browser





 This completes all the five steps to run nginx as a container.


Below are few other commands and steps to stop the container.
---------------------------------------------------------------------------------
$ sudo docker stop My_nginx
My_nginx 



This will stop the container. Or you could press ^Ctrl C to get back to the command prompt (refer screenshot in Step 5(a)), that would also stop the container.

$ sudo docker start My_nginx
This will again start our container My_nginx
Or alternatively you could use below restart command as well

$ sudo docker restart My_nginx
docker-nginx     (output on console after above command is run)

Above command will restart the nginx container. 
You can check with the below ps -a command also access the default nginx web page


If you again try this command (while the current container/nginx is running), you will be shown an error because the container already exists.   $ sudo docker run --name My_nginx -p 8080:80 nginx
/usr/bin/docker-current: Error response from daemon: Conflict. The name "/My_nginx" is already in use by container ba7ce68c9ab1e7353e57f71b312658014490509bdad10f4a4be546e7fc7e4814. You have to remove (or rename) that container to be able to reuse that name..
See '/usr/bin/docker-current run --help'.




Comments

  1. Thanks for sharing this wonderful content.its very useful to us.It was very interesting and meaningful.I gained many unknown information, the way you have clearly explained is really fantastic.keep posting such useful information.
    oracle training in chennai

    oracle training institute in chennai

    oracle training in bangalore

    oracle training in hyderabad

    oracle training

    hadoop training in chennai

    hadoop training in bangalore

    ReplyDelete

Post a Comment

Popular posts from this blog

VMware fix for Invalid manifest and ova file import failed errors

SOAPUI - import certificate

Centrally Managed Users (CMU) - New Feature in Oracle Database 18c