Connect from one Docker container to another

后端 未结 7 1646
故里飘歌
故里飘歌 2020-12-04 18:11

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

相关标签:
7条回答
  • 2020-12-04 18:50

    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
    
    0 讨论(0)
  • 2020-12-04 18:51

    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"
    
    0 讨论(0)
  • 2020-12-04 18:56

    You can get the docker instance IP with...

    CID=$(sudo docker run -d -p :5672 markellul/rabbitmq /usr/sbin/rabbitmq-server); sudo docker inspect $CID | grep IPAddress
    

    But that's not very useful.

    You can use pipework to create a private network between docker containers.

    0 讨论(0)
  • 2020-12-04 19:01

    This is currently on the 0.8 roadmap:

    https://github.com/dotcloud/docker/issues/1143

    0 讨论(0)
  • 2020-12-04 19:08

    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.

    0 讨论(0)
  • 2020-12-04 19:13

    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.

    0 讨论(0)
提交回复
热议问题