Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Traefik with docker compose
victor_dalet
victor_dalet

Posted on

     

Traefik with docker compose

In this short post, I show how to use the traefik image in docker compose to publish your site with your domain name and generate an ssl certificate.


I - Create the traefik.toml file

  • Create the traefik.tom file
  • Copy the following code into it
  • Add your email address at the end of the file
[api]dashboard=trueinsecure=true[entryPoints][entryPoints.web]address=":80"[entryPoints.web.http][entryPoints.web.http.redirections][entryPoints.web.http.redirections.entryPoint]to="websecure"scheme="https"permanent=true[entryPoints.websecure]address=":443"[entryPoints.websecure.http.tls]certResolver="default"[providers][providers.docker]watch=trueexposedByDefault=falsenetwork="web"[certificatesResolvers][certificatesResolvers.default][certificatesResolvers.default.acme]email=""storage="acme.json"caServer="https://acme-v02.api.letsencrypt.org/directory"[certificatesResolvers.default.acme.tlsChallenge]
Enter fullscreen modeExit fullscreen mode

II - Create the acme.json file

This file is used to store https certificates.
You must assign the correct right as in the following example.

touchacme.jsonchmod600 acme.json
Enter fullscreen modeExit fullscreen mode

III - Create network

  • Type the following command in your terminal
docker network create web
Enter fullscreen modeExit fullscreen mode
  • Add this code to the end of your docker-compose file
networks:web:external:true
Enter fullscreen modeExit fullscreen mode

IV - Traefik docker image

Here's the docker-compose block to call traefik, share the http and https ports and the many configuration files.
D'ont fortget to add the network we've just created inside.
Port 8080 is the traefik interface, so you don't have to add it.
The different labels are used to accept www subdomains.

reverse-proxy:image:traefik:v2.4container_name:traefikports:-"80:80"-"8080:8080"-"443:443"volumes:-/var/run/docker.sock:/var/run/docker.sock-traefik.toml:/etc/traefik/traefik.toml-acme.json:/acme.jsonlabels:-"traefik.http.middlewares.strip-www.redirectregex.regex=^https?://(www\\.)(.+)"-"traefik.http.middlewares.strip-www.redirectregex.replacement=https://$${2}"-"traefik.http.middlewares.strip-www.redirectregex.permanent=true"restart:alwaysnetworks:-web
Enter fullscreen modeExit fullscreen mode

V - Configure traefik for your docker compose service

In your other block, you need to add the network and the various labels.
The first label is to activate traefik, the second to add your domain or sub-domain (don't forget to change the name of your router --> in this example it's api and front), the last is to redirect to https.

api:build:api/labels:-"traefik.enable=true"-"traefik.http.routers.api.rule=Host(`api.monsite.fr`)"-"traefik.http.routers.api.entrypoints=websecure"networks:-webrestart:alwaysfront:build:context:front/dockerfile:Dockerfile.prodlabels:-"traefik.enable=true"-"traefik.http.routers.front.rule=Host(`monsite.fr`)"-"traefik.http.routers.front.entrypoints=websecure"networks:-web
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi....
  • Location
    Paris
  • Education
    ESGI
  • Work
    computer vision developer
  • Joined

More fromvictor_dalet

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp