简体   繁体   English

如何在不重新启动的情况下更改docker容器的源代码?

[英]How to change docker container's source code without restarting it?

I have an application where I am using two docker containers. 我有一个应用程序,我使用两个docker容器。 Say container A and B . 说容器AB. Now B is dependent upon A. So when I start container 'B', I do mention the ip address of container 'A' in the command line. 现在B依赖于A.所以当我启动容器'B'时,我确实在命令行中提到了容器'A'的ip地址。

Now I have a code change in the container 'A'. 现在我在容器'A'中进行了代码更改。 If I have to restart the container 'A' after change the source code then after restarting A's ip address will change. 如果我必须在更改源代码后重新启动容器'A',那么在重新启动后,A的IP地址将会改变。 For that reason container 'B' will lose connection to 'A' and I have to restart 'B' with new ip address of 'A'. 因此容器'B'将失去与'A'的连接,我必须用新的IP地址'A'重新启动'B'。

Is there any docker approach where I can reserve the ip address for container 'A' always? 是否有任何码头工作方法,我可以保留容器'A'的IP地址? or can I make the code changes without restarting the container 'A'? 或者我可以在不重新启动容器'A'的情况下更改代码吗? Please suggest. 请建议。

You would need a service discovery like consul , combined with registrator in order to record A (in consul) each time it starts. 您需要像consul这样的服务发现,并与registrator结合使用,以便每次启动时记录A (在领事中)。

Then B could query consul in order to get A up-to-date IP address. 那么B可以为了得到查询领事A上最新的IP地址。

See " Automatic Docker Service Announcement with Registrator " 请参阅“ 使用Registrator自动Docker服务公告

If your containers are not managed by the same docker daemon (meaning you are in a multi-hosts multi-nodes environment), you would use docker swarm (still in conjunction with consul ): 如果您的容器不是由同一个docker守护程序管理(意味着您处于多主机多节点环境中),那么您将使用docker swarm (仍与consul一起使用):

Two ways 两种方式

Custom IP's 自定义IP

You can customise Docker networking inside a container far beyond what Docker allows on the command line with pipework by Jérôme Petazzoni . 您可以在容器内自定义Docker网络,远远超出Docker在命令行中允许的JérômePetazzoni pipework

It allows you to set the networking you would like so you could create a bridge, lets be inventive and call it br1 , for your containers and set specific IP's on each container or even use a dhcp server to allocate the IP's. 它允许您设置您想要的网络,以便您可以创建桥接,让我们有创造力并将其称为br1 ,为您的容器设置并在每个容器上设置特定的IP,甚至使用dhcp服务器来分配IP。 This is a static config: 这是一个静态配置:

Start the aa container and add an IP on our bridge, br1 as eth0 启动aa容器并在我们的桥上添加一个IP, br1eth0

→ docker run -d --net=none --name aa busybox cat
→ pipework br1 -eth0 aa 192.168.17.4/24

Start the bb container and add 192.168.17.5 on br1 启动bb容器并在br1上添加192.168.17.5

→ docker run -d --net=none --name bb busybox cat
→ pipework br1 -eth0 bb 192.168.17.5/24

Now we know IP address of a container all the time 现在我们一直知道容器的IP地址

→ docker exec bb ping 192.168.17.4

Docker hosts file Docker托管文件

Docker 1.8 enabled /etc/hosts container name sharing by default. Docker 1.8默认启用/etc/hosts容器名称共享。

Docker 1.9 disabled this on the default network/bridge docker0 but will share container name information on a custom network Docker 1.9在默认网络/网桥docker0上禁用了此功能,但将在自定义网络上共享容器名称信息

Create the network 创建网络

→ docker create network shared
ce8fb4c6d6d379e87314952f4dfbcdcd6cf6c03416431721bb8f6001492575ea

Run a container named aa 运行名为aa的容器

→ docker run --name aa --net=shared -ti -d busybox sleep 5
754c7716cee65526cb2284066ccf2e495d494f5c351e281a89672aa5bdf1d84d

Ping by container name aa 按容器名称aa Ping

→ docker run --name bb --net=shared -ti --rm busybox ping aa
PING aa (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=1.354 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.081 ms
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.090 ms
64 bytes from 172.18.0.2: seq=3 ttl=64 time=0.114 ms

--- aa ping statistics ---
8 packets transmitted, 4 packets received, 50% packet loss
round-trip min/avg/max = 0.081/0.409/1.354 ms

Now the container has stopped after 5 seconds 现在容器在5秒后停止了

→ docker run --name bb --net=shared -ti --rm busybox ping aa
ping: bad address 'aa'

Docker multi node hosts file Docker多节点主机文件

Docker natively supports multiple host networking via its overlay network driver . Docker本身通过其overlay网络驱动程序支持多个主机网络。 This does require an external service discovery agent to provide the kay/value store service but it means you can do this over multiple docker nodes too. 这确实需要外部服务发现代理来提供kay /值存储服务,但这意味着您也可以在多个docker节点上执行此操作。

you do this with the --volumes-from flag. 你用--volumes-from标志做到这一点。 It would look like this: 它看起来像这样:

$ docker run a --name a
$ docker run b --name b --volumes-from a

Docker will transparently handle the networking for you. Docker将透明地为您处理网络。

https://docs.docker.com/engine/userguide/dockervolumes/ https://docs.docker.com/engine/userguide/dockervolumes/

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

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