I use Traefik to access docker containers on localhost with same port 80.
containous/traefik:experimental-v2.0
is used for
Traefik setup:
version: '3.7'
services:
traefik:
image: containous/traefik:experimental-v2.0
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
labels:
- "traefik.enable=true"
- "traefik.docker.network=web"
- "traefik.http.routers.traefik.rule=Host(`traefik.localhost`)"
- "traefik.http.routers.traefik.entrypoints=web"
networks:
- "web"
- "default"
networks:
web:
external: true
Container 1 setup:
version: '3.7'
services:
container-1:
image: "containous/whoami"
labels:
- "traefik.enable=true"
- "traefik.docker.network=web"
- "traefik.http.routers.whoami.rule=Host(`container-1.localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
networks:
- "web"
Container 2 setup:
version: '3.7'
services:
container-2:
image: "containous/whoami"
labels:
- "traefik.enable=true"
- "traefik.docker.network=web"
- "traefik.http.routers.whoami.rule=Host(`container-2.localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
networks:
- "web"
/etc/hosts
(it is planned to use defreitas/dns-proxy-server
in future):
[...]
127.0.0.1 container-1.localhost container-2.localhost
When just one container (container 1) is UP, it can be nicely reached via container-1.localhost
, it is also listed as UP in Traefik.
{
"routers":{
"traefik@docker":{
"entryPoints":[
"web"
],
"service":"traefik-traefik",
"rule":"Host(`traefik.localhost`)",
"status":"enabled",
"using":[
"web"
]
},
"whoami@docker":{
"entryPoints":[
"web"
],
"service":"container-1-test1",
"rule":"Host(`container-1.localhost`)",
"status":"enabled",
"using":[
"web"
]
}
},
"services":{
"container-1-test1@docker":{
"loadBalancer":{
"servers":[
{
"url":"http://172.18.0.3:80"
}
],
"passHostHeader":true
},
"status":"enabled",
"usedBy":[
"whoami@docker"
],
"serverStatus":{
"http://172.18.0.3:80":"UP"
}
},
"traefik-traefik@docker":{
"loadBalancer":{
"servers":[
{
"url":"http://172.18.0.2:80"
}
],
"passHostHeader":true
},
"status":"enabled",
"usedBy":[
"traefik@docker"
],
"serverStatus":{
"http://172.18.0.2:80":"UP"
}
}
}
}
When two containers (container 1 + container 2) are UP, neither can be reached anymore, their routers are gone (only the route for Traefik itself is still there). Their services are both listed though.
Both containers are shown as DOWN by Traefik:
{
"routers":{
"traefik@docker":{
"entryPoints":[
"web"
],
"service":"traefik-traefik",
"rule":"Host(`traefik.localhost`)",
"status":"enabled",
"using":[
"web"
]
}
},
"services":{
"container-1-test1@docker":{
"loadBalancer":{
"servers":[
{
"url":"http://172.18.0.3:80"
}
],
"passHostHeader":true
},
"status":"enabled"
},
"container-2-test2@docker":{
"loadBalancer":{
"servers":[
{
"url":"http://172.18.0.4:80"
}
],
"passHostHeader":true
},
"status":"enabled"
},
"traefik-traefik@docker":{
"loadBalancer":{
"servers":[
{
"url":"http://172.18.0.2:80"
}
],
"passHostHeader":true
},
"status":"enabled",
"usedBy":[
"traefik@docker"
],
"serverStatus":{
"http://172.18.0.2:80":"UP"
}
}
}
}
container-1.localhost
doesn't work anymore: 404 page not found
and also
container-2.localhost
doesn't work anymore: 404 page not found
.
When I take the other container down, so that only one container is still up, e.g. container 1 as in the beginning, it is shown as UP in Traefik again, its router is also listed and it is reachable via container-1.localhost
again.
Traefik is very useful for to me because I can have multiple sites in development and reach them all by a convenient name instead juggling around with changing ports on localhost or other tricks. I can also test them over HTTPS because I can use a trusted test-CA for self-signing certs in the dev environment.