How to Run Nginx webserver in a Docker container on Ubuntu 16.04/18.04 LTS

Introduction

Nginx is an open-source high-performance reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server. Normally we need to install and manage Nginx from the package manager or built it from the source. But In this tutorial we will run Nginx in docker container and serve basic webpage on obuntu 16.04.

Before we Begin

  • Ubuntu 16.04/18.04 desktop/server with docker installed and running
  • Root user or user with sudo privileges

If you are not installed docker in your system ,follow our previous tutorial on Install Docker on Ubuntu .

Pull nginx docker image

In order to get Nginx official docker image from the DockerHub, first search for the image from the terminal as:

docker search nginx

will get out put as :

We can see the first image name “nginx and Description shows Official build of Nginx.This is the image we are going to use.You can also search by visiting DockerHub and search for Nginx you can see the same image details over there

If you further click on Nginx will see the image details clearly as below

You can see this image is already downloaded over 1 billion times.

Now pull this image into our system by running

docker pull nginx 

If you want  to put specific tags for the image  you can search on view available Tags link.Other wise our docker image wil be downloaded  with default tag latest

Will get output as :

Using default tag: latest
latest: Pulling from library/nginx
8559a31e96f4: Already exists
1cf27aa8120b: Pull complete
67d252a8c1e1: Pull complete
9c2b660fcff6: Pull complete
4584011f2cd1: Pull complete
Digest: sha256:a93c8a0b0974c967aebe868a186e5c205f4d3bcb5423a56559f2f9599074bbcd
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

This downloads 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.

Run nginx as docker container from the image

Now let’s start our Nginx Docker container with this command:

docker run --name docker-nginx -p 8000:80 -d nginx
  • run is the command to create a new container from the docker image
  • The --name the flag is how we specify the name of the container (if we left blank this Docker will assign a name to the container specifies the port we are exposing in the format of -p yourlocal-machine-port:internal-container-port. In this case, we are mapping Port 80 in the container to Port 8000 on the local machine. You can give any port which is open in your system. For security purposes, it is best practice to avoid the default port flag to run this container in the background. So the container will run in detached mode. If we do not mention it on docker command our container will be stopped when we press CTRL+C to get back our shell session
  • nginx is the name of the image on DockerHub (we downloaded this before with the pull command, but Docker will do this automatically if the image is missing)

Now check the status of our newly created nginx container

docker ps

Here we can see that our container named docker-nginx is running on port 8000 of local machine and its status is shows Up.

Now check our nginx webserver is running by typing ip of server with port 8000 on web browser.For this tutorial my local machine is ubuntu desktop 18.04.So I am typing localhost:8000

We are getting nginx welcome page means our webserver running successfully.

Now we have a running instance of Nginx in a detached container!.But we cannot edit any config file of container and the container has no access to our website files.So we must configure docker container so that will get more control and manage over our webserver.

Stop the container and remove before we modify our container to server our custom webpage.

docker stop docker-nginx 

Now that the container is stopped (you can check with sudo docker ps -a to confirm)

Now remove the conatiner by running :

docker rm docker-nginx 

Run nginx container with custom webpage

In this step we will create a custom index page for our website.So our nginx container will serve content from local machine .

Let us create a directory for our website content on home directory by running below commands

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

Now create a sample HTML page as below using any editor

nano index.html

<html>
  <head>
    <title>Docker nginx Tutorial</title>
  </head>
  <body>
    <div class="container">
      <h1>Hello Nginx container</h1>
      <p>This nginx page is brought to you by Docker</p>
    </div>
  </body>
</html>

After entering the above contents save the file and close.

We’ve now got a simple index page to replace the default Nginx landing page.

How to link container to access our local filesystem on server

In this section we will start our nginx webserver so its accessible on the internet on port 80 and will connect it to our custom website content on the server.

Docker allows us to link directories from our virtual machine’s (here our local machine ) local file system to our containers.

In our case, since we want to serve our web pages, we need to give our container the files in our local folder.

We have two options here:

  • We could copy the files into the container as part of a Dockerfile
  • copy our contents into the container

 But both of these methods leave our website in a static state inside the container.

 By using Docker’s data volumes feature, we can create a symbolic link between our local machines filesystem and the container’s filesystem. This allows us to edit our existing web page files and add new ones into the directory and our container will automatically access them.

If you want to read more about Docker and volumes check out the Docker data volume documentation

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.

Making link to access our local machine content for nginx docker container:

To do this, we use the -v flag to map a folder from our local machine (~/docker-nginx/html) to a relative path in the container (/usr/share/nginx/html).

We can do this by running command :

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

We can see that the new addition to the command from our previous commands -v ~/docker-nginx/html:/usr/share/nginx/html is our volume link.

  • -v specifies that we’re linking a volume
  • the part to the left of the : is the location of our file/directory on our local machine/server(~/docker-nginx/html)
  • the part to the right of the : is the location that we are linking to in our container (/usr/share/nginx/html)

Now our nginx container is running and check our new webapage from the browser.

Yes.Now your website is working properly.You can upload more content to the ~/docker-nginx/html/ directory, and it will be added to your live website.

For example, if we modify our index file, and if we reload our browser window, we will be able to see it update in realtime.

Conclusion

You now learned to create a running Nginx container serving a custom web page. We recommend learning more about Docker linking and docker volume concepts in detail

Leave a Reply