Docker NGINX

Docker NGINX Tutorial – We shall learn to run NGINX in a Docker Container on Ubuntu. Following is a step by step guide to dockerize NGINX :

1. Install Docker Engine

Docker is the prerequisite.

Follow the tutorial, Install Docker on Ubuntu, to install docker on your computer with Ubuntu.

ADVERTISEMENT

2. Pull NGINX from Docker Hub

Run the following command to pull NGINX from Docker Hub to Docker Host.

$ sudo docker pull nginx
arjun@arjun-VPCEH26EN:~$ sudo docker pull nginx
[sudo] password for arjun: 
Using default tag: latest
latest: Pulling from library/nginx
afeb2bfd31c0: Pull complete 
7ff5d10493db: Pull complete 
d2562f1ae1d0: Pull complete 
Digest: sha256:aa1c5b5f864508ef5ad472c45c8d3b6ba34e5c0fb34aaea24acf4b0cee33187e
Status: Downloaded newer image for nginx:latest

3. Run Docker with nginx

Run the following command as root user.

$ docker run --name docker-nginx -p 80:80 -d nginx

–name docker-nginx : Name given to the container that is run is docker-nginx-p 80:80 : the port we are exposing and mapping from local machine port number to that of container, in the format local_machine_port:container_port-d : Detached mode – Runs the container in background

root@arjun-VPCEH26EN:/home/arjun# docker run --name docker-nginx -p 80:80 -d nginx
26676407c03db63a74b7ccf17796d034bcd2ffd909a568585f3047e2d52d091c

Now the NGINX is running in its default configuration. If you open a browser and hit the url0.0.0.0:80, you would get following welcome page from NGINX Web Server.

Docker NGINX

4. Include a static Web Application in the Docker with NGINX

To include our static Web Application into the Docker Image with NGINX, we shall create a Dockerfile (including commands to build image) and an html file with name index.html (acting as our web application) in a directory named nginx-app.

FROM nginx
COPY . /usr/share/nginx/html
<html>
  <head>
    <title>Docker NGINX Tutorial</title>
  </head>
  <body>
	<h1>NGINX Tutorial - Brought to you by TutorialKart</h1>
	<p>Learn to Dockerize with NGINX and your web application.</p>
	<a href="https://www.tutorialkart.com/docker/docker-tutorial/">Docker Tutorial</a>
	<a href="https://www.tutorialkart.com/nginx/nginx-tutorial/">NGINX Tutorial</a>
  </body>
</html>

Run the following command in Terminal from directory – nginx-app, to create image file.

$ docker build -t nginx-application .
root@arjun-VPCEH26EN:/home/arjun/workspace/docker/nginx-app# docker build -t nginx-application .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM nginx
 ---> da5939581ac8
Step 2/2 : COPY . /usr/share/nginx/html
 ---> ae2c0a239687
Removing intermediate container f90e669aafe9
Successfully built ae2c0a239687
Successfully tagged nginx-application:latest

Image file is created successfully.

root@arjun-VPCEH26EN:/home/arjun/workspace/docker/nginx-app# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx-application   latest              ae2c0a239687        2 minutes ago       108MB
nginx               latest              da5939581ac8        7 days ago          108MB
python              latest              26acbad26a2c        7 days ago          690MB
java                8                   d23bdf5b1b1b        8 months ago        643MB
hello-world         latest              c54a2cc56cbb        14 months ago       1.85kB

Run the following command in Terminal from directory, to run the image file in a container.

$ docker run --name docker-nginx -p 80:80 -d nginx-application

nginx-application – Docker Image file name.

root@arjun-VPCEH26EN:/home/arjun/workspace/docker/nginx-app# docker run --name docker-nginx -p 80:80 -d nginx-application
1a2ddec91961b76b73e19af28db06e658afcbe2a44e3ae01205d55f9d910922f

The container docker-nginx is running.

root@arjun-VPCEH26EN:/home/arjun/workspace/docker/nginx-app# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
1a2ddec91961        nginx-application   "nginx -g 'daemon ..."   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp   docker-nginx

Hit the url0.0.0.0:80/index.html in any of your browser.

Docker NGINX Application

Conclusion

In this NGINX Tutorial – Docker NGINX, we have learnt to deploy NGINX with Docker