简体   繁体   English

Docker容器在主机中无法访问

[英]Docker container not accessible in host machine

Some of my containers are running a web server (PHP, NodeJS) in production. 我的一些容器正在生产中运行Web服务器(PHP,NodeJS)。

If we take the example of the NodeJS container, this is its configuration 如果我们以NodeJS容器为例,这就是它的配置

docker-compose.yml 泊坞窗,compose.yml

version: '2'
services:
  webapp:
    build: .
    volumes_from:
     - data
    links:
     - db:db
     - redis:redis
    ports:
      - '8080:3000'
    restart: always

  ...

With this configuration, I'm able to access to my webapp through http://example.com:8080/ but I cannot access on my host machine with 使用此配置,我可以通过http://example.com:8080/访问我的webapp,但我无法在我的主机上访问

$ curl localhost:8080
(Time-out)

I have a Nginx server on my host with this configuration: 我的主机上有一个Nginx服务器,配置如下:

example.com.conf example.com.conf

upstream webapp {
   server 127.0.0.1:8080;
}

server {
  listen 80;
  listen [::]:80;

  server_name example.com;

  location / {
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   HOST $http_host;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-NginX-Proxy true;
    proxy_redirect off;

    proxy_pass http://webapp;
  }

}

But acceding to http://example.com/ throw a 504 Gateway Time-out. 但是加入http://example.com/抛出504网关超时。

If I try to change port configuration in my docker-compose.yml by '127.0.0.1:8080:3000' the problem still here but cannot access to http://example.com (and http://example.com:8080 but it is the result I want). 如果我尝试通过'127.0.0.1:8080:3000'更改docker-compose.yml的端口配置,问题仍然存在,但无法访问http://example.com (和http://example.com:8080但这是我想要的结果)。

This is very strange and I don't have the problem running my app on a fresh server. 这很奇怪,我没有在新服务器上运行我的应用程序的问题。 I'm searching for the root of this issue. 我正在寻找这个问题的根源。

Thanks for your support. 谢谢你的支持。

EDIT: 编辑:

$ netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address        State       PID/Program name
tcp        0      0 127.0.0.11:59712        0.0.0.0:*               LISTEN      -               
udp        0      0 127.0.0.11:33293        0.0.0.0:*                           -               

Web server has to be listening on port 3000 but I have Web服务器必须在端口3000上侦听,但我有

$ docker-compose logs
webapp_1  | Express server listening on port 3000 in production mode

The server is also running... 服务器也在运行......

$ docker port webapp_1
3000/tcp -> 127.0.0.1:8080

Edit: This should work similar to how you have it, did you try changing: proxy_set_header HOST $http_host; 编辑:这应该与您的方式类似,您是否尝试更改: proxy_set_header HOST $http_host; to proxy_set_header HOST $host; proxy_set_header HOST $host; in your nginx config? 在你的nginx配置?

You could put nginx in your Docker Compose file and let Compose use its own network. 您可以将nginx放入Docker Compose文件中,让Compose使用自己的网络。 If you want to add items in another network, you can specify a custom network, see: https://docs.docker.com/compose/networking/ 如果要在其他网络中添加项目,可以指定自定义网络,请参阅: https//docs.docker.com/compose/networking/

Otherwise, you can look at the output of ifconfig , etc on your host and see which IP and network Compose is using and what the host is listening to. 否则,您可以查看主机上ifconfig等的输出,并查看正在使用的IP和网络Compose以及主机正在侦听的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM