traefik not respecting frontend rule - docker

I am trying to deploy multiple apps on my docker host and have traefik route traffic based on hostnames to the different apps
I am using docker-compose for all my docker containers
Here is my traeffik.yaml file
version: '3.5'
services:
traefik:
image: traefik
container_name: traefik
command: --api --docker
networks:
- traefik_network
ports:
- "80:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
traefik_network:
name: traefik_network
here is my wpapp1.yaml file
version: '3.5'
services:
mysql:
image: mysql:5.7
volumes:
- wpapp1_mysql:/var/lib/mysql
restart: always
container_name: wpapp1_mysql
networks:
- traefik_network
environment:
MYSQL_ROOT_PASSWORD: wpapp1
MYSQL_DATABASE: wpapp1
MYSQL_USER: wpapp1
MYSQL_PASSWORD: wpapp1
wordpress:
depends_on:
- mysql
image: wordpress:latest
volumes:
- wpapp1_wordpress:/var/www/html
restart: always
container_name: wpapp1_wordpress
networks:
- traefik_network
labels:
- "traefik.frontend.rule=Host:wpapp1.example.com"
- "traefik.port=80"
- "traefik.docker.network=traefik_network"
environment:
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_USER: wpapp1
WORDPRESS_DB_PASSWORD: wpapp1
volumes:
wpapp1_mysql:
name: wpapp1_mysql
wpapp1_wordpress:
name: wpapp1_wordpress
networks:
traefik_network:
external:
name: traefik_network
and here is my wpapp2.yaml file
version: '3.5'
services:
mysql:
image: mysql:5.7
volumes:
- wpapp2_mysql:/var/lib/mysql
restart: always
container_name: wpapp2_mysql
networks:
- traefik_network
environment:
MYSQL_ROOT_PASSWORD: wpapp2
MYSQL_DATABASE: wpapp2
MYSQL_USER: wpapp2
MYSQL_PASSWORD: wpapp2
wordpress:
depends_on:
- mysql
image: wordpress:latest
volumes:
- wpapp2_wordpress:/var/www/html
restart: always
container_name: wpapp2_wordpress
networks:
- traefik_network
labels:
- "traefik.frontend.rule=Host:wpapp2.example.com"
- "traefik.port=80"
- "traefik.docker.network=traefik_network"
environment:
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_USER: wpapp2
WORDPRESS_DB_PASSWORD: wpapp2
volumes:
wpapp2_mysql:
name: wpapp2_mysql
wpapp2_wordpress:
name: wpapp2_wordpress
networks:
traefik_network:
external:
name: traefik_network
So now i expect traefik to route based on the hostnames wpapp1.example.com and wpapp2.example.com BUT traefik is loadbalancing traffic!!!
So when i go to http:/wpapp1.example.com, traefik is loadbalancing it between the two apps and same for the other hostnames. Now sure what is going on here since i specifically add the traefik.frontend.rule
I mean how in the hell is that happening?
I have spent hours to figure what is going on and before i go insane i decided to some here to get some help on what is going on here.

