This repository is to demonstrate how to use the external
network specification in docker-compose
to enable communication between containers defined in different compositions. It also uses the nice auto-discover capabilities of traefik respond to all services that require it without having to touch anything in this directory.
Traefik forward traffic to services based on their hostnames. Since it listens to a local address, there must be a name resolution for the browser to reach the traefik proxy and the various backends. This can be done in two ways:
- change your
/etc/hosts
file so that the desired name is resolved to localhost:This is the only option if you don't have access to the DNS for the domain you intend to use. If you have dnsmasq installed you can probably assign full subdomains like127.0.0.1 www.example.com
*.dev.local
tolocalhost
(not tested in person at least recently). - redirect all your domain (or a subdomain) to localhost in the dns of your domain. A line like the following will redirect any request in the subdomain
dev
to localhost. For example, if your domain wereexample.com
, thenmywebapp.dev.example.com
would be resolved to localhost.*.dev 1800 IN A 127.0.0.1
For local domains like mywebapp.local
, the only choice is to auto-generate the certificates. The good thing is that browsers are more relaxed regarding validity of the certificate in this case. Still the easiest option is to use mkcert automatically as explained below.
For global domains, we cannot use the great feature of traefik of generating the acme certificates from Let's encrypt because the dev machine is not reacheable from the internet. However, there are still two viable options that do not require generating a certificate for each service:
- Official wildcard certificates: generate a certificate for your
dev
subdomain and store it in a subdirectory of yourCRTDIR
. I do this using the procedure described here which uses Let's encrypt and works nicely (and free of charge) for domains registered with gandi.net. The list of subdomains that you want traefik to be aware of have to be listed in theDOMAINS
environment variable. So, if the domain isjkldsa.com
, anything likemyapp.dev.jkldsa.com
will point to localhost and reach traefik. Since the certificate is valid forr all hosts in the*.dev.jkldsa.com
, traefik will not have to generate a new one and you will avoid complains from the browser. - Use mkcert to generate cerificates on the fly for the sub-domains you intend to use. In this case, all you have to do is to list the domains in the
MKCERT_DOMAINS
environment variable.
For this to work, you need to have
- docker installed and running or, if you have a zen attidude, you can also try with podman.
- have any verion of ruby's erb preprocessor. This is needed to fix some issues coming from rancher desktop on mac (at least my mac). This is probably no longer the case but I got rid of rancher desktop. Therefore, I cannot test myself.
- have access to a directory containing the glob ssl certificates (by default /keybase/team/epfl_idevfsd/certs)
- eventually have the [mkcert][mkcert] utility installed
- copy
env.example
to.env
and edit it to suit your needs:
- provide a directory with a valid wildcard certificate and key as the
CRTDIR
env variable; - provide
DOMAINS
andMKCERT_DOMAINS
environment variables listing the domains you want to use;
- make sure that traefik is running:
make up
; - add labels and network to your app's
docker-compose.yml
file so that it can be added automatically to the list of services. See the example.