When I planned to update the server I have to Traefik 2.0, at first I thought it was a bit scary, after having a quick look at the official migration tutorial I realized there weren't too much things to do.
This is the official migration documentation to update Traefik 1.7 to 2.0:
Basically there are 4 steps to upgrade Traefik from 1.7 to v2.0:
- Add TOML configuration into commands label
- Update labels syntax
- Remove the traefik.toml file from volumes
- Update the docker-compose file of the projects that work with Traefik for the new syntax
Let me show as example what my old files look like, I highlighted the parts that change between 1.7 <-> 2.0:
Traefik 1.7 docker-compose.yml (old)
version: '3'
services:
traefik:
image: traefik:v1.7.26-alpine
restart: always
ports:
- 80:80
- 443:443
networks:
- web
labels:
- "traefik.frontend.rule=Host:traefik.mydomain.com"
- "traefik.frontend.entryPoints=http,https"
- "traefik.port=8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./config/traefik.toml:/traefik.toml
- ./config/acme.json:/acme.json
container_name: traefik
networks:
web:
external: true
Traefik 2.0 docker-compose.yml (new)
version: '3'
services:
traefik:
image: traefik:v2.3.0
command:
- "--api.insecure=true"
- "--providers.docker=true"
- "--entryPoints.web.address=:80"
- "--entryPoints.websecure.address=:443"
- "--certificatesResolvers.le.acme.email=my@email.com"
- "--certificatesResolvers.le.acme.storage=acme.json"
- "--certificatesResolvers.le.acme.tlsChallenge=true"
- "--certificatesResolvers.le.acme.httpChallenge=true"
- "--certificatesResolvers.le.acme.httpChallenge.entryPoint=web"
restart: always
ports:
- 80:80
- 443:443
networks:
- web
labels:
- traefik.http.routers.http_catchall.rule=HostRegexp(`{any:.+}`)
- traefik.http.routers.http_catchall.entrypoints=web
- traefik.http.routers.http_catchall.middlewares=https_redirect
- traefik.http.middlewares.https_redirect.redirectscheme.scheme=https
- traefik.http.middlewares.https_redirect.redirectscheme.permanent=true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./config/acme.json:/acme.json
container_name: traefik
networks:
web:
external: true
After updating docker-compose.yml file for Traefik, it's ready to work. We still have to update the syntax of ALL docker-compose files that use Traefik.
In this example I'll show you how I had to update a Wordpress project I have, the only thing to do is to update the labels from the old syntax to the new one. I highlighted the only change in the old and new file:
Wordpress docker-compose.yml (old)
version: '3'
services:
wordpress:
image: wordpress
restart: unless-stopped
depends_on:
- wordpress_db
networks:
- web
- wordpress_net
env_file:
- wp_env
volumes:
- ./config/php.conf.uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
- wordpress:/var/www/html
links:
- wordpress_db:mysql
labels:
- "traefik.enable=true"
- "traefik.frontend.rule=Host:mywordpress.domain.com"
- "traefik.frontend.entryPoints=http,https"
- "traefik.port=80"
container_name: wordpress
wordpress_db:
image: mariadb
restart: unless-stopped
networks:
- web
- wordpress_net
env_file:
- wp_db_env
volumes:
- ./wp-data:/docker-entrypoint-initdb.d
- wordpress_db:/var/lib/mysql
expose:
- 3306
container_name: wordpress_db
volumes:
wordpress:
wordpress_db:
networks:
web:
external: true
wordpress_net:
driver: bridge
Wordpress docker-compose.yml (new)
version: '3'
services:
wordpress:
image: wordpress
restart: unless-stopped
depends_on:
- wordpress_db
networks:
- web
- wordpress_net
env_file:
- wp_env
volumes:
- ./config/php.conf.uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
- wordpress:/var/www/html
links:
- wordpress_db:mysql
labels:
- traefik.http.routers.wordpress.rule=Host(`mywordpress.domain.com`)
- traefik.http.routers.wordpress.tls=true
- traefik.http.routers.wordpress.tls.certresolver=le
- traefik.http.services.wordpress.loadbalancer.server.port=80
container_name: wordpress
wordpress_db:
image: mariadb
restart: unless-stopped
networks:
- web
- wordpress_net
env_file:
- wp_db_env
volumes:
- ./wp-data:/docker-entrypoint-initdb.d
- wordpress_db:/var/lib/mysql
expose:
- 3306
container_name: wordpress_db
volumes:
wordpress:
wordpress_db:
networks:
web:
external: true
wordpress_net:
driver: bridge