Put your database on a different network. Otherwise WordPress will RR load balance to the two mysql instances in the same docker network (that's the expected behavior when you have two containers with the same alias on the same network). You can do that with the default network:
version: '3.5'
services:
mysql:
image: mysql:5.7
volumes:
- mysql:/var/lib/mysql
restart: unless-stopped
networks:
- db
environment:
MYSQL_ROOT_PASSWORD: wpapp
MYSQL_DATABASE: wpapp
MYSQL_USER: wpapp
MYSQL_PASSWORD: wpapp
wordpress:
depends_on:
- mysql
image: wordpress:latest
volumes:
- wordpress:/var/www/html
restart: unless-stopped
networks:
- traefik
- db
labels:
- "traefik.frontend.rule=Host:wpapp1.example.com"
- "traefik.port=80"
- "traefik.docker.network=traefik_network"
environment:
WORDPRESS_DB_HOST: mysql:3306
WORDPRESS_DB_USER: wpapp
WORDPRESS_DB_PASSWORD: wpapp
volumes:
mysql:
wordpress:
networks:
db:
traefik:
external:
name: traefik_network

Related

How to link PostgreSQL container and PgAdmin in Docker?

I have a docker-compose file with postgres and pgadmin services in my Laravel project. The problem is that when I do the database migration, it succeeds, but when I go to pgadmin, I do not see the tables I created there. I guess I, as always, made a mistake while writing docker-compose and I just can't find what I was missing.
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- postgres
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- postgres
#Redis
redis:
image: 'redis:alpine'
ports:
- "6379:6379"
#PostgreSQL
postgres:
container_name: postgres_container
image: postgres
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-5050}:80"
networks:
- postgres
restart: unless-stopped
#RabbitMQ
rabbit:
image: "rabbitmq:3-management"
hostname: "rabbit"
environment:
RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
RABBITMQ_DEFAULT_USER: "rabbitmq"
RABBITMQ_DEFAULT_PASS: "rabbitmq"
RABBITMQ_DEFAULT_VHOST: "/"
ports:
- "15672:15672"
- "5672:5672"
labels:
NAME: "rabbitmq"
#ElasticSearch
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.4.3
ports:
- "9200:9200"
- "9300:9300"
networks:
- postgres
#Docker Networks
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:
In pgAdmin, when you add a new server, you need to define "host.docker.internal" (when on Windows) as Host (How to add a new server).

Multi Docker-Compose projects sharing service

I am attempting to create multiple Docker-Compose projects and have them share a single service, a database server. I'm currently unable to get projects to use my mariadb instance. I'm not sure if my issues is in traefik or docker. I can get wordpress to recognize the database if it is included in the same docker-compose, but as I would ultimately like to not have multiple instances of a database server running, I would prefer it to be it's own project.
# ~/docker/traefik/docker-compose.yml
version: "3.3"
services:
traefik:
image: "traefik:v2.2"
container_name: "traefik"
command:
- "--api=true"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.myresolver.acme.dnschallenge=true"
- "--certificatesresolvers.myresolver.acme.dnschallenge.provider=namedotcom"
- "--certificatesresolvers.myresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory"
- "--certificatesresolvers.myresolver.acme.email=<MY-EMAIL>"
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
networks:
t2_proxy:
ports:
- "80:80"
- "443:443"
environment:
- "NAMECOM_USERNAME=<My-USERNAME>
- "NAMECOM_API_TOKEN=<My-Token>
labels:
# Dashboard
- "traefik.enable=true"
- "traefik.http.routers.traefik.rule=Host(`api.mydomain.com`)"
- "traefik.http.routers.traefik.service=api#internal"
- "traefik.http.routers.traefik.middlewares=admin"
- "traefik.http.routers.traefik.tls.certresolver=myresolver"
- "traefik.http.routers.traefik.entrypoints=websecure"
- "traefik.http.middlewares.admin.basicauth.users=myuser:mypasswordhash/"
# middleware redirect
- "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
# global redirect to https
- "traefik.http.routers.redirs.rule=hostregexp(`{host:.+}`)"
- "traefik.http.routers.redirs.entrypoints=web"
- "traefik.http.routers.redirs.middlewares=redirect-to-https"
volumes:
- "./letsencrypt:/letsencrypt"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
networks:
t2_proxy:
# ~/docker/db/docker-compose.yml
version: '3.3'
services:
mysql:
image: mariadb
container_name: "mariadb"
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: wordpressDB
MYSQL_USER: wordpressUSER
MYSQL_PASSWORD: wordpressPW
networks:
default:
ports:
- 3306:3306
adminer:
image: adminer
restart: always
container_name: "adminer"
labels:
- "traefik.enable=true"
- "traefik.http.routers.adminer.rule=PathPrefix(`/adminer`)"
- "traefik.http.routers.adminer.entrypoints=websecure"
- "traefik.http.routers.adminer.tls.certresolver=myresolver"
networks:
- default
- traefik_t2_proxy
networks:
default:
traefik_t2_proxy:
external: true
# ~/docker/wordpress/docker-compose.yml
version: '3.3'
services:
wordpress:
image: wordpress:latest
restart: always
container_name: "mySite"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpressUSER
WORDPRESS_DB_PASSWORD: wordpressPW
WORDPRESS_DB_NAME: wordpressDB
networks:
traefik_t2_proxy:
db_default:
labels:
- "traefik.enable=true"
- "traefik.http.routers.wp2.rule=PathPrefix(`/wordpress`)"
- "traefik.http.routers.wp2.entrypoints=websecure"
- "traefik.http.routers.wp2.tls.certresolver=myresolver"
volumes:
db_data: {}
networks:
traefik_t2_proxy:
external: true
db_default:
external: true
You can have multiple docker-compose files, with the same network and run them together as below:
docker-compose -f docker-compose-1.yml -f docker-compose-2.yml up -d
But if you need the services to recognize eachother, the networks should be the same. Give your traefik_t2_proxy network to mysql too.

