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 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.
How to prepare your system?
- Install dnsmasq by running the following command:
sudo apt-get install dnsmasq
- Edit configuration in /etc/dnsmasq.conf. Add the following lines
address=/.loc/127.0.0.1 interface=docker0 interface=lo
- 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;
- Restart dnsmasq by running
sudo service dnsmasq restart
- Update firewall settings so that Docker containers can access dnsmasq
sudo ufw allow in on docker0 to any port 53
- 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
- Restart Docker
sudo service docker restart
- 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.
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.