In this tutorial, you will learn how to run Nginx as a Docker container, and expose it to your local network. In addition to, 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, and more. 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.
Docker is a containerization platform, used to package up your application into one easily manageable container image.
You have to fulfill the following requirements to complete this tutorial:
- Docker should be installed and locally be running on your system.
- 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. On your docker host, run the following Docker pull command on the terminal to download the Nginx latest image from the Docker hub.
sudo docker pull nginx
Using default tag: latest latest: Pulling from library/nginx 69692152171a: Pull complete ... Digest: sha256:6d75c99af15565a301e48297fa2d121e15d80ad526f8369c526324f0f7ccb750 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest
Run the Nginx Docker container
Now, 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:
docker run -d -p 80:80 --name my-nginx-server nginx
-d– Start a container in detached mode (container is running in the backround).
-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 to visit http://localhost and you should see a Nginx web server offering its default web page.
There it is. You have a working instance of NGINX, via Docker container.
List Docker Containers
To list all running Docker containers, execute the following command:
sudo docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 462f2f2a8445 nginx "docker-entrypoint.…" 2 minutes ago Up 12 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp my-nginx-server
Based on the above output, we can stop our Nginx Docker container with either of the following two:
docker stop 462f2f2a8445 docker stop my-nginx-server
To see all containers, even those that are not running, you need to add the
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 to outside using Nginx Docker container. This setup allows us to have persistent website content that’s hosted outside of the container.
In order to achieve this functionality, we are going to 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.
Now let’s create a simple HTML file and put some text on your index page.
<html> <head> <title>Nginx Docker</title> </head> <body> <h1>My static page.</h1> </body> </html>
Copy the above snippet and paste it inside. Save the file and exit Vim.
Next, we will run the Nginx Docker container with 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 nginx
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
/ directory from inside the Nginx Docker container to the
~/www directory on the host machine.
Docker uses a
: to split the host’s path from the container path. The host path always comes first.
Now, if you browse the http://localhost, you will get the below-given output on the displaying window.
That’s all about it. You now have a running Nginx Docker container serving a custom web page.
Nginx and Docker work very well together. In this tutorial we demonstrated how to set up and use the Nginx Docker container. Now, you also know how to share information between a container and the host file system.
In case of queries, please leave your comments.