How To Run Nginx in a Docker Container on Ubuntu 16.04.

Khaled Naser

Khaled Naser

Nginx is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server (origin server).

Docker containers enable developers to focus their efforts on application “content” by separating applications from the constraints of infrastructure. Dockerized applications are instantly portable to any infrastructure–laptop, bare-metal server, VM, or cloud–making them modular components that can be readily assembled and re-assembled into fully featured distributed applications and continuously innovated on in real time. In practice, this means we can take one application or group of applications to wrap them in a container to make them modular, portable, compose-able, and lightweight. This portability means you can install the Docker Engine on a wide variety of operating systems, and any functional container written by anyone will run on it.

Docker containers are a popular form of a relatively old operations practice, that is containerization. Containerization differs from virtualization in that virtualization abstracts away the hardware, while containerization abstracts away the base operating system, too.

In this article we will serve a basic web page, so we can focus on configuring Nginx with a Docker container.


Login to your Ubuntu 16.04 LTS server using your sudo credentials or the best practice is to login using SSH-key . Docker requires a 64-bit installation regardless of your Ubuntu version. Additionally, your kernel must be 3.10 at minimum. The latest 3.10 minor version or a newer maintained version are also acceptable.

Once you got access to your server, run the command below to check your kernel version, to make sure the kernel is at least 3.10 or above.

ubuntu@ubuntu-16:~$ uname -r

Docker 1.11.2 relies on some fairly recent kernel features, so we using the right kernel version recommended for Docker 1.11.2 . But it’s fairly good to update your system by flowing the command below before moving to the next step.

$ sudo apt-get -y update

Installing Docker on Ubuntu 16.04:

Docker hosts a startup script to get Docker up and running on your machine. We can install it simply by running the command below.

$ sudo curl -sSL https://get.docker.com/ | sh

It will take a while and upon successful installation, you’ll see the installed version and some instructions for running as non-root/without sudo as shown below.

docker install

Now verify that ‘docker’ is installed correctly on your system by using below command.

$ sudo docker run hello-world

docker verify

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message. Then, it exists.

As we have installed the Docker Client as part of our Docker installation, so we can have access to the command line tool that allows us to interact with our containers. Now we will see that how we can run a basic container and then remove it by using below commands.

$ sudo docker ps -a

After execution of above command, you will get some basic information about the container. Now when we run this container again with the container then you will see that the container has run recently as shown below.

$ sudo docker start angry_bassi

By default, Docker containers run their assigned commands and then exit. But some containers will be set up to run through a list of tasks and finish, while others will run indefinitely.

To remove the hello-world image, as we won’t be needing it again run below command containing your own docker name.

$ sudo docker rm angry_bassi

docker basics

Downloading Nginx Docker Image:

In this section, we’ll download the Nginx Docker image and show you how to run the container so it’s publicly accessible as a web server. By default, containers are not accessible from the Internet, so we need to map the container’s internal port to the Droplet’s port.

First, run the below command to get the Nginx image.

$ sudo docker pull nginx

This will download all the necessary components for the container. Docker will cache these, so when we run the container we don’t need to download the container image(s) each time.

Let’s start Nginx Docker container using below command.

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

That’s all we need to get Nginx up! Paste the IP address of your Ubuntu Droplet’s into a web browser and you should see Nginx’s “Welcome to nginx!” page. Then hit the break shortcut CTRL+C to get back to our shell session.

In order to verify, run below command to check the status of Nginx container has exited.

$ sudo docker ps -a

docker nginx

Running Docker Nginx in Detached Mode:

First, remove the old Nginx container and then create a new, detached Nginx container by using below commands.

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

Whereas the -d is being used to run this container in the background.

Now you will its status as Up upon execution of below command.

$ sudo docker ps

If you open your server’s IP address again in your browser, we will be able to see the “Welcome to nginx!” page again.

Now first stop the docker-nginx container and then remove it using commands shown below to move to the next step.

$ sudo docker stop docker-nginx
$ sudo docker rm docker-nginx

nginx detached mode

Building a Web Page to Serve on Nginx:

Now we’ll get to the final version of our container, with a quick stop to generate a custom website file. This setup allows us to have persistent website content that’s hosted outside of the (transient) container.

Create a new directory for your website content within the home directory, and change your directory to it, by running the commands shown below.

$ mkdir -p ~/docker-nginx/html
cd ~/docker-nginx/html

Now let’s create an HTML file using any of your command line editor and put some text on your index page.

$ vim index.html

Save the file using :wq! you will get a simple index page to replace the default Nginx landing page.

Now we’ll start our Nginx container so it’s accessible on the Internet over Port 80, and we’ll connect it to our website content on the server. Since we want to serve web pages, so we need to give our container the files to render.

The Nginx container is set up by default to look for an index page at /usr/share/nginx/html, so in our new Docker container, we need to give it access to our files at that location. Let’s use the -v flag to map a folder from your local machine ~/docker-nginx/html to a relative path in the container that is /usr/share/nginx/html

This can be accomplished by using the following command in your ssh terminal.

$ sudo docker run --name docker-nginx -p 80:80 -d -v ~/docker-nginx/html:/usr/share/nginx/html nginx

Now if you open your web browser followed by the IP address or hostname of your Ubuntu 16.04 server, you will your index page text.


You can upload more content to the ~/docker-nginx/html/ directory, and it will be added to your live website. We can build a whole site out of flat HTML files this way if we wanted to. For example, if we added an about.html page, we could access it at http://your_server_ip/about.html without needing to interact with the container.


Congratulations, we have successfully setup Nginx container serving a custom web page. NGINX, and Docker work extremely well together. Whether you use the open source NGINX image from the Docker Hub repository or create your own NGINX image, you can easily spin up new instances of NGINX in Docker containers.

Would you like to know about Zuul, a CI/CD project gating tool? Download our white paper and get reading!

How to up your DevOps game with Project Gating

Share on Social Media:


Cluster API driver for OpenStack Magnum

Khaled Naser

Public Cloud

9000 MTUs (jumbo frames) in all Public Cloud Regions

Khaled Naser


OpenInfra Summit Berlin 2022 VEXXHOST Recap

Khaled Naser

Go to Top