Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking containers in Docker

Tags:

docker

Docker allows you to link containers by name.

I have two questions on this:

  • Supposed A (client) is linked to B (service), and B's port is exposed dynamically (i.e. the actual host port is determined by Docker, not given by the user). What happens if B goes down and is being restarted?
    • Does Docker update the environment variable on A?
    • Does Docker assign the very same port again to B?
    • Is A link to B broken?
    • …?
  • Besides that, it's quite clear that this works fine if both containers are run on the same host machine. Does linking containers also work across machine boundaries?
like image 342
Golo Roden Avatar asked Feb 04 '14 12:02

Golo Roden


People also ask

Can Docker containers interact with each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

What is links in Docker compose?

According to the Docker Compose's compose-file documentation: depends_on - Express dependency between services. links - Link to containers in another service and also express dependency between services in the same way as depends_on.

Which command line is used to connect two containers together?

For an easy solution you could use Docker-compose . in you compose file (docker-compose. yml) use the option links Link to containers in another service. Either specify both the service name and a link alias (SERVICE:ALIAS), or just the service name.


2 Answers

Have you looked into the ambassador pattern?

It's ideal for this concept where you may want App server linked to DB server but if you take DB server down then App server needs to be restarted also.

http://docs.docker.io/en/latest/use/ambassador_pattern_linking/

like image 109
TheStoneFox Avatar answered Oct 29 '22 23:10

TheStoneFox


I would say: try ;).

At the moment, docker as no control whatsoever on the process once started as it execve(3) without fork. It is not possible to update the env, that's why the links need to be done before the container runs and can't be edited afterward.

Docker will try to reassign the same port to B, but there is no warranty as an other container could be using it.

What do you mean by 'broken'? If you disabled the networking between unlinked container, it should still be working if you stop/start a container.

No, you can't link container across network yet.

like image 33
creack Avatar answered Oct 29 '22 23:10

creack