9

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

Community
  • 1
  • 1
Andreas Reiff
  • 7,961
  • 10
  • 50
  • 104
  • 1
    https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname/30550990#30550990 – xuhdev Sep 23 '15 at 18:49
  • 2
    I guess you can use --link multiple times to link to multiple containers. I have used the same many times. – Sachin Malhotra Sep 24 '15 at 03:45

3 Answers3

31
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.

Badr Bellaj
  • 11,560
  • 2
  • 43
  • 44
5

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.

Docker Networking

Michael
  • 8,229
  • 4
  • 17
  • 14
1

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
Badr Bellaj
  • 11,560
  • 2
  • 43
  • 44