Connect from one Docker container to another

34,053

Solution 1

[edit 2016]

Direct links are deprecated now. The new way to do link containers is docker network connect. It works quite similar to virtual networks and has a wider feature set than the old way of linking.

First you create your named containers:

docker run --name rabbitmq -d -p :5672 markellul/rabbitmq /usr/sbin/rabbitmq-server
docker run --name celery -it markellul/celery /bin/bash

Then you create a network (last parameter is your network name):

docker network create -d bridge --subnet 172.25.0.0/16 mynetwork

Connect the containers to your newly created network:

docker network connect mynetwork rabbitmq
docker network connect mynetwork celery

Now, both containers are in the same network and can communicate with each other.

A very detailed user guide can be found at Work with networks: Connect containers.

[old answer]

There is a new feature in Docker 0.6.5 called linking, which is meant to help the communication between docker containers.

First, create your rabbitmq container as usual. Note that i also used the new "name" feature which makes life a litte bit easier:

docker run --name rabbitmq -d -p :5672 markellul/rabbitmq /usr/sbin/rabbitmq-server

You can use the link parameter to map a container (we use the name here, the id would be ok too):

docker run --link rabbitmq:amq -i -t markellul/celery /bin/bash

Now you have access to the IP and Port of the rabbitmq container because docker automatically added some environmental variables:

$AMQ_PORT_5672_TCP_ADDR
$AMQ_PORT_5672_TCP_PORT

In addition Docker adds a host entry for the source container to the /etc/hosts file. In this example amq will be a defined host in the container.
From Docker documentation:

Unlike host entries in the /etc/hosts file, IP addresses stored in the environment variables are not automatically updated if the source container is restarted. We recommend using the host entries in /etc/hosts to resolve the IP address of linked containers.

Solution 2

Just get your container ip, and connect to it from another container:

CONTAINER_IP=$(sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CONTAINER_ID)
echo $CONTAINER_IP

Solution 3

When you specify -p 5672, What docker does is open up a new port, such as 49xxx on the host and forwards it to port 5672 of the container.

you should be able to see which port is forwarding to the container by running:

sudo docker ps -a

From there, you can connect directly to the host IP address like so:

amqp://guest@HOST_IP:49xxx

You can't use localhost, because each container is basically its own localhost.

Solution 4

I think you can't connect to another container directly by design - that would be the responsibility of the host. An example of sharing data between containers using Volumes is given here http://docs.docker.io/en/latest/examples/couchdb_data_volumes/, but I don't think that that is what you're looking for.

I recently found out about https://github.com/toscanini/maestro - that might suit your needs. Let us know if it does :), I haven't tried it myself yet.


Edit. Note that you can read here that native "Container wiring and service discovery" is on the roadmap. I guess 7.0 or 8.0 at the latest.

Solution 5

Create Image:

 docker build -t "imagename1" .
 docker build -t "imagename2" .

Run Docker image:

docker run -it -p 8000:8000 --name=imagename1 imagename1
docker run -it -p 8080:8080 --name=imagename2 imagename2

Create Network:

docker network create -d bridge "networkname"

Connect the network with container(imagename) created after running the image:

docker network connect "networkname" "imagename1"
docker network connect "networkname" "imagename2"

We can add any number of containers to the network.

docker network inspect ''networkname"
Share:
34,053

Related videos on Youtube

Mark Ellul
Author by

Mark Ellul

A Software Engineer with a Passion for Problem solving and new technologies. Freelancing for clients around the world. So if you have a project and need someone who can help, I am your man.

Updated on February 28, 2020

Comments

  • Mark Ellul
    Mark Ellul over 4 years

    I want to run rabbitmq-server in one docker container and connect to it from another container using celery (http://celeryproject.org/)

    I have rabbitmq running using the below command...

    sudo docker run -d -p :5672 markellul/rabbitmq /usr/sbin/rabbitmq-server
    

    and running the celery via

    sudo docker run -i -t markellul/celery /bin/bash
    

    When I am trying to do the very basic tutorial to validate the connection on http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html

    I am getting a connection refused error:

    consumer: Cannot connect to amqp://[email protected]:5672//: [Errno 111] Connection refused.

    When I install rabbitmq on the same container as celery it works fine.

    What do I need to do to have container interacting with each other?

  • qkrijger
    qkrijger almost 11 years
    and can you get the HOST_IP automatically ?
  • Mark Ellul
    Mark Ellul almost 11 years
    @amattn did you install ifconfig in your container?
  • Mark Ellul
    Mark Ellul almost 11 years
    Whats interesting in maestro, is they have a example todos app which does connect a node container to a mongodb container. Today I am investigating how the node app finds the mongodb one.
  • amattn
    amattn almost 11 years
    The container doesn't usually need it... I almost always install it on the host.
  • qkrijger
    qkrijger almost 11 years
    So how do you pass it to the container - I guess that would nicely finish up this line of information :)
  • amattn
    amattn almost 11 years
    ifconfig lists a whole bunch of info. there might be flags to get just the host, but I just run ifconfig, then copy paste the ip address into where ever it needs to go.
  • dagelf
    dagelf over 9 years
    You can also check your /etc/hosts file, or docker inspect from the host
  • Willian Paixao
    Willian Paixao over 7 years
    Your answer is great @alp, and since you updated it I would also mention Docker Compose as a long-term solution.
  • Alp
    Alp over 7 years
    Docker compose is a great tool for bootstrapping and running, but not exactly in the scope of this question imo. Thanks nonetheless.