Easy nginx deployment with Docker

I’ve been looking for an easy way to run multiple websites. I was used to manage virtualhosts with Apache or nginx in the latest years.

However I was wondering how Docker could deal with it. Reading this article about how to proxy nginx with Docker has given me a new approch that is worth investigating.

Usage

Here’s how it goes:

  1. run the nginx-proxy docker image,
  2. run each docker containers you want to be proxied, passing an extra argument that is VIRTUAL_HOST=…

I recommend the official nginx:alpine variant to run your sites container.

Example

1. Run the docker-proxy. Nothing to tune here, just copy-paste:

docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

2. Run your websites each with their own domain (neomad.org and webnomad.org in this example):

cd /path/to/neomad/src
docker run -d -e VIRTUAL_HOST=neomad.org --name neomad -v `pwd`:/usr/share/nginx/html nginx:alpine

cd /path/to/webnomad/src
docker run -d -e VIRTUAL_HOST=webnomad.org — name webnomad -v `pwd`:/usr/share/nginx/html nginx:alpine

Nothing more to run, nginx-proxy is listening to dockers running with the virtual_host argument and forwarding accordingly. Quite convenient!

Notes

I’ve also (finally) been able to install Docker on a Raspberry Pi following this Hypriot blog article section.