Nginx反向代理到主机中的应用
I have an app that is running outside Docker on port 5000. I am trying to run a reverse proxy in nginx via Docker compose but am unable to communicate with the host's port 5000. In my docker-compose.yml file I have:
ports:
- 80:80
- 443:443
- 5000:5000
When I try to run this I get:
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
If I comment out - 5000:5000
I get:
[error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream
How do I connect to an already running app in the Host from a Docker nginx container?
EDIT:
My nginx.conf file
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;
}
}
}
The response when I try to curl localhost is 502 Bad Gateway
. The app itself and curl 127.0.0.1:5000 responds fine from the host.
EDIT 2:
I have also tried the solution found here but I get nginx: [emerg] host not found in upstream "docker"
. Docker is my host's hostname.
EDIT 3: My docker-compose.yml
version: '3'
services:
simple:
build: ./simple
container_name: simple
ports:
- 80:80
- 443:443
My Dockerfile:
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
EDIT:
I am getting the computer host via the "hostname" command in linux.
我有一个在Docker外的端口5000上运行的应用程序。我试图通过以下方式在nginx中运行反向代理 Docker组成但无法与主机的端口5000通信。在我的docker-compose.yml文件中,我有: p>
端口:
-80:80
-443 :443
-5000:5000
code> pre>
当我尝试运行此程序时,我得到: p>
错误: for nginx无法启动服务nginx:驱动程序无法对端点nginx上的外部连接进行编程(374026a0d34c8b6b789dcd82d6aee6c4684b3201258cfbd3fb18623c4101):启动Userland代理时出错:监听tcp 0.0.0.0:5000:绑定:地址已在使用中
code> pre>
如果我注释掉-5000:5000 code>,我得到: p>
[错误] 6#6:* 1 connect()失败 (111:连接被拒绝)连接到上游时
code> pre>
如何从Docker nginx容器连接到主机中已经运行的应用程序? p> \ n
编辑: p>
我的nginx.conf文件 p>
用户www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
上游mysite {
服务器0.0.0.0 :5000;
}
服务器{
监听80;
server_name localhost;
位置/ {
proxy_pass http:// mysite;
}
}
}
pre>
我尝试卷曲localhost时的响应是 502 Bad Gateway code>。 该应用程序本身和curl 127.0.0.1:5000从主机响应良好。 p>
编辑2:
我也尝试过在此处找到解决方案,但我得到了 nginx:[emerg]主机未在上游“ 搬运工“代码>。 Docker是我的主机的主机名。 p>
编辑3:
我的docker-compose.yml p>
版本:'3'
服务:\ n简单:
构建:./simple
container_name:简单
端口:
-80:80
-443:443
code> pre>
我的Dockerfile : p>
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80443
CMD [“ nginx”,“ -g”,“守护程序 关闭;“,”-c“,” / etc / nginx / nginx.conf“]
code> pre>
EDIT: p>
我正在通过linux中的“主机名”命令来获得计算机主机。 p>
div>
The problem lies with 0.0.0.0:5000. Since Nginx is running inside the docker, it tries to find this address inside docker machine but fails since there is nothing running on 0.0.0.0:5000 inside docker.
So in order to resolve this
- You need to give it an address that it can reach. Solving it requires that you first run your application at 0.0.0.0:5000 on your host machine i.e you should be able to open your application at 0.0.0.0:5000 from your browser.
- Find your IP address. once you get your IP address you should be able to open you application through ip_address:5000. since your docker and host share the same network this address can be reached from docker also
- Now, replace the 0.0.0.0:5000 in your Nginx conf file with this ip_address:5000. you would be able to serve your application