Search The Hostwinds Guides Knowledge Base

Use Ngnix to Proxy Docker Containers on Ubuntu

Share This Article [TheChamp-Sharing]

Installing Docker

If you’ve heard about Docker, you may know about its ability to help you build applications in a sealed environment. This is helpful, as it eliminates any changes that may occur between your developer machine and your production machine, such as a VPS server from Hostwinds. 

How To Install Docker

Install docker using this command:

# apt-get install docker

Container technology is very helpful in sealing an environment, but when you need to configure things like SSL or other applications, this can introduce additional complexity.

Installing Nginx

Nginx (Pronounced “Engine-ex”) is a highly configurable web server. This makes it great for things setting up what’s called a “reverse proxy”, which simply means that this web server will take in requests, and forward them to our Docker containers. add this into your Hostwinds VPS with:

# apt-get install nginx

Setting up a simple Apache Container

For this, We’re going to use the official Apache image on Docker hub, it’s documentation can be found here.

As the documentation mentions, we will need to create a DockerFile.

Using a text editor, such as nano, (nano apache.docker) write a file with these contents:

FROM httpd:2.4
COPY ./index.html /usr/local/apache2/htdocs/

Now create a file called index.html, and insert this into its contents:

<h1> Hello World! </h1>

It is possible to use any static page or even a directory in place of the index file. For simplicity, a single HTML file is fine. Once the file is written, run these commands to build the docker file.

# docker build -t apache-demo .

# docker run -dit --name demo-web -p 8080:80 apache-demo

Visiting your server at [yourdomain:8080] or [your IP]:8080 Should result in seeing this page in your web browser

Configuring Nginx as a reverse proxy

With the docker container set up to serve your files, we can set up Nginx. This way, we can mostly leave the configuration of the container alone and silo it off behind Nginx. Edit /etc/nginx/nginx.conf and add in this code:

server {
                server_name [YOUR.DOMAIN.NAME];
                listen 80;
                location / {
                        proxy_set_header Host $host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

From here, NGINX takes in all the public requests, and the Apache container just serves your files.

Related Articles: