Press enter to see results or esc to cancel.

How to: Docker, Dnsmasq and nginx-proxy on Ubuntu 16.04

I like the dynamics of working on multiple project. However, there’s a caveat. One has to manage multiple development environments on one computer. Docker comes to the rescue with it’s ability to isolate different environments. There’s a lot literature about it on the internet. I hope you will find this tutorial useful as well.

We will set up a system where we can access many of our projects at the same time just by accessing a different address in the browser. To achieve this, we will use

  • https://github.com/jwilder/nginx-proxy
  • http://www.thekelleys.org.uk/dnsmasq/doc.html
  • Ubuntu 16.04 or derivative (Kubuntu, Mint…)

How it works?

When browser requests anything that ends with .loc, dnsmasq will resolve that to 127.0.0.1 which is your local computer. Browser will send HTTP request to nginx-proxy which is listening on port 80 of your computer and is automatically configured with several virtual hosts. Based on domain name it forwards request to either App1 or App2.

nginx-proxy-stack

How to prepare your system?

    1. Install dnsmasq by running the following command:
      sudo apt-get install dnsmasq
    2. Edit configuration in /etc/dnsmasq.conf. Add the following lines
      address=/.loc/127.0.0.1
      interface=docker0
      interface=lo
    3. Edit /etc/dhcp/dhclient.confAfter
      #supersede domain-name "fugue.com home.vix.com";

      add the following line:

      prepend domain-name-servers 127.0.0.1;
    4.  Restart dnsmasq by running
      sudo service dnsmasq restart
    5. Update firewall settings so that Docker containers can access dnsmasq
      sudo ufw allow in on docker0 to any port 53
    6. Update Docker settings by creating a file called /etc/systemd/system/docker.service.d/01-default-network.conf . Create necessary directories if they don’t exist. This should be the contents of 01-default-network.conf:
      [Service]
      ExecStart=
      ExecStart=/usr/bin/docker daemon -H fd:// --bip=172.17.42.1/16 --dns 172.17.42.1
    7. Restart Docker
      sudo service docker restart
    8. Setup nginx-proxy by running
      docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy --restart always

      This command will ensure nginx-proxy is always running and is started also when you reboot your system.

Usage example

Let’s create a docker-compose.yml file in some folder:

dokuwiki:
 image: istepanov/dokuwiki:2.0
 environment:
 - VIRTUAL_HOST=dokuwiki.loc
 ports:
 - 80

After that run docker-compose up -d.

Now let’s create another project in different folder:

wekan:
  image: mquandalle/wekan
  links:
    - wekandb
  environment:
    - MONGO_URL=mongodb://wekandb/wekan
    - ROOT_URL=http://wekan.loc
    - MAIL_URL=smtp://user:[email protected]:25/
    - [email protected]
    - VIRTUAL_HOST=wekan.loc
  ports:
    - 80

wekandb:
  image: mongo

Again, run docker-compose up -d from this folder. Visiting http://dokuwiki.loc and http://wekan.loc will now take you to each of the applications.

Screenshot_20160714_174416