Andreas Reiff
Andreas Reiff

Reputation: 8404

docker network link to 2 or multiple containers

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

Upvotes: 9

Views: 17647

Answers (3)

saikarthik parachi
saikarthik parachi

Reputation: 335

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.

Upvotes: 31

Badr Bellaj
Badr Bellaj

Reputation: 12841

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

Upvotes: 1

Michael
Michael

Reputation: 8663

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

Upvotes: 5

Related Questions