-
Notifications
You must be signed in to change notification settings - Fork 8.1k
HTTP routing with Traefik: Update guide to include DHI #23769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,23 +41,86 @@ While there are [many Traefik-monitored labels](https://doc.traefik.io/traefik/r | |
|
|
||
| Let’s do a quick demo of starting Traefik and then configuring two additional containers to be accessible using different hostnames. | ||
|
|
||
| <!-- markdownlint-disable MD029 --> | ||
|
|
||
| 1. In order for two containers to be able to communicate with each other, they need to be on the same network. Create a network named `traefik-demo` using the `docker network create` command: | ||
|
|
||
| ```console | ||
| $ docker network create traefik-demo | ||
| ``` | ||
|
|
||
| 2. Start a Traefik container using the following command. The command exposes Traefik on port 80, mounts the Docker socket (which is used to monitor containers to update configuration), and passes the `--providers.docker` argument to configure Traefik to use the Docker provider. | ||
| 2. Start a Traefik container using one of the following methods. These commands exposes Traefik on port 80, mounts the Docker socket (which is used to monitor containers to update configuration), and passes the `--providers.docker` argument to configure Traefik to use the Docker provider. | ||
|
|
||
| ```console | ||
| $ docker run -d --network=traefik-demo -p 80:80 -v /var/run/docker.sock:/var/run/docker.sock traefik:v3.6.2 --providers.docker | ||
| ``` | ||
| {{< tabs >}} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the content between steps 2 to 3 should be indented. |
||
| {{< tab name="Using Docker Hardened Images" >}} | ||
|
|
||
| Docker Hardened Images (DHI) for Traefik are available on [Docker Hub](https://hub.docker.com/hardened-images/catalog/dhi/traefik). | ||
| If you haven't authenticated yet, first run: | ||
|
|
||
| ```bash | ||
| docker login dhi.io | ||
| ``` | ||
|
|
||
| For example — use: | ||
| `FROM dhi.io/traefik:<tag>` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Dockerfile snippet seems out of place. Not sure if it's a typo that was suppose to show a docker pull command, or it needs more context like, this guide shows you how to run the container, but you can also use it as a base image in a Dockerfile. For example... |
||
|
|
||
| Then start a container using the Hardened image: | ||
|
|
||
| ```console | ||
| $ docker run -d --network=traefik-demo \ | ||
| -p 80:80 \ | ||
| -v /var/run/docker.sock:/var/run/docker.sock \ | ||
| dhi.io/traefik:3.6.2 \ | ||
| --providers.docker | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
| {{< tab name="Using the official image" >}} | ||
|
|
||
| You can also use the official image from Docker Hub: | ||
|
|
||
| ```console | ||
| $ docker run -d --network=traefik-demo \ | ||
| -p 80:80 \ | ||
| -v /var/run/docker.sock:/var/run/docker.sock \ | ||
| traefik:v3.6.2 \ | ||
| --providers.docker | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
| {{< /tabs >}} | ||
|
|
||
| 3. Now, start a simple Nginx container and define the labels Traefik is watching for to configure the HTTP routing. Note that the Nginx container is not exposing any ports. | ||
|
|
||
| ```console | ||
| $ docker run -d --network=traefik-demo --label 'traefik.http.routers.nginx.rule=Host(`nginx.localhost`)' nginx | ||
| ``` | ||
| {{< tabs >}} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole section between steps 3 to 4 should be indented. |
||
| {{< tab name="Using Docker Hardened Images" >}} | ||
|
|
||
| Docker Hardened Images (DHI) for Nginx are available on [Nginx DHI image](https://hub.docker.com/hardened-images/catalog/dhi/nginx). | ||
| If you haven't authenticated yet, first run: | ||
|
|
||
| ```bash | ||
| docker login dhi.io | ||
| ``` | ||
|
|
||
| ```console | ||
| $ docker run -d --network=traefik-demo \ | ||
| --label 'traefik.http.routers.nginx.rule=Host(`nginx.localhost`)' \ | ||
| dhi.io/nginx:1.29.3 | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
| {{< tab name="Using the official image" >}} | ||
|
|
||
| You can also run the official Nginx image as follows: | ||
|
|
||
| ```console | ||
| $ docker run -d --network=traefik-demo \ | ||
| --label 'traefik.http.routers.nginx.rule=Host(`nginx.localhost`)' \ | ||
| nginx:1.29.3 | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
| {{< /tabs >}} | ||
|
|
||
| Once the container starts, open your browser to [http://nginx.localhost](http://nginx.localhost) to see the app (all Chromium-based browsers route \*.localhost requests locally with no additional setup). | ||
|
|
||
|
|
@@ -69,6 +132,8 @@ Let’s do a quick demo of starting Traefik and then configuring two additional | |
|
|
||
| Once the container starts, open your browser to http://welcome.localhost. You should see a “Welcome to Docker” website. | ||
|
|
||
| <!-- markdownlint-enable MD029 --> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted above, can remove disabling this after fixing the indentation. |
||
|
|
||
| ## Using Traefik in development | ||
|
|
||
| Now that you’ve experienced Traefik, it’s time to try using it in a development environment. In this example, you will use a sample application that has a split frontend and backend. This app stack has the following configuration: | ||
|
|
@@ -81,33 +146,85 @@ Now that you’ve experienced Traefik, it’s time to try using it in a developm | |
|
|
||
| The application can be accessed on GitHub at [dockersamples/easy-http-routing-with-traefik](https://github.com/dockersamples/easy-http-routing-with-traefik). | ||
|
|
||
| <!-- markdownlint-disable MD029 --> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted above, can remove disabling this after fixing the indentation. |
||
|
|
||
| 1. In the `compose.yaml` file, Traefik is using the following configuration: | ||
|
|
||
| ```yaml | ||
| services: | ||
| proxy: | ||
| image: traefik:v3.6.2 | ||
| command: --providers.docker | ||
| ports: | ||
| - 80:80 | ||
| volumes: | ||
| - /var/run/docker.sock:/var/run/docker.sock | ||
| ``` | ||
| {{< tabs >}} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole section between steps 1 to 2 should be indented. |
||
| {{< tab name="Using DHI image" >}} | ||
|
|
||
| ```yaml | ||
| services: | ||
| proxy: | ||
| image: dhi.io/traefik:3.6.2 | ||
| command: --providers.docker | ||
| ports: | ||
| - 80:80 | ||
| volumes: | ||
| - /var/run/docker.sock:/var/run/docker.sock | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
| {{< tab name="Using official image" >}} | ||
|
|
||
| ```yaml | ||
| services: | ||
| proxy: | ||
| image: traefik:v3.6.2 | ||
| command: --providers.docker | ||
| ports: | ||
| - 80:80 | ||
| volumes: | ||
| - /var/run/docker.sock:/var/run/docker.sock | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
| {{< /tabs >}} | ||
|
|
||
| Note that this is essentially the same configuration as used earlier, but now in a Compose syntax. | ||
|
|
||
| 2. The client service has the following configuration, which will start the container and provide it with the labels to receive requests at localhost. | ||
|
|
||
| ```yaml {hl_lines=[7,8]} | ||
| services: | ||
| # … | ||
| client: | ||
| image: nginx:alpine | ||
| volumes: | ||
| - "./client:/usr/share/nginx/html" | ||
| labels: | ||
| traefik.http.routers.client.rule: "Host(`localhost`)" | ||
| ``` | ||
| {{< tabs >}} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole section between steps 2 to 3 should be indented. |
||
| {{< tab name="Using Docker Hardened Images" >}} | ||
|
|
||
| Docker Hardened Images (DHI) for Nginx are available on [Nginx DHI image](https://hub.docker.com/hardened-images/catalog/dhi/nginx). | ||
|
|
||
| If you haven't authenticated yet, first run: | ||
|
|
||
| ```bash | ||
| docker login dhi.io | ||
| ``` | ||
|
|
||
| You can use it as your base image as shown following: | ||
|
|
||
| ```yaml | ||
| services: | ||
| # … | ||
| client: | ||
| image: dhi.io/nginx:1.29.3-alpine3.21 | ||
| volumes: | ||
| - "./client:/usr/share/nginx/html" | ||
| labels: | ||
| traefik.http.routers.client.rule: "Host(`localhost`)" | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
| {{< tab name="Using the official image" >}} | ||
|
|
||
| ```yaml | ||
| services: | ||
| # … | ||
| client: | ||
| image: nginx:1.29.3-alpine3.22 | ||
| volumes: | ||
| - "./client:/usr/share/nginx/html" | ||
| labels: | ||
| traefik.http.routers.client.rule: "Host(`localhost`)" | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
| {{< /tabs >}} | ||
|
|
||
| 3. The api service has a similar configuration, but you’ll notice the routing rule has two conditions - the host must be “localhost” and the URL path must have a prefix of “/api”. Since this rule is more specific, Traefik will evaluate it first compared to the client rule. | ||
|
|
||
|
|
@@ -140,6 +257,8 @@ The application can be accessed on GitHub at [dockersamples/easy-http-routing-wi | |
|
|
||
| And that’s it. Now, you only need to spin up the Compose stack with a `docker compose up` and all of the services and applications will be ready for development. | ||
|
|
||
| <!-- markdownlint-enable MD029 --> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted above, can remove disabling this after fixing the indentation. |
||
|
|
||
| ## Sending traffic to non-containerized workloads | ||
|
|
||
| In some situations, you may want to forward requests to applications not running in containers. In the following architecture diagram, the same application from before is used, but the API and React apps are now running natively on the host machine. | ||
|
|
@@ -173,9 +292,31 @@ This configuration indicates that requests that for `localhost/api` will be forw | |
|
|
||
| With this file, the only change is to the Compose configuration for Traefik. There are specifically two things that have changed: | ||
|
|
||
| <!-- markdownlint-disable MD029 --> | ||
|
|
||
| 1. The configuration file is mounted into the Traefik container (the exact destination path is up to you) | ||
| 2. The `command` is updated to add the file provider and point to the location of the configuration file | ||
|
|
||
| {{< tabs >}} | ||
| {{< tab name="Using DHI image" >}} | ||
|
|
||
| ```yaml | ||
| services: | ||
| proxy: | ||
| image: dhi.io/traefik:3.6.2 | ||
| command: --providers.docker --providers.file.filename=/config/traefik-config.yaml --api.insecure | ||
| ports: | ||
| - 80:80 | ||
| - 8080:8080 | ||
| volumes: | ||
| - /var/run/docker.sock:/var/run/docker.sock | ||
| - ./dev/traefik-config.yaml:/config/traefik-config.yaml | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
|
|
||
| {{< tab name="Using official image" >}} | ||
|
|
||
| ```yaml | ||
| services: | ||
| proxy: | ||
|
|
@@ -189,6 +330,11 @@ services: | |
| - ./dev/traefik-config.yaml:/config/traefik-config.yaml | ||
| ``` | ||
|
|
||
| {{< /tab >}} | ||
| {{< /tabs >}} | ||
|
|
||
| <!-- markdownlint-enable MD029 --> | ||
|
|
||
| ### Starting the example app | ||
|
|
||
| To run the example app that forwards requests from Traefik to native-running apps, use the following steps: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest not disabling this. The errors are probably related to indention in the ordered list. Content between steps should be indented. In this particular case, between 2-3 and 3-4, you can add a couple spaces before all lines.