NGINX is a popular lightweight web application that is used for developing server-side applications. It is an open-source web server that is developed to run on a variety of operating systems. Since nginx is a popular web server for development, Docker has ensured that it has support for nginx.
We will now see the various steps for getting the Docker container for nginx up and running.
Step 1 − The first step is to pull the image from Docker Hub. When you log into Docker Hub, you will be able to search and see the image for nginx as shown below. Just type in nginx in the search box and click on the nginx (official) link which comes up in the search results.
Step 2 − You will see that the Docker pull command for nginx in the details of the repository in Docker Hub.
Step 3 − On the Docker Host, use the Docker pull command as shown above to download the latest nginx image from Docker Hub.
Step 4 − Now let’s run the nginx container via the following command.
sudo docker run –p 8080:80 –d nginx
We are exposing the port on the nginx server which is port 80 to the port 8080 on the Docker Host.
Once you run the command, you will get the following output if you browse to the URL http://dockerhost:8080. This shows that the nginx container is up and running.
Step 5 − Let’s look at another example where we can host a simple web page in our ngnix container. In our example, we will create a simple HelloWorld.html file and host it in our nginx container.
Let’s first create an HTML file called HelloWorld.html
Let’s add a simple line of Hello World in the HTML file.
Let’s then run the following Docker command.
sudo docker run –p 8080:80 –v “$PWD”:/usr/share/nginx/html:ro –d nginx
The following points need to be noted about the above command −
We are exposing the port on the nginx server which is port 80 to the port 8080 on the Docker Host.
Next, we are attaching the volume on the container which is /usr/share/nginx/html to our present working directory. This is where our HelloWorld.html file is stored.
Now if we browse to the URL http://dockerhost:8080/HelloWorld.html we will get the following output as expected −