Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker network link to 2 or multiple containers

Tags:

docker

port

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

like image 514
Andreas Reiff Avatar asked Sep 23 '15 16:09

Andreas Reiff


People also ask

Which is the command line option getting used to link 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.

Can 2 Docker containers talk to 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.

Do containers share network?

Container-defined Networking: A container can share the address and network configuration of another container. This enables process isolation between containers, where each container runs one service but where services can still communicate with one another on 127.0.

Can one Docker image have multiple containers?

The docker-compose. yml file allows you to configure and document all your application's service dependencies (other services, cache, databases, queues, etc.). Using the docker-compose CLI command, you can create and start one or more containers for each dependency with a single command (docker-compose up).


3 Answers

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.

like image 129
saikarthik parachi Avatar answered Oct 16 '22 22:10

saikarthik parachi


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

like image 22
Michael Avatar answered Oct 16 '22 20:10

Michael


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
like image 29
Badr Bellaj Avatar answered Oct 16 '22 22:10

Badr Bellaj