简体   繁体   English

如何将Docker容器的IP地址动态映射到另一个容器

[英]How to map the IP address of docker container to another container dynamically

I have few docker containers running in a host. 我在主机中运行着几个Docker容器。 I have a scenario where i need to map the IP address of one container to the configuration file available in another container. 我有一种情况,我需要将一个容器的IP地址映射到另一个容器中可用的配置文件。 I know that i can get the IP address of a container using the docker inspect [container] command. 我知道我可以使用docker inspect [container]命令获取容器的IP地址。 I assumes the IP address is assigned dynamically every time i restart the container. 我假设每次重新启动容器时都会动态分配IP地址。 So every time i need to change this IP address in another container. 因此,每次我需要在另一个容器中更改此IP地址时。 Is it possible to have a static ip for container or can i use the ip address of the host instead of ip address of the container. 是否可以为容器使用静态IP,或者我可以使用主机的IP地址代替容器的IP地址。 I am new to docker world. 我是docker world的新手。

Also i have a case where i need to map the IP address to a container that is running in another host. 另外,我有一种情况,我需要将IP地址映射到在另一台主机中运行的容器。 Example, i have a redis container running in HostA and tomcat container running in HostB. 例如,我有一个Redis容器在HostA中运行,而tomcat容器在HostB中运行。 In order to communicate with redis from tomcat, i need to map the ip address and port of redis in the tomcat container. 为了与tomcat的redis通信,我需要在tomcat容器中映射redis的IP地址和端口。 how this can be achieved in docker ? 如何在docker中实现?

Update: linking container with --link parameter is marked as legacy enter link description here 更新:使用--link参数链接容器标记为旧版,请在此处输入链接说明

Warning: The --link flag is a legacy feature of Docker. 警告:--link标志是Docker的旧功能。 It may eventually be removed. 它最终可能会被删除。 Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link . 除非您绝对需要继续使用它,否则建议您使用用户定义的网络来促进两个容器之间的通信,而不要使用--link One feature that user-defined networks do not support that you can do with --link is sharing environmental variables between containers. 用户定义的网络不支持您使用--link进行的一项功能是在容器之间共享环境变量。 However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way. 但是,您可以使用其他机制(例如卷)以更可控的方式在容器之间共享环境变量。

You need docker network. 您需要泊坞窗网络。 Examples below are from official documentation but here it's in few lines: 以下示例来自官方文档,但此处仅几行:

First create docker network eg.: 首先创建docker网络,例如:

docker network create --driver bridge you now you can run containers like in the documentation - Linking containers in user-defined networks : --driver network create --driver桥接器,您现在可以运行文档中的容器-在用户定义的网络中链接容器

docker run --network=your_netowrk -itd --name=container4 --link container5:c5 image
docker run --network=your_netowrk -itd --name=container5 --link container4:c4 image

Original answer: 原始答案:

You can run linked containers like in the documentation - Linking containers : 您可以像在文档中那样运行链接的容器- 链接容器

docker run -itd --name=container4 --link container5:c5 image
docker run -itd --name=container5 --link container4:c4 image

and ping second container by alias (c5) : 通过别名 (c5) ping第二个容器:

docker attach container4

/ # ping -w 4 c5
PING c5 (172.25.0.5): 56 data bytes
64 bytes from 172.25.0.5: seq=0 ttl=64 time=0.070 ms

so you can see it's possible to ping second container with alias - not with IP address . 因此您可以看到使用别名 ping第二个容器( 而不是IP地址)是可能的

To your second Quesiton: It's possible if you have redis and tomcat and they know IP addresses of each other (and they are accessible because they are in some network), so this is almost all about the configuration of network. 对于第二个问题:如果您有redistomcat并且它们彼此知道IP地址(并且由于它们在某些网络中而可以访问),则这是可能的,因此,这几乎是关于网络配置的。

As Vlado pointed out, you should access your containers by hostname and allow Docker to manage that mapping those names to IP addresses. 正如Vlado指出的那样,您应该按主机名访问容器,并允许Docker管理将这些名称映射到IP地址的操作。 There are two options for this legacy links and links in user defined networks . 旧版链接用户定义网络中的 链接有两个选项。 Both descriptions are worth reading, but his paragraph outlines the difference between the two approaches (emphasis mine) 两种描述都值得一读,但他的段落概述了两种方法之间的区别(重点是我的)

Please note that while creating container4, we linked to a container named container5 which is not created yet. 请注意,在创建container4时,我们链接到一个尚未创建的名为container5的容器。 That is one of the differences in behavior between the legacy link in default bridge network and the new link functionality in user defined networks. 这是默认网桥网络中的旧链接与用户定义网络中的新链接功能之间的行为差​​异之一。 The legacy link is static in nature and it hard-binds the container with the alias and it doesn't tolerate linked container restarts. 传统链接本质上是静态的,并且将容器与别名硬绑定,并且不容许链接的容器重新启动。 While the new link functionality in user defined networks are dynamic in nature and supports linked container restarts including tolerating ip-address changes on the linked container. 用户定义网络中的新链接功能本质上是动态的,并且支持链接容器的重新启动,包括允许在链接容器上更改ip地址。

Based on your next question , if you want to run containers on multiple hosts, you will probably want to set up an overlay network . 根据下一个问题 ,如果要在多个主机上运行容器,则可能需要设置覆盖网络 Those definitely don't support legacy links. 那些绝对不支持旧版链接。

The official docs have good examples, including what commands to run, to create links and user defined networks. 官方文档有很好的示例,包括运行哪些命令以创建链接和用户定义的网络。

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

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