Docker + Nginx


Date: 2016-06-24

This is a repository for hosting a configurable docker + nginx container running on CentOS 7. I use this container for hosting my blog with the sphinx-bootstrap container:

Docker Hub Image: jayjohnson/nginx


I built this container so I could have an extensible nginx container that could utilize a mounted volume for static files and ssl certs. By default this container will start by installing the necessary configuration files, install a robots.txt file, and then start a backgrounded nginx process.

Environment Variables

Here are the available environment variables that are used by the script as the container starts up. By using environment variables to drive one-time install/configuration behaviors this container can be used with docker compose for other technologies that I want to use with nginx. I wrote this blog post if you want to learn more.

Variable Name Purpose Default Value
ENV_BASE_NGINX_CONFIG Provide a path to a base nginx.conf /root/containerfiles/base_nginx.conf
ENV_DERIVED_NGINX_CONFIG Provide a path to a derived nginx.conf /root/containerfiles/derived_nginx.conf
ENV_DEFAULT_ROOT_VOLUME Path to shared volume for static html, js, css, images, and assets /usr/share/nginx/html

Getting Started

By default this container exposes ports: 80 and 443 from the container to the host OS. For development purposes, I usually have this container using host ports 82 and 444 to prevent collisions with existing apache or nginx services.


To build the container you can run that automatically sources the file:

$ ./
Building new Docker image(
Sending build context to Docker daemon 37.38 kB
Step 1 : FROM centos:7
 ---> 904d6c400333


---> 8bfb9d8ca828
Successfully built 8bfb9d8ca828

Here is the full the command:

docker build --rm -t <your name>/nginx --build-arg --build-arg maintainer=<your name> --build-arg imagename=nginx .

Start the Container

To start the container run:

$ ./
Starting new Docker image(

Looking into the you can see that there are quite a few defaults taken from the file:

$ cat

source ./ .

echo "Starting new Docker image($registry/$maintainer/$imagename)"
docker run --name=$imagename \
            -p 82:80 \
            -p 444:443 \
            -d $maintainer/$imagename

exit 0

Test the Container

To test the container is working try wget-ting the index.html file and confirm there is a returned 200 response status code:

$ wget http://localhost:82/
--2016-06-24 13:29:38--  http://localhost:82/
Resolving localhost (localhost)...
Connecting to localhost (localhost)||:82... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13534 (13K) [text/html]
Saving to: ‘index.html’

index.html               100%[====================================>]  13.22K  --.-KB/s   in 0s

2016-06-24 13:29:38 (264 MB/s) - ‘index.html’ saved [13534/13534]


The example above is using port 82 for development and documentation purposes instead of port 80

Stop the Container

To stop the container run:

$ ./
Stopping Docker image(

Or run the command:

$ docker stop nginx


This repository is licensed under the MIT License.

The nginx license:

Thanks for reading,


Want to learn more?