use*_*084 0 webserver reverse-proxy nginx go docker
我有一个在 Docker 5000 端口上运行的应用程序。我试图通过 Docker compose 在 nginx 中运行反向代理,但无法与主机的端口 5000 通信。在我的 docker-compose.yml 文件中,我有:
ports:
- 80:80
- 443:443
- 5000:5000
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我得到:
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
Run Code Online (Sandbox Code Playgroud)
如果我注释掉,- 5000:5000我会得到:
[error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream
Run Code Online (Sandbox Code Playgroud)
如何从 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试 curl localhost 时的响应是502 Bad Gateway. 应用程序本身和 curl 127.0.0.1:5000 从主机响应良好。
编辑 2:我也尝试过这里找到的解决方案,但我得到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
Run Code Online (Sandbox Code Playgroud)
我的 Dockerfile:
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
Run Code Online (Sandbox Code Playgroud)
编辑:
我正在通过 linux 中的“主机名”命令获取计算机主机。
rdR*_*ul 7
问题在于 0.0.0.0:5000。由于 Nginx 在 docker 内运行,它试图在 docker 机器内找到这个地址但失败了,因为在 docker 内 0.0.0.0:5000 上没有任何东西在运行。
所以为了解决这个问题
172.17.0.1是主机上运行的 docker 容器可用的默认主机 IP。只需在 nginx conf 文件中使用172.17.0.1:5000,您就应该能够连接到在容器外部的主机上运行的应用程序。我的 docker 版本是19.03.12,我测试了相同的版本。
| 归档时间: |
|
| 查看次数: |
3978 次 |
| 最近记录: |