Ho to Run Nginx in a Docker Container: A Step by Step Guide

In this tutorial, you'll learn how to run Nginx as a Docker container and expose it to your local network.

In addition, you will learn how to create a Docker data volume to share information between a container and the host file system.

Nginx is a popular open-source software used for web serving, reverse proxying, caching, load balancing, etc. It is quite popular and used on many high-traffic websites today.

One of the most common workloads of Docker is using it to containerize web servers. Weโ€™ll show you how to set it up with Nginx. So, let us walk you through the process.

Related: How to Install and Configure Nginx Web Server

Docker is a containerization platform that packages up your application into one easily manageable container image.

Prerequisites

You have to fulfill the following requirements to complete this tutorial:

  1. Docker should be installed and locally be running on your system.
  2. You need a root account, or a user can run sudo commands.

Setting up Nginx Inside Docker

Pulling the Image

First, we are going to pull the official Nginx image. Run the following Docker pull command on the terminal to download the Nginx latest image from the Docker hub on your Docker host.

sudo docker pull nginx
Pullinx Nginx Docker Image

Run the Nginx Docker Container

We are ready  to run the Nginx Docker container and expose its port to your local network. To do this, we run the image with the command:

sudo docker run -d -p 80:80 --name my-nginx-server nginxCode language: CSS (css)
Run the Nginx Docker Container
  • -d – Start a container in detached mode (container is running in the background).
  • -p – Bind a port from container to host (routes host traffic port 80 to container port 80).
  • -name – The name of our Docker container.

The last argument, nginx, tells Docker which image to use for the container.

Now open a browser and point it to the address of the host on which you ran your Nginx container. In my case, it’s http://192.168.122.101. The Nginx web server default page should greet you.

Nginx Default Welcome Page

There it is. You have a working instance of the Nginx Docker container.

List Docker Containers

To list all running Docker containers, execute the following command:

sudo docker container ls
Nginx Docker Container

Based on the above output, we can stop our Nginx Docker container with either of the following two:

sudo docker stop 19411f8b3f35
sudo docker stop my-nginx-server

To see all containers, even those not running, you need to add the -a flag.

sudo docker container ls -a

Sharing Data Between the Nginx Docker Container and the Host

Docker containers are ephemeral. By default, any data created inside the container is only available from within the container and only while the container is running.

So, let us show you how to make data from inside the container accessible on the host machine. Our target is to create a simple HTML file, host it inside a container and serve it outside using the Nginx Docker container.

This setup allows us to have persistent website content hosted outside the container.

To achieve this functionality, we will use the bind mounting feature in Docker. When you use a bind mount, a file or directory on the host machine is mounted into a container.

So, let’s first create a new directory for your website content within the home directory.

mkdir ~/www

Now letโ€™s create a simple HTML file and put some text on your index page.

vim ~/www/index.htmlCode language: JavaScript (javascript)
<html>
<head>
<title>Nginx Docker</title>
</head>
<body>
<h1>My static page.</h1>
</body>
</html>Code language: HTML, XML (xml)

Please copy the above snippet and paste it inside. Then, save the file and exit Vim.

Next, we will run the Nginx Docker container with the attached volume on the container /usr/share/nginx/html to the present on the host www directory where the index.html file is saved.

docker run -d -p 80:80 -v ~/www:/usr/share/nginx/html/ --name my-nginx-server nginxCode language: JavaScript (javascript)
Nginx Docker Container with Persistend Data

The Nginx container is set up by default to look for an index page at /usr/share/nginx/html/. In the command above, the -v option sets up a bind mount volume that links the /usr/share/nginx/html/ directory from inside the Nginx Docker container to the ~/www directory on the host machine.

Docker uses a colons symbol (:) to split the hostโ€™s path from the container path. Remember, the host path always comes first.

Again, if you browse http://192.168.122.101, you will get the below-given output in the browser window.

Nginx Docker Container with Volume Mount

That’s all about it. You now have a running Nginx Docker container serving a custom web page.

Conclusion

Nginx and Docker work very well together. This tutorial demonstrated how to set up and use the Nginx Docker container. You also know how to share information between a container and the host file system.

In case of queries, please leave your comments.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%