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

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 Nginxlatest imagefrom the Docker hub on your Docker host.

sudo docker pull nginx
Ho to Run Nginx in a Docker Container: A Step by Step Guide (1)

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)
Ho to Run Nginx in a Docker Container: A Step by Step Guide (2)
  • -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.

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

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
Ho to Run Nginx in a Docker Container: A Step by Step Guide (4)

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

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

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

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 willuse the bind mounting feature in Docker. When you use a bind mount, a file or directory on thehost machineis 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)
Ho to Run Nginx in a Docker Container: A Step by Step Guide (5)

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.

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

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.

Ho to Run Nginx in a Docker Container: A Step by Step Guide (2024)
Top Articles
Latest Posts
Article information

Author: Msgr. Benton Quitzon

Last Updated:

Views: 5640

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Msgr. Benton Quitzon

Birthday: 2001-08-13

Address: 96487 Kris Cliff, Teresiafurt, WI 95201

Phone: +9418513585781

Job: Senior Designer

Hobby: Calligraphy, Rowing, Vacation, Geocaching, Web surfing, Electronics, Electronics

Introduction: My name is Msgr. Benton Quitzon, I am a comfortable, charming, thankful, happy, adventurous, handsome, precious person who loves writing and wants to share my knowledge and understanding with you.