Open multiple Wordpress projects in the docker

I am creating containers for all my projects, mostly in Wordpress, and I was wondering if for example I could upload several projects at the same time, because in this configuration that I use the docker, I have to disable one, in order to activate another. Example project1.local and project2.local, how could you do it that way?
docker-compose.yml
version: '3.3'
services:
wordpress:
build: .
container_name: ${APP_NAME}-wordpress
restart: always
ports:
- 80:80
environment:
WORDPRESS_DB_HOST: ${DB_HOST}
WORDPRESS_DB_USER: ${DB_USER}
WORDPRESS_DB_PASSWORD: ${DB_PASS}
WORDPRESS_DB_NAME: ${DB_NAME}
WORDPRESS_TABLE_PREFIX: ${DB_TABLE_PREFIX}
WORDPRESS_CONFIG_EXTRA: |
/* Direct FTP */
define('FS_METHOD', 'direct');
volumes:
- ./src:/var/www/html/wp-content:rw
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
depends_on:
- db
db:
image: mariadb
container_name: ${APP_NAME}-db
volumes:
- './database/db:/var/lib/mysql:delegated'
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASS}
restart: always
ports:
- '3306:3306'
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: ${APP_NAME}-phpmyadmin
volumes:
- ./config/phpmyadmin.ini:/usr/local/etc/php/conf.d/phpmyadmin.ini
environment:
PMA_HOST: ${DB_HOST}
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
ports:
- '8080:80'
links:
- db:db
Dockerfile
FROM wordpress
RUN chown -R www-data:www-data /var/www/html
There are some things that you can do:
1- Since you've declare:
ports:
- 80:80
You can't get to many wordpress at the same time with different docker-compose. So, you can delete the binding from your instance like this:
ports:
- 80
Doing it, you can make several docker-compose with different ports without worrying about the ports.
But there is another problem... Your database it's already up. So, in this case, if you want to use several docker-compose for each project you'll need another docker-compose for your database. So... the docker-compose solution is get another docker-compose for your database and in every docker-compose file add an external network:
With it, you can use the dns name as its always.
The other way is using the same template like this:
version: '3.3'
networks:
default:
external:
name: wordpresses
services:
wordpress_1:
build: .
container_name: ${APP_NAME}-wordpress
restart: always
ports:
- 80
environment:
WORDPRESS_DB_HOST: ${DB_HOST}
WORDPRESS_DB_USER: ${DB_USER}
WORDPRESS_DB_PASSWORD: ${DB_PASS}
WORDPRESS_DB_NAME: ${DB_NAME}
WORDPRESS_TABLE_PREFIX: ${DB_TABLE_PREFIX}
WORDPRESS_CONFIG_EXTRA: |
/* Direct FTP */
define('FS_METHOD', 'direct');
volumes:
- ./src/project1:/var/www/html/wp-content:rw
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
depends_on:
- db
wordpress_2:
build: .
container_name: ${APP_NAME}-wordpress
restart: always
ports:
- 80
environment:
WORDPRESS_DB_HOST: ${DB_HOST}
WORDPRESS_DB_USER: ${DB_USER}
WORDPRESS_DB_PASSWORD: ${DB_PASS}
WORDPRESS_DB_NAME: ${DB_NAME}
WORDPRESS_TABLE_PREFIX: ${DB_TABLE_PREFIX}
WORDPRESS_CONFIG_EXTRA: |
/* Direct FTP */
define('FS_METHOD', 'direct');
volumes:
- ./src/project2:/var/www/html/wp-content:rw
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
depends_on:
- db
db:
image: mariadb
container_name: ${APP_NAME}-db
volumes:
- './database/db:/var/lib/mysql:delegated'
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASS}
restart: always
ports:
- '3306:3306'
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: ${APP_NAME}-phpmyadmin
volumes:
- ./config/phpmyadmin.ini:/usr/local/etc/php/conf.d/phpmyadmin.ini
environment:
PMA_HOST: ${DB_HOST}
PMA_PORT: 3306
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
ports:
- '8080:80'
links:
- db:db
I know wordpress has some issues if you don't put the domain. In any case, you can use a NGINX in front of all wordpress as a reverse proxy like this Example.
Feel free to ask!

