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

  • Ubuntu 16.04 or derivative (Kubuntu, Mint…)

How it works?

When browser requests anything that ends with .loc, dnsmasq will resolve that to 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.


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
    3. Edit /etc/dhcp/dhclient.confAfter
      #supersede domain-name "";

      add the following line:

      prepend domain-name-servers;
    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:
      ExecStart=/usr/bin/docker daemon -H fd:// --bip= --dns
    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:

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

After that run docker-compose up -d.

Now let’s create another project in different folder:

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

  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.