我希望能够访问同一主机上包含 Web 应用程序的多个容器。
当我想访问主机(通过他的 ip 地址)或容器(例如通过 host_ip_adress/container1)时,我会从 Nginx 得到一个 503 错误。我想要的是通过 ip_addrress_host/container1 访问我的 container1。
我在互联网上找到的解决方案是设置一个 nginx-proxy 前端服务器(来源:https ://blog.florianlopes.io/host-multiple-websites-on-single-host-docker/ )
我的码头工人撰写文件:
version: '2'
services:
nginx-proxy:
image: jwilder/nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock
site_a:
image: php:7.0-apache
expose:
- "80"
environment:
- VIRTUAL_HOST=192.168.12.28/container1
- VIRTUAL_PORT=80
volumes:
- ./php:/var/www/html
site_b:
image: php:7.0-apache
expose:
- "80"
environment:
- VIRTUAL_HOST=192.168.12.28/container2
- VIRTUAL_PORT=80
volumes:
- ./php:/var/www/html
我用命令运行它:
docker-compose up
我在/etc/hosts 文件中的条目:
127.0.1.1 container1
127.0.0.1 container2
我从外部提出请求时看到的日志:
nginx-proxy_1 | nginx.1 | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /container1 HTTP/1.1" 503 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
nginx-proxy_1 | nginx.1 | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /favicon.ico HTTP/1.1" 503 615 "http://192.168.12.28/container1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
谢谢你的帮助,对不起我的英语不好!:-)
编辑:我试图将 nginx-proxy 容器和我的 Web 应用程序容器放在与 docker hub (https://hub.docker.com/r/jwilder/nginx-proxy/)中解释的相同的“docker网络”中,但是当我愿意 :
docker-compose up
我有一个错误:
ERROR: Service "site_b" uses an undefined network "proxy-nginx"
编辑 2:
这是我在开始时 nginx-proxy 容器的日志:
forego | starting dockergen.1 on port 5000
forego | starting nginx.1 on port 5100
dockergen.1 | 2017/10/25 14:01:53 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
dockergen.1 | 2017/10/25 14:01:53 Running 'nginx -s reload'
nginx.1 | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container1" has suspicious symbols in /etc/nginx/conf.d/default.conf:60
nginx.1 | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container2" has suspicious symbols in /etc/nginx/conf.d/default.conf:74
dockergen.1 | 2017/10/25 14:01:54 Watching docker events
dockergen.1 | 2017/10/25 14:01:54 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'
编辑 3:
所以,现在,我的 nginx 配置看起来像:
server {
listen 192.168.12.28;
server_name 192.168.12.28;
access_log /var/log/nginx/host.access.log;
location / {
proxy_http_version 1.1;
proxy_pass http://172.23.0.3:80;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering Off;
proxy_cache_bypass $http_upgrade;
}
location /container1/ {
proxy_http_version 1.1;
proxy_pass http://172.23.0.3:80;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering Off;
proxy_cache_bypass $http_upgrade;
}
location /container2/ {
proxy_http_version 1.1;
proxy_pass http://172.23.0.2:80;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering Off;
proxy_cache_bypass $http_upgrade;
}
}
当我去http://192.168.12.28/container1
我的日志是:
site_a_1 | 172.23.0.1 - - [26/Oct/2017:09:22:40 +0000] "GET /container1/ HTTP/1.1" 404 449 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
当我去http://192.168.12.28/container2
我的日志是:
site_b_1 | 172.23.0.1 - - [26/Oct/2017:09:22:50 +0000] "GET /container2/ HTTP/1.1" 404 449 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
对于两者,响应都是 404 not found。似乎它查看了我的容器服务器的“/container1”或“/container2”路径?如果是真的,我可以强迫他照顾 /var/www/html/ 吗?