docker network link to 2 or multiple containers
Solution 1
docker run -d --link node1:node1 --link node2:node2 --link node3:node3 -p hostport:containerport your-image
I run the command above and it works.
Solution 2
Alternatively, you can turn on inter-container communication by adding --icc=true to the docker daemon's command-line, and you won't have to link the containers, just access them using the Docker Host's IP address and the containers' published ports.
Solution 3
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.
container_name:
links:
- node1
- node2
- node3:alias3
- noden
Andreas Reiff
I like to develop and lots of other stuff. I also like to tell other people what to do. :) In a helpful way, of couse. Life is good. Last jobs include UI Automation, TFS Automation integration (focus on Coded UI), Hardware Interfacing and Project Management. Technology Focus is on C#, also I am still strong in C++/C (did a lot a lot earlier on), on Windows Desktop. Looking into web/mobile right now out of interest. Please also have a look at my website at http://www.andreas-reiff.de . I am working as a freelancer right now.
Updated on July 27, 2022Comments
-
Andreas Reiff almost 2 years
As per docker link docs I can only --link to one (already running) container to access internal ports of that container.
How can I link one container to 2 or more other containers? (MongoDB and another web service in my case.)
(Right now I am exposing ports of second container to host and then accessing via host:port, also possible workaround might be Let two Containers getting linked to eachother .)