search

Home  >  Q&A  >  body text

docker 怎么在主机转发请求到各个容器里进行处理?

刚开始折腾docker没几天。

比如我做一个web服务器,nginx放在主机里,在docker里建了几个php-fpm的容器,打算用nginx转发到容器里做负载均衡(测试,搞着玩的)。然后发现,在nginx的配置里写的转发地址,不好写啊。每次重启容器,都会自动重新分配一个新的ip,那我在主机nginx配置里写的转发地址,就会出错。。。

在网上看了很多,都说可以把容器弄成固定ip的,但各种麻烦,要自己弄网卡之类的,而且也不完美,据说重启后也是会重新分配,还需要再执行一次。。。

大家有没有什么好的思路,求解答

大家讲道理大家讲道理2774 days ago682

reply all(3)I'll reply

  • 阿神

    阿神2017-04-25 09:03:25

    Two methods:

    1. The php container uses -p to shoot the port to the host, and then nginx forwards it to localhost:[mapped port]

    2. Put nginx in the container, link the php container, and forward nginx to linkname:port

    reply
    0
  • 阿神

    阿神2017-04-25 09:03:25

    LZ seems to have to run nginx on the host. Here is something that can dynamically change upstream. Combined with docker inspect you can get it:

    https://github.com/yzprofile/...

    reply
    0
  • PHP中文网

    PHP中文网2017-04-25 09:03:25

    docker inspect --format='{{.NetworkSettings.IPAddress}}' [CONTAINER_ID]
    

    You can get the IP with this command, and write an automated script to generate nginx config yourself

    reply
    0
  • Cancelreply