"port is missing"

I'm trying to get a Hello World up and running but failing miserably.

I started with the basic example Traefik gives with the whoami image, which works, but as soon as I apply the same config to a custom image I have with a server running, I get an error: time="2020-02-14T19:30:57Z" level=error msg="port is missing" providerName=docker container=my_custom_image-src-2812cdd4df598b75a9cb03395e72398370ca9e27a9abf531d5f813ee524b42a7

Here is my docker-compose:

version: '3.4'

services:
  
  traefik:
    # The official v2.0 Traefik docker image
    image: traefik:v2.0
    # Enables the web UI and tells Traefik to listen to docker
    command: --api.insecure=true --providers.docker
    ports:
      # The HTTP port
      - "80:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock

  whoami:
    # A container that exposes an API to show its IP address
    image: containous/whoami
    labels:
      - "traefik.http.routers.whoami.rule=Host(`whoami.whatever.localhost`)"
      
  my_custom_image:
    container_name: my_custom_image
    image: my_custom_image
    build:
      context: ./my_custom_image
    labels:
      - "traefik.http.routers.my_custom_image.rule=Host(`my_custom_image.whatever.localhost`)"

Any help would be greatly appreciated.

Hello,

You have to define the port of your app with the label: traefik.http.services.my_custom_image.loadbalancer.server.port

https://docs.traefik.io/v2.1/providers/docker/#port-detection

  my_custom_image:
    container_name: my_custom_image
    image: my_custom_image
    build:
      context: ./my_custom_image
    labels:
      - "traefik.http.routers.my_custom_image.rule=Host(`my_custom_image.whatever.localhost`)"
      - "traefik.http.services.my_custom_image.loadbalancer.server.port=80"

When I do that and visit my_custom_image.whatever.localhost it just gives me Bad Gateway

You have to set the good port, I putted 80 but I don't know the port of your application inside your my_custom_image

I did. Inside my container I have a server listening on 120.0.0.1:8080, and I set "traefik.http.services.my_custom_image.loadbalancer.server.port=8080". Is that correct?

yes so:

 - "traefik.http.services.my_custom_image.loadbalancer.server.port=8080"

I have that. When I run as it, I get 404 Not Found.

I tried using this config:

[log]
level = "INFO"

[entryPoints]
[entryPoints.web]
address = ":80"

[api]
insecure = true

[providers.docker]
endpoint = "unix:///var/run/docker.sock"
exposedByDefault = false

and adding these labels to my_custom_image:


    labels:
      - "traefik.enable=true"
      - "traefik.http.services.my_custom_image.loadbalancer.server.port=8080"
      - "traefik.http.routers.my_custom_image.entrypoints=web"
      - "traefik.http.routers.my_custom_image.rule=Host(`my_custom_image.whatever.localhost`)"

and with this config I get Bad Gateway

1 Like

Ah I figured it out. Super noob mistake: my server code was listening to localhost:80, so I changed it to 0.0.0.0:80 and it works now!

Is that normal? I thought Docker port bindings mapped to the container's localhost...

Just in case anyone runs into this specific scenario:

Also make sure that your container exposes the/a port and if needed set the Traefik port in the labels. (I.e. if there are multiple ports exposed or a non-default port is being used, overriding the Traefik port for the container might be necessary.)

If there image does not expose a particular port by default, you can solve it this way:

...

    expose:
    - 80
    labels:
    - "traefik.enable=true"
    - "traefik.http.routers.site.entryPoints=web,websecure"
    - "traefik.http.routers.site.rule=Host(`host.example.com`)"
    - "traefik.http.routers.site.tls=true"
    - "traefik.http.routers.site.tls.certresolver=letsencrypt"
    - "traefik.port=80"

...