Docker + Nginx
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.
Here are the available environment variables that are used by the start_container.sh 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|
By default this container exposes ports:
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
build.sh that automatically sources the properties.sh file:
$ ./build.sh Building new Docker image(docker.io/jayjohnson/nginx) 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 registry=docker.io --build-arg maintainer=<your name> --build-arg imagename=nginx .
Start the Container
To start the container run:
$ ./start.sh Starting new Docker image(docker.io/jayjohnson/nginx) ad836abf4f30eb629d501b4c1cf5b9709001293dab4e3e6b886639bd00ab4d33 $
$ cat start.sh #!/bin/bash source ./properties.sh . echo "Starting new Docker image($registry/$maintainer/$imagename)" docker run --name=$imagename \ -e ENV_BASE_NGINX_CONFIG=$ENV_BASE_NGINX_CONFIG \ -e ENV_DERIVED_NGINX_CONFIG=$ENV_DERIVED_NGINX_CONFIG \ -e ENV_DEFAULT_ROOT_VOLUME=$ENV_DEFAULT_ROOT_VOLUME \ -v $EXT_MOUNTED_VOLUME:$INT_MOUNTED_VOLUME \ -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)... 127.0.0.1 Connecting to localhost (localhost)|127.0.0.1|: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:
$ ./stop.sh Stopping Docker image(docker.io/jayjohnson/nginx) nginx $
Or run the command:
$ docker stop nginx
This repository is licensed under the MIT License.
The nginx license: http://nginx.org/LICENSE