Traefik Bad Gateway – Traefik

Photo of author
Written By M Ibrahim
bad-gateway boot2docker load-balancing traefik

The Problem:

A Traefik user is experiencing "502 Bad Gateway" errors randomly. The user has provided the docker-compose file, Traefik configuration, and debug output, but the cause of the issue remains unclear. Additionally, the user suggests that the issue might be related to the use of the same docker-compose file for multiple sites.

The Solutions:

Solution 1: Recreate Network and Restart Containers

Recreate the docker network named “proxy” using the command `docker network create proxy`. Then, restart all the containers associated with the network by running `docker restart $(docker ps -a -f network=proxy –format “{{.Names}}”)`. This may resolve the 502 Bad Gateway issue.

Solution 3: Network Configuration

When encountering a `Bad Gateway` error with Traefik, the underlying cause is often a Docker networking issue. Traefik has a `docker.network` setting that specifies the default network for containers. By default, this setting is not set. You can set a default network or override it for specific services using the `traefik.docker.network` label.

To set a default network, add the following to your Traefik configuration:

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "example.com"
watch = true
exposedbydefault = false
network = "proxy"

Alternatively, you can override the network for a specific service using the `traefik.docker.network` label. For example:

labels:
 - traefik.docker.network=proxy

Solution 4: Wrong loadbalancer server port

In some cases, the issue can be caused by a wrong load balancer server port. To fix it, you should remove the label or change it to the correct port using the following format:

- "traefik.http.services.XXX.loadbalancer.server.port=XXX"

Solution 5: Enable Traefik

In this example, Traefik is not enabled. To resolve the issue, enable Traefik by setting:

“`
traefik.enable=true
“`