skip to Main Content

Here is a docker compose file

version: "3"
networks:
  vester-net:
    name: vester-net

services:
  traefik:
    image: traefik:v2.10
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro        # It's not recommended mounting the docker socket into a container -> see https://github.com/wollomatic/traefik2-hardened
    networks:
      - vester-net
    command: --api.insecure=true --providers.docker --accessLog
    ports:
      - 80:80
      - 8080:8080
    labels:
      - traefik.enable=true
 
  whoami:
    image: traefik/whoami:v1.8
    networks:
      - vester-net
    labels:
      - traefik.http.routers.mywhoami.rule=(Host(`localhost`) && PathPrefix(`/whoami`))

  simplified_flask:
    build:
      context: ./apps/score
    networks:
      - vester-net
    labels:
      - traefik.http.routers.myflask.rule=(Host(`localhost`) && PathPrefix(`/flask`))

When I go to http://localhost:8080, everything goes well: it redirects to traefik dashboard and it works.

When I go to http://localhost/whoami, everything goes well: I see whoami infos

When I go to http://localhost/flask, it says 404 page not found.

I would love to have the community support on this. Thanks in advance.

2

Answers


  1. You have set up Traefik to route requests to different services based on the URL path. Your "404 page not found" error might be due to a misconfiguration in the Traefik labels for this service.

    +------------------+          +-----------------+          +------------------+
    |                  | 80, 8080 |                 |          |                  |
    |     Traefik      +---------->  whoami service |          | simplified_flask |
    |                  |          |                 |          |     service      |
    +------------------+          +-----------------+          +------------------+
          ^      |                    /whoami                        /flask
          |      |                      ^                              ^
          |      |                      |                              |
          |      +----------------------+------------------------------+
          |
          +-- Traefik Dashboard (http://localhost:8080)
    

    Make sure the Traefik labels for your simplified_flask service are correctly configured. The traefik.http.routers.myflask.rule should correctly define the host and path prefix. (- traefik.http.routers.myflask.rule=(Host(localhost) && PathPrefix(/flask)))

    Use the Traefik dashboard to inspect the routers and services. Look for the router corresponding to your Flask application (e.g., myflask). Check if the router is properly configured and linked to the correct service.

    And check the logs of the Traefik container (docker logs [traefik_container_name]) to see if there are any errors or warnings related to the simplified_flask service.

    Make sure your Flask application is configured to serve content on the root (/) or the specified path prefix (/flask). If it is expecting a different base path, it might respond with a 404 error.
    For that, open the Flask application code, typically found in the directory specified in the context of the simplified_flask service in docker-compose.yml.


    What would you have done?

    I would start from an existing Traefik-Flask tutorial/example, and then modify it to match my use case.

    From example, start from testdrivenio/flask-docker-traefik propose a good starting point.

    Also, the article "Flask, Gunicorn, Docker and Traefik" from Paul Marriott can give you some ideas.

    Login or Signup to reply.
  2. change

    traefik.http.routers.myflask.rule=(Host(`localhost`) && PathPrefix(`/flask`))
    

    to

    traefik.http.routers.simplified_flask.rule=(Host(`localhost`) && PathPrefix(`/flask`))
    

    that way traefik knows what service to route to

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search