【发布时间】:2021-09-17 23:27:32
【问题描述】:
我有一个在 Docker 外部的 5000 端口上运行的应用程序。我正在尝试通过 Docker compose 在 nginx 中运行反向代理,但无法与主机的端口 5000 通信。在我的 docker-compose.yml 文件中:
ports:
- 80:80
- 443:443
- 5000:5000
当我尝试运行它时,我得到:
ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity on endpoint nginx (374026a0d34c8b6b789dcd82d6aee6c4684b3201258cfbd3fb18623c4101): Error starting userland proxy: listen tcp 0.0.0.0:5000: bind: address already in use
如果我注释掉 - 5000:5000 我会得到:
[error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream
如何从 Docker nginx 容器连接到主机中已经运行的应用程序?
编辑:
我的 nginx.conf 文件
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
upstream mysite {
server 0.0.0.0:5000;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://mysite;
}
}
}
当我尝试 curl localhost 时的响应是 502 Bad Gateway。应用程序本身和 curl 127.0.0.1:5000 从主机响应良好。
编辑 2:
我也尝试过solution found here,但我得到了nginx: [emerg] host not found in upstream "docker"。 Docker 是我主机的主机名。
编辑 3: 我的 docker-compose.yml
version: '3'
services:
simple:
build: ./simple
container_name: simple
ports:
- 80:80
- 443:443
我的 Dockerfile:
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
编辑:
我正在通过 linux 中的“主机名”命令获取计算机主机。
【问题讨论】:
标签: docker nginx go webserver reverse-proxy