psaha4
psaha4

Reputation: 339

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

I have an application where I am using two docker containers. Say container A and B. 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.

Now I have a code change in the container 'A'. If I have to restart the container 'A' after change the source code then after restarting A's ip address will change. For that reason container 'B' will lose connection to 'A' and I have to restart 'B' with new ip address of 'A'.

Is there any docker approach where I can reserve the ip address for container 'A' always? or can I make the code changes without restarting the container 'A'? Please suggest.

Upvotes: 4

Views: 1809

Answers (3)

Matt
Matt

Reputation: 74670

Two ways

Custom IP's

You can customise Docker networking inside a container far beyond what Docker allows on the command line with pipework by Jérôme Petazzoni.

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. This is a static config:

Start the aa container and add an IP on our bridge, br1 as eth0

→ 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

→ 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

→ docker exec bb ping 192.168.17.4

Docker hosts file

Docker 1.8 enabled /etc/hosts container name sharing by default.

Docker 1.9 disabled this on the default network/bridge docker0 but will share container name information on a custom network

Create the network

→ docker create network shared
ce8fb4c6d6d379e87314952f4dfbcdcd6cf6c03416431721bb8f6001492575ea

Run a container named aa

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

Ping by container name aa

→ 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

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

Docker multi node hosts file

Docker natively supports multiple host networking via its overlay network driver. 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.

Upvotes: 1

VonC
VonC

Reputation: 1323953

You would need a service discovery like consul, combined with registrator in order to record A (in consul) each time it starts.

Then B could query consul in order to get A up-to-date IP address.

See "Automatic Docker Service Announcement with Registrator"

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):

Upvotes: 1

code_monk
code_monk

Reputation: 10130

you do this with the --volumes-from flag. 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.

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

Upvotes: -1

Related Questions