I have a NAS where I am running various web apps in docker containers through docker-compose. I want some of these web apps to be accessible through the internet, not only when I am connected to my home network.
The problem I’m currently facing is that while cloudflare is able to expose the default web apps (default NAS management 192.168.1.135:80
can be mapped to subdomain.domain.com
, for instance), it is unable to expose any docker container I try to run (192.168.1.135:4444
cannot be mapped to subdomain2.domain.com
), and I receive a 502 bad gateway error with every app I have tried so far.
The configuration shouldn’t be the issue, and it’s definitely not the NoTLSVerify
flag because the apps run on HTTP and I have configured it that way, so I am out of options to know what is going on and how to solve it.
2
Answers
Turns out the problem is due to how docker works with networks, not with how Cloudflare accesses them. I first had to create a network that connected both containers, since adding cloudflare to my docker-compose file didn't work for some reason.
docker network create tunnel
docker run -d --name cloudflare cloudflare/cloudflared:latest tunnel --no-autoupdate run --token
docker network connect tunnel cloudflare
docker-compose up
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container
Looks like the apps you’re running on your NAS are proxied through the docker runtime. Consequently, the IP:port you need to add to the cloudflare tunnel config is the one that is reachable from the Host (not the IP of the host itself).
If the host is 192.168.1.135, you need to know which the the IP (internal to the docker network) of the app that you want to access from the outside, typically in the 172.0.0.1/24 range.
Example: If the containers running the apps you want to access are running on 172.0.0.2:4444 for app1 and 172.0.0.3:5555 for app2, the cloudflare config would look like this:
See more details and a video here: How to redirect subdomain to port (docker)