我用自己的nginx/php-fpm创建了两个容器laravel webapp(project 1和project 2),并将它们与traefik容器链接。每个项目都有自己的文件夹和docker-compose.yaml,并正确配置了traefik标签。
多亏了traefik,我期望的是,当我访问project1.laravel.test
时,我会查看项目1的内容,当我访问project2.laravel.test
时,我会查看项目2的内容。
问题是当我访问project1.laravel.test
时,交替显示project 1的内容,其他时候显示project 2的内容。如果我关闭project 2的容器,project 1可以正常工作。看起来traefik被配置为负载均衡器,但我不明白问题在哪里。
如何复制我的问题?
1. git clone https://github.com/gtoto007/traefik-laravel-docker
2. cd traefik-laravel-docker
3. docker-compose -f traefik/docker-compose.yaml up -d
4. docker-compose -f project1/docker-compose.yaml up -d
5. docker-compose -f project1/docker-compose.yaml up -d
在主机文件中
127.0.0.1 traefik.laravel.test
127.0.0.1 project1.laravel.test
127.0.0.1 project2.laravel.test
我的DOCKER合成文件
下面为简单起见,我把三个docker-compose项目1,项目2和traefik:
./traefik/ Docker -编写.yaml
version: "3.3"
networks:
my-network:
external: true
services:
traefik:
image: "traefik:v2.9"
container_name: "traefik"
networks:
- my-network
command:
- "--api"
- "--providers.docker.exposedbydefault=false"
- "--api.insecure=true"
- "--accesslog.filepath=/data/access.log"
# entrypoints
- "--entrypoints.http.address=:80"
- "--entrypoints.https.address=:443"
- "--entrypoints.traefik.address=:8888"
ports:
- "80:80"
- "443:443"
- "8888:8888"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
labels:
- "traefik.enable=true"
- "traefik.http.routers.traefik.rule=Host(`traefik.laravel.test`)"
- "traefik.http.routers.traefik.entrypoints=traefik"
./项目1/停靠-编写.yaml
version: '3.8'
networks:
my-network:
external: true
services:
nginx:
build:
dockerfile: docker/nginx/Dockerfile
context: ./
image: my-nginx
volumes:
- ./docker/nginx/conf.d:/etc/nginx/conf.d/
- my-data_project1:/var/www
networks:
- my-network
depends_on:
- php-fpm
labels:
- "traefik.enable=true"
- "traefik.http.routers.project1.rule=Host(`project1.laravel.test`)"
- "traefik.docker.network=my-network"
php-fpm:
build:
dockerfile: docker/php/Dockerfile
context: ./
image: my-php-fpm
ports:
- "5173:5173"
volumes:
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
- my-data_project1:/var/www
networks:
- my-network
project1:
build:
dockerfile: docker/project1/Dockerfile
context: ./
image: project1:1.0
volumes:
- my-data_project1:/var/www
networks:
- my-network
volumes:
my-data_project1:
./项目2/停靠-编写.yaml
version: '3.8'
networks:
my-network:
external: true
services:
nginx:
build:
dockerfile: docker/nginx/Dockerfile
context: ./
image: my-nginx
volumes:
- ./docker/nginx/conf.d:/etc/nginx/conf.d/
- my-data_project2:/var/www
networks:
- my-network
depends_on:
- php-fpm
labels:
- "traefik.enable=true"
- "traefik.http.routers.project2.rule=Host(`project2.laravel.test`)"
- "traefik.docker.network=my-network"
php-fpm:
build:
dockerfile: docker/php/Dockerfile
context: ./
image: my-php-fpm
ports:
- "5174:5173"
volumes:
- ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
- my-data_project2:/var/www
networks:
- my-network
project2:
build:
dockerfile: docker/project2/Dockerfile
context: ./
image: project2:1.0
volumes:
- my-data_project2:/var/www
networks:
- my-network
# - ./:/var/www
volumes:
my-data_project2:
1条答案
按热度按时间e5nqia271#
我解决了这个问题。这个问题是由容器名称冲突引起的,因为项目共享相同的网络,并且一些容器具有相同的默认主机名。
例如,如果执行
ping nginx
,有时会获得project 1的nginx服务的ip,有时会获得project 2的nginx服务的ip,因为这两个服务使用相同的主机名。我通过docker-compose的
Hostname
属性用唯一的主机名覆盖每个容器的主机名来修复它,并更正了每个项目的nginx配置。你可以看我的固定在这个提交:https://github.com/gtoto007/traefik-laravel-docker/commit/707925465b979168448128b8b307660bde2b5aeb