I am trying to move a Traefik web service config from a yml file to embedding the config as labels in the docker-compose file. I have tried several times and not seen any evidence that the labels are being read.
The docs https://docs.traefik.io/providers/docker/#docker-swarm-modeseem to state that config can be read from the docker-compose file. And several searches show people have to move the labels to the deploy section of the docker-compose file. As I understand it that is only when running a docker swarm.
I am not running a swarm
I have multiple docker-compose files on the same server. Only the labels in the Traefik docker-compose file (traefik and whoami services) seem to work.
Do I have to move over to a swarm to be able to take advantage of the labels?
version: '3'
services:
web:
build: nginx
# networks:
# - outside
# - default
#ports:
# Port should be set in docker-compose.override.yml
# - "9999:80"
volumes:
- ./src/public:/usr/share/nginx/html:ro
- ./nginx/sites-enabled:/etc/nginx/sites-enabled
- ./nginx/snippets:/etc/nginx/snippets
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./files:/files
- /var/run/docker.sock:/var/run/docker.sock:ro
labels:
- "traefik.enable=true"
- "traefik.http.routers.web.entrypoints=websecure"
- "traefik.http.routers.web.rule=Host(`fiagt.gxxxs.page`)"
- "traefik.http.routers.web.service=fiagt"
# Authentication
- "traefik.http.routers.web.middlewares=usersfile@file"
# Certificate name and san
- "traefik.http.routers.web.tls.certresolver=basic"
- "traefik.http.routers.web.tls.domains[0].main=gxxxs.page"
- "traefik.http.routers.web.tls.domains[0].sans=*.gxxxs.page"
# Services
- "traefik.http.services.web.servers.url=http://192.168.1.130:8007/index.php"
- "traefik.http.services.web.loadbalancer.server.port=80"
- "traefik.http.services.web.passhostheader=false"
- "traefik.http.services.web.hostname=fiagt.docker.dev.gxxxs.page"
links:
- php
php:
build: php
# networks:
# - default
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
#mem_limit: 4000000000
#memswap_limit: 8000000000
privileged: true
volumes:
- ./src/public:/usr/share/nginx/html
# On the php service run ps aux | grep php-fpm to see where the php-fpm.conf is found
#decided to stop sharing these files and use the Dockerfile to create the required entries
#- ./php/conf:/usr/local/etc
- ./files:/files
links:
- db
# The Database
db:
image: mysql:5.7
volumes:
- ./mysql/db:/var/lib/mysql
- ./mysql/source:/docker-entrypoint-initdb.d
#restart: always
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "no"
MYSQL_ROOT_PASSWORD:
MYSQL_DATABASE: fiagt
MYSQL_USER: root
MYSQL_PASSWORD:
ports:
- "33061:3306"
# Add a phpmyadmin container when needed.
# http://docker.dev:8000 to launch
# credentials:
phpmyadmin:
image: nazarpc/phpmyadmin
#ports:
#- "9998:80"
links:
- db:mysql
labels:
- "traefik.enable=true"
- "traefik.http.routers.phpmyadmin.entrypoints=websecure"
- "traefik.http.routers.phpmyadmin.rule=Host(`fiagtadm.gxxxs.page`)"
- "traefik.http.routers.phpmyadmin.service=fiagtadm"
# Authentication
- "traefik.http.routers.phpmyadmin.middlewares=usersfile@file"
# Certificate name and san
- "traefik.http.routers.phpmyadmin.tls.certresolver=basic"
- "traefik.http.routers.phpmyadmin.tls.domains[0].main=gxxxs.page"
- "traefik.http.routers.phpmyadmin.tls.domains[0].sans=*.gxxxs.page"
# Services
- "traefik.http.services.phpmyadmin.servers.url=http://192.168.1.130:9007/index.php"
- "traefik.http.services.phpmyadmin.passhostheader=false"
#networks:
# outside:
# external:
# name: adbname
As the IP are dynamic (related to Docker) and detected automatically by Traefik, you just have to define (if needed) the port by using traefik.http.services.<random_name>.loadbalancer.server.port
Thank you Idez,
the docs are helpful, but I did not succeed yet.
First a historic statement - I learnt to use docker on an old Mac with VirtualBox and networking was a nightmare to get working. I recently moved my docker env to Ubuntu and did not think to review the network settings - I'd be happy if I can remove all the network references - I tried - but so far I kept getting problems.
Back to the settings - I removed the unsupported parameters and tried again. Still no luck ( I assume a docker-compose down / docker-compose up for my app will trigger Traefik to refresh the list of routers).
For simplicity, I'll omit the 2nd service.
version: '3'
networks:
default:
external:
name: nucnuc
services:
web:
build: nginx
# networks:
# - nucnuc
# - default
ports:
# Set the required port number here and update launcher :: siteCatalog.yml
- "8007:80"
#ports:
# Port should be set in docker-compose.override.yml
# - "9999:80"
volumes:
- ./src/public:/usr/share/nginx/html:ro
- ./nginx/sites-enabled:/etc/nginx/sites-enabled
- ./nginx/snippets:/etc/nginx/snippets
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./files:/files
- /var/run/docker.sock:/var/run/docker.sock:ro
labels:
- "traefik.enable=true"
- "traefik.docker.network=nucnuc"
- "traefik.http.routers.web.rule=Host(`fiagt.gxxxs.page`)"
- "traefik.http.routers.web.entrypoints=websecure"
- "traefik.http.routers.web.service=fiagt"
# Authentication
- "traefik.http.routers.web.middlewares=usersfile@file"
# Certificate name and san
- "traefik.http.routers.web.tls.certresolver=basic"
- "traefik.http.routers.web.tls.domains[0].main=gxxxs.page"
- "traefik.http.routers.web.tls.domains[0].sans=*.gxxxs.page"
# Services
- "traefik.http.services.fiagt.loadbalancer.server.port=8007"
- "traefik.http.services.fiagt.loadbalancer.server.scheme=http"
- "traefik.http.services.fiagt.passhostheader=false"
links:
- php
...omitted
Should the port be the internal port or the published port ?
traefix services has - --providers.docker.exposedbydefault=false
@Idez, thanks for the pointers. I managed to get it working in the end.
One thing that was throwing me was that the service names used in docker-compose have to be unique across the docker-machine.
I had two different solutions using service web and eventually spotted that the Traefik router on one solution was actually invoking the web service on the other. I now have it working, but don't feel this is the best solution as I would like to reuse docker-compose files as templates in several projects.