gpt4 book ai didi

docker - 如何将特定 IP 分配给容器并使其可在 VM 主机之外访问?

转载 作者:行者123 更新时间:2023-12-04 20:36:10 26 4
gpt4 key购买 nike

我希望让我的两个容器在 VM 主机之外的单独特定 IP 地址( 192.168.0.222192.168.0.227 )上可用,而无需端口映射。这意味着我希望使用其 IP 直接访问容器上的任何端口。我已经在 VM 主机之外的网络中运行了 192.168.0.1–192.168.0.221 范围内的机器。

现在 Docker 1.10.0 是否可以实现,如果可以,如何实现?

我在 OS X 10.11 上使用 docker version 1.10.0, build 590d5108docker-machine version 0.6.0, build e27fb87 , 使用 boot2docker/VirtualBox 驱动程序。

我一直试图弄清楚这一点,但运气不佳,我阅读了以下问题和答案:

  • How to assign static public IP to docker container
  • How to expose docker container's ip and port to outside docker host without port mapping?
  • How can I make other machines on my network access my Docker containers (using port mapping)?
  • 最佳答案

    根据 Jessie Frazelle ,这现在应该是可能的。
    见“IPs for all the Things

    This is so cool I can hardly stand it.

    In Docker 1.10, the awesome libnetwork team added the ability to specifiy a specific IP for a container. If you want to see the pull request it’s here: docker/docker#19001.

    # create a new bridge network with your subnet and gateway for your ip block
    $ docker network create --subnet 203.0.113.0/24 --gateway 203.0.113.254 iptastic

    # run a nginx container with a specific ip in that block
    $ docker run --rm -it --net iptastic --ip 203.0.113.2 nginx

    # curl the ip from any other place (assuming this is a public ip block duh)
    $ curl 203.0.113.2

    # BOOM golden
    这确实说明了 new docker run --ip option您现在在 docker network connect 中看到的.

    If specified, the container's IP address(es) is reapplied when a stopped container is restarted. If the IP address is no longer available, the container fails to start.

    One way to guarantee that the IP address is available is to specify an --ip-range when creating the network, and choose the static IP address(es) from outside that range. This ensures that the IP address is not given to another container while this container is not on the network.

    $ docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 multi-host-network

    $ docker network connect --ip 172.20.128.2 multi-host-network container2

    “使可访问”部分将像往常一样涉及 port forwarding .

    关于docker - 如何将特定 IP 分配给容器并使其可在 VM 主机之外访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35218621/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com