Multiple containers on localhost - prebuilt images work, own Dockerfile => Bad Gateway

I have Traefix Reverse Proxy Server up and running on my host machine (localhost, my own laptop) and have set up a few test services.

This is the contents of my docker-compose.yml:

version: "3"

networks:
  web:
    external: true
  internal:
    external: false

services:
    proxy:
        image: traefik
        ports:
          - "80:80"
          - "443:443"
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
          - $PWD/traefik.toml:/traefik.toml
          - $PWD/traefik_dynamic.toml:/traefik_dynamic.toml
          - $PWD/acme.json:/acme.json
        networks:
          - internal
          - web
    blog:
        image: wordpress:4.9.8-apache
        environment:
          WORDPRESS_DB_PASSWORD:
        labels:
          - traefik.http.routers.blog.rule=Host(`blog.localhost`)
          - traefik.http.routers.blog.tls=true
          - traefik.http.routers.blog.tls.certresolver=lets-encrypt
          - traefik.port=80
        networks:
          - internal
          - web
    apache:
        image: docker.io/bitnami/apache:2.4
        labels:
           - traefik.http.routers.apache.rule=Host(`apache.localhost`)
           - traefik.http.routers.apache.tls=true
           - traefik.http.routers.apache.tls.certresolver=lets-encrypt
           - traefik.port=80
        networks:
          - internal
          - web
    one:
        build:
            context: .
            dockerfile: PHP.Dockerfile
        labels:
           - traefik.http.routers.one.rule=Host(`one.localhost`)
           - traefik.http.routers.one.tls=true
           - traefik.http.routers.one.tls.certresolver=lets-encrypt
           - traefik.port=80
        networks:
          - internal
          - web
        working_dir: /var/www
        volumes:
            - ./:/var/www
    mysql:
        image: mysql:5.7
        environment:
          MYSQL_ROOT_PASSWORD:
        networks:
          - internal
        labels:
          - traefik.enable=false
    adminer:
        image: adminer:4.6.3-standalone
        labels:
          - traefik.http.routers.adminer.rule=Host(`db-admin.localhost`)
          - traefik.http.routers.adminer.tls=true
          - traefik.http.routers.adminer.tls.certresolver=lets-encrypt
          - traefik.port=8080
        networks:
          - internal
          - web
        depends_on:
          - mysql

All of the services which run from images (i.e. adminer, mysql, blog, apache and treafik itself) work perfectly fine and are accessible on the domains specified (e.g. blog.localhost, apache.localhost etc).

However, the one service (named ‘one’) which is built from my own Dockerfile returns “Bad Gateway” at one.localhost

This is the contents of my Dockerfile, PHP.Dockerfile which creates this ‘one’ container:

# Apache
FROM php:7.4-fpm

RUN apt-get update && apt-get install -y \
    curl \
    unzip \
    zlib1g-dev \
    libzip-dev \
    libonig-dev \
    libpng-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev

RUN docker-php-ext-configure gd --with-freetype --with-jpeg=/usr/include/ --enable-gd

RUN docker-php-ext-install gd
RUN docker-php-ext-install zip
RUN docker-php-ext-install mysqli

This PHP.Dockerfile is based on which which I had working prior to using Traefix, and just nginx on it’s own, i.e.

version: "3"
services:
    nginx:
        image: nginx:latest
        container_name: nginx
        ports:
            - 80:80
        volumes:
            - ./nginx.conf:/etc/nginx/conf.d/nginx.conf
            - ./app:/app
    web_one:
        build:
            context: .
            dockerfile: PHP.Dockerfile
        container_name: web_one
        volumes:
            - ./app:/app

The only difference between the services which are working and the one that isn’t is the fact it’s built from the Dockerfile, and the working_dir and volumes for that service.

docker-compose up returns no errors.

docker ps :

CONTAINER ID   IMAGE                      COMMAND                  CREATED          STATUS         PORTS                                                                      NAMES
696b94a6e4b0   dockertest_one             "docker-php-entrypoi…"   8 minutes ago    Up 8 minutes   9000/tcp                                                                   dockertest_one_1
4a1795cd77a4   wordpress:4.9.8-apache     "docker-entrypoint.s…"   10 minutes ago   Up 8 minutes   80/tcp                                                                     dockertest_blog_1
0e52c17fc79c   bitnami/apache:2.4         "/opt/bitnami/script…"   22 minutes ago   Up 8 minutes   8080/tcp, 8443/tcp                                                         dockertest_apache_1
9d783c82042d   adminer:4.6.3-standalone   "entrypoint.sh docke…"   31 minutes ago   Up 8 minutes   8080/tcp                                                                   dockertest_adminer_1
a80db3972ca9   mysql:5.7                  "docker-entrypoint.s…"   31 minutes ago   Up 8 minutes   3306/tcp, 33060/tcp                                                        dockertest_mysql_1
09e8d8fdc445   traefik                    "/entrypoint.sh trae…"   31 minutes ago   Up 8 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   dockertest_proxy_1

From this output, I notice it’s referenced dockertest_one and on port 9000, whilst blog.localhost (wordpress) which works, is on port 80.

Can anyone point me in the right direction?

Much appreciated,

Hello @runriot,

The traefik.port label is not used on Traefik v2.

You have your application container listening on port 9000, but Traefik is trying to forward requests to it on port 80 by default.

You need to set the load balancer server port to 9000 to tell Traefik to forward to that port: (Docker - Traefik)