在Docker上游找不到Docker + Nginx主机 [英] Docker + Nginx host not found in upstream in docker
问题描述
我正在尝试使用network_mode:docker-compose中的host运行Nginx和PHP-FPM容器,当将network_mode定义为 bridge时,它可以正常工作,但是当将network_mode定义为 host时,我会收到:
I'm trying to run Nginx and PHP-FPM containers with network_mode: host in docker-compose, when defining a network_mode as "bridge" it works fine, but when defining network_mode as "host" I receive :
nginx:在/etc/nginx/conf.d/ui.conf:10
nginx: [emerg] host not found in upstream "ui" in /etc/nginx/conf.d/ui.conf:10
将network_mode设置为网桥时,一切正常,不幸的是,我需要将其设置为主机,因为我需要访问主机网络才能访问ueye摄像机。
Everything works fine when setting network_mode to bridge, unfortunately I need to set it to host, as I need access to host network so I can access ueye camera.
这是我的docker-compose文件
This is my docker-compose file
version: '3'
services:
nginx:
image: nginx:alpine
container_name: nginx
tty: true
ports:
- "80:80"
- "443:443"
- "8000:8000"
volumes:
- ./../../ui/:/var/www/
- ./nginx/:/etc/nginx/conf.d/
network_mode: host
depends_on:
- ui
restart: always
ui:
build:
context: ./../../ui
dockerfile: Dockerfile
volumes:
- ./../../ui/:/var/www/
container_name: ui
tty: true
environment:
SERVICE_NAME: ui
working_dir: /var/www
network_mode: host
ports:
- "9000:9000"
nginx conf文件
The nginx conf file
server {
listen 80;
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass ui:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
推荐答案
使用主机网络模式,您的容器会将Docker主机视为localhost&而不是容器本身,因为它使用的是主机网络。尝试在nginx conf中将 ui
替换为 localhost
-
While using host network mode, your container will see Docker host as localhost & not the container itself because it's using the host network. Try replacing ui
with localhost
in your nginx conf -
fastcgi_pass localhost:9000;
这篇关于在Docker上游找不到Docker + Nginx主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!