This is the fourth part of the series about local development with b5, docker and traefik. If you haven’t read the first three parts, you should do that to get the full context of this article.
How to resolve a custom top level domain to localhost
As a prerequisite, we need to install
dnsmasq to resolve a custom
tld to localhost.
Throughout the whole series, I will expect that you are using a macOS (10.15.3 from the this was written)
We use brew to install dnsmasq:
brew install dnsmasq
Additionally, we have to add a
dnsmasq.conf file with one line of configuration:
echo 'address=/.test/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf
This line configures
dnsmasq to point all requests to domains ending on
Add a custom macOS resolver
We also need to add a resolver to macOS:
sudo mkdir -p /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/test'
dnsmasq automatically start on boot
To add dnsmasq to the auto start we use
sudo brew services start dnsmasq
Reboot macOS that the changes take effect.
Test dnsmasq and the resolver
In order to test this part, we can simply ping a random
.test domain. It should get an answer from
PING foobar.test (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.038 ms
Set up the traefik project
In our setup, traefik is a dedicated
b5 project that we will put it into a
.b5 folder in our home directory. I will not go into details on the project structure because this is explained in detail in (this) article.
mkdir ~/.b5/traefik && cd ~/.b5/traefik
traefik as a
project key and enable the docker module of
In this tutorial, we will use version
1.7 of traefik. The latest version at the time that this article was written is
2.1. I tried for a few hours to figure out a working configuration for the latest version but it was all in vain. As soon as I have a working configuration, I will update this post.
We have a
docker-compose.yml with a single service named
traefik. With the
restart: always configuration, it will automatically start when
docker starts. The service will be attached to the
gateway network, which is defined at the end of the file. Traefik needs the docker socket to mount into the service next to the
traefik.toml, which we create in the next step. We map the hosts port
443 to traefik because it will proxy all requests later. Port
8080 is for the traefik dashboard which gives us information about running services etc..
debug = true
defaultEntryPoints = ["http", "https"]
address = ":80"
address = ":443"
address = ":8080"
domain = "test"
exposedbydefault = false
watch = true
This file contains the complete configuration for traefik to act as a reverse proxy for all our projects in development. The explanations are right in the file.
The Taskfile will only contain three commands which are self explanitory:
docker:docker-compose up "[email protected]"
docker:docker-compose down "[email protected]"
docker:docker-compose "[email protected]"
Now we can start the project with b5. We will demonise it with
b5 run -d
The dashboard should now be accessible on
Configure our example project to use traefik
In the last article of this series, we built an example project with Laravel, phpMyAdmin and MailHog.
This is the current
docker-compose.yml from our example project:
Because we only need to enable traefik for services which need to be accessible from the host system, I have stripped out all services and volumes which are not relevant.
Add traefiks network
Since our traefik does not run inside this project but in its own project with its own namespace, we need to connect these docker networks. This can be achieved by the following configuration at the end of the
default network is as its name says, the network which is created by default in any project. When we add a new network, we also have to list the
default network to be created. Additionally, we add the
traefik_gateway network which is the network that we defined in our traefik project and mark it as external.
Enable traefik for the web and the phpMyAdmin service
To enable traefik for the web service, we have to attach the
traefik_gateway to it and add two labels:
As you can see, we commented out the port mapping because we don't need this anymore. The configuration for both services is absolutely identical.
Enable traefik for MailHog
MailHog needs an additional configuration because it does not run on port
443 by default:
That's all. Now we can test the setup by executing
backends in traefik should now be visible n the
The scheme regarding which url will be resolved is as follows:
This means we can access our three services with the following urls:
dnsmasq and created an independent
traefik service which is able to proxy requests for multiple projects at the same time. Since we do not need the port mappings anymore, we can start multiple projects at the same time without shifting the ports. As last step we configured our example project to use traefik for three different services.
Thanks for reading the article. If you have questions just drop me a line here.