Cannot connect to second docker container - port error

I have two directories each running an identical docker build except for the allocated ports. I cannot connect to one of the containers in my localhost.
After running the docker ps command I see that 80/tcp being prepended to my second recipe-blog container. Below is my yml file, its nothing crazy, just setting up a database running php my admin and have it connect to a WordPress install. I also attached an image of the docker ps command.
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '9090:90'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: password
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:5.3.0
ports:
- '9000:90'
restart: always
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data: {}
wp-content:
Once again - finalsandbox_ I can connect to fine, I cannot connect to recipe-blog_. The only difference between the two yml files is that for the recipe-blog_ I changed the ports to be 9090:90 instead of 8080:80 and 9000:90 instead of 8000:80.
Thanks in advance.
It works for me, example
version: "3"
services:
# Database
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wpsite
# phpmyadmin
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
restart: always
ports:
- '9090:80'
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: wordpress
networks:
- wpsite
# Wordpress
wordpress:
depends_on:
- db
image: wordpress:5.3.0
ports:
- '9000:80'
restart: always
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wpsite
networks:
wpsite:
volumes:
db_data: {}
wp-content:
Change all the ports assigned by 90 to 80.
docker-compose up -d
with your we-browser favorite, connect to http://127.0.0.1:9000 and http://127.0.0.1:9090

phpMyAdmin not accessible from Docker

I have the following docker-compose file:
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./vDocker/php/php.Dockerfile
image: digitalocean.com/php
container_name: app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./vDocker/.env:/var/www/.env
- ./vDocker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: webserver
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes:
- ./:/var/www
- ./vDocker/.env:/var/www/.env
- ./vDocker/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql:5.7.22
container_name: db
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: translive
MYSQL_ROOT_PASSWORD: gio123
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- dbdata:/var/lib/mysql/
- ./vDocker/mysql/my.cnf:/etc/mysql/my.cnf
networks:
- app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
depends_on:
- db
restart: unless-stopped
tty: true
ports:
- "8080:8080"
environment:
PMA_PORT: 3306
PMA_HOST: db
PMA_USER: root
PMA_PASSWORD: gio123
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
I have four services. Three of them work fine and no problem at all, but phpmyadmin doesn't work at all. The thing is all is work fine, database, webserver, but not phpMyAdmin. When I try to go to localhost:8080, it says site can't be reached. I don't know what's going on. What am I missing?

Resources