Docker Compose example

In this section, we quickly go over a Docker Compose file exposing a service using the Docker provider. This will also be used as a starting point for the other Docker Compose guides.

Setup

  • Edit a docker-compose.yml file with the following content:
  1. version: "3.3"
  2. services:
  3. traefik:
  4. image: "traefik:v2.10"
  5. container_name: "traefik"
  6. command:
  7. #- "--log.level=DEBUG"
  8. - "--api.insecure=true"
  9. - "--providers.docker=true"
  10. - "--providers.docker.exposedbydefault=false"
  11. - "--entrypoints.web.address=:80"
  12. ports:
  13. - "80:80"
  14. - "8080:8080"
  15. volumes:
  16. - "/var/run/docker.sock:/var/run/docker.sock:ro"
  17. whoami:
  18. image: "traefik/whoami"
  19. container_name: "simple-service"
  20. labels:
  21. - "traefik.enable=true"
  22. - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
  23. - "traefik.http.routers.whoami.entrypoints=web"

Networking

The Traefik container has to be attached to the same network as the containers to be exposed. If no networks are specified in the Docker Compose file, Docker creates a default one that allows Traefik to reach the containers defined in the same file. You can customize the network as described in the example below. You can use a pre-existing network too.

  1. version: "3.3"
  2. networks:
  3. traefiknet: {}
  4. services:
  5. traefik:
  6. image: "traefik:v2.10"
  7. ...
  8. networks:
  9. - traefiknet
  10. whoami:
  11. image: "traefik/whoami"
  12. ...
  13. networks:
  14. - traefiknet
  • Replace whoami.localhost by your own domain within the traefik.http.routers.whoami.rule label of the whoami service.
  • Run docker-compose up -d within the folder where you created the previous file.
  • Wait a bit and visit http://your_own_domain to confirm everything went fine. You should see the output of the whoami service. Something similar to:

    1. Hostname: d7f919e54651
    2. IP: 127.0.0.1
    3. IP: 192.168.64.2
    4. GET / HTTP/1.1
    5. Host: whoami.localhost
    6. User-Agent: curl/7.52.1
    7. Accept: */*
    8. Accept-Encoding: gzip
    9. X-Forwarded-For: 192.168.64.1
    10. X-Forwarded-Host: whoami.localhost
    11. X-Forwarded-Port: 80
    12. X-Forwarded-Proto: http
    13. X-Forwarded-Server: 7f0c797dbc51
    14. X-Real-Ip: 192.168.64.1

Details

  • As an example, we use whoami (a tiny Go server that prints OS information and HTTP request to output) which was used to define our simple-service container.

  • We define an entry point, along with the exposure of the matching port within Docker Compose, which allow us to “open and accept” HTTP traffic:

  1. command:
  2. # Traefik will listen to incoming request on the port 80 (HTTP)
  3. - "--entrypoints.web.address=:80"
  4. ports:
  5. - "80:80"
  • We expose the Traefik API to be able to check the configuration if needed:
  1. command:
  2. # Traefik will listen on port 8080 by default for API request.
  3. - "--api.insecure=true"
  4. ports:
  5. - "8080:8080"

Note

If you are working on a remote server, you can use the following command to display configuration (require curl & jq):

  1. curl -s 127.0.0.1:8080/api/rawdata | jq .
  • We allow Traefik to gather configuration from Docker:
  1. traefik:
  2. command:
  3. # Enabling Docker provider
  4. - "--providers.docker=true"
  5. # Do not expose containers unless explicitly told so
  6. - "--providers.docker.exposedbydefault=false"
  7. volumes:
  8. - "/var/run/docker.sock:/var/run/docker.sock:ro"
  9. whoami:
  10. labels:
  11. # Explicitly tell Traefik to expose this container
  12. - "traefik.enable=true"
  13. # The domain the service will respond to
  14. - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
  15. # Allow request only from the predefined entry point named "web"
  16. - "traefik.http.routers.whoami.entrypoints=web"

Using Traefik for Business Applications?

If you are using Traefik in your organization, consider Traefik Enterprise. You can use it as your:

Traefik Enterprise simplifies the discovery, security, and deployment of APIs and microservices across any environment. See it in action in this short video walkthrough.