Nginx Webserver with Docker

This guide documents how to run an nginx server in Docker to statically serve files out of a directory on the host.

Firewalls

Important: make sure you’ve disabled networking firewalls on your host machine, using setenforce 0 and systemctl stop firewalld. This isn’t the most secure but neither is running an HTTP webserver. I guess you could configure specific rules/policies to make things more secure but for this example we’ll just be letting everything through.

Host Directory

In this case, we’ve got all the files in /mnt/images. This can be any directory on the host machine, but make sure you’ve got the files with read permissions set:

chmod 0755 -R /mnt/images/*

Nginx Configuration

Create a directory to store the custom nginx config file and Dockerfile. I called mine /root/nginx.

Create a new default.conf nginx config file:

server {
        listen       80;
        server_name  localhost sp06.hpc.amslabs.hpecorp.net;
        root   /usr/share/nginx/html;

        location / {
                autoindex on;
        }
}

We’ll be replacing the existing default.conf in the nginx image.

Create a Dockerfile:

FROM nginx:latest AS base

RUN mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
COPY ./default.conf /etc/nginx/conf.d/default.conf

ENTRYPOINT ["nginx", "-g", "daemon off;"]

Running

Go ahead and build and deploy your webserver, volume-mounting /mnt/images to nginx’s default host location of /usr/share/nginx/html. Also map port 8080 on the host to port 80 in the container, where your HTTP server will be running.

docker build -t nginx_webserver .
docker run --rm -d --name webserver -v /mnt/images/:/usr/share/nginx/html -p 8080:80 nginx_webserver

You should now have a running nginx container:

sp06 ~/nginx » docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS        PORTS                                   NAMES
31a1862da3bc   nginx_webserver   "nginx -g 'daemon of…"   2 seconds ago   Up 1 second   0.0.0.0:8080->80/tcp, :::8080->80/tcp   webserver

Now, navigate to your machine’s hostname, in this case it’s http://sp06.hpc.amslabs.hpecorp.net:8080/, and you should see your files listed and available for download.