How to map the IP address of docker container to another container dynamically

I have few docker containers running in a host. I have a scenario where i need to map the IP address of one container to the configuration file available in another container. I know that i can get the IP address of a container using the docker inspect [container] command. I assumes the IP address is assigned dynamically every time i restart the container. So every time i need to change this IP address in another container. Is it possible to have a static ip for container or can i use the ip address of the host instead of ip address of the container. I am new to docker world.

Also i have a case where i need to map the IP address to a container that is running in another host. Example, i have a redis container running in HostA and tomcat container running in HostB. In order to communicate with redis from tomcat, i need to map the ip address and port of redis in the tomcat container. how this can be achieved in docker ?

  • Use Docker, Gunicorn, Nginx in django development environment but can only see nginx welcome page?
  • Which Docker images will run on Kubernetes?
  • docker container does not start Java in background
  • Docker: Issue with pulling from a private registry from another server
  • Docker: enhancing containers
  • Can't access service outside of Docker container
  • Docker Container Command cannot locate shell script
  • Why is bin/sh not found on my alpine based docker-compose build?
  • what will be impacted for compiling code in different kernel in docker?
  • How to remove docker completely from ubuntu 14.04
  • GC overhead limit exceeded in Docker when trying to build project by sbt
  • Docker: save internal hd contents
  • 2 Solutions collect form web for “How to map the IP address of docker container to another container dynamically”

    You can run linked containers like in the documentation – Linking containers:

    docker run -itd --name=container4 --link container5:c5 image
    docker run -itd --name=container5 --link container4:c4 image

    and ping second container by alias (c5):

    docker attach container4
    / # ping -w 4 c5
    PING c5 ( 56 data bytes
    64 bytes from seq=0 ttl=64 time=0.070 ms

    so you can see it’s possible to ping second container with alias – not with IP address.

    To your second Quesiton: It’s possible if you have redis and tomcat and they know IP addresses of each other (and they are accessible because they are in some network), so this is almost all about the configuration of network.

    As Vlado pointed out, you should access your containers by hostname and allow Docker to manage that mapping those names to IP addresses. There are two options for this legacy links and links in user defined networks. Both descriptions are worth reading, but his paragraph outlines the difference between the two approaches (emphasis mine)

    Please note that while creating container4, we linked to a container named container5 which is not created yet. That is one of the differences in behavior between the legacy link in default bridge network and the new link functionality in user defined networks. The legacy link is static in nature and it hard-binds the container with the alias and it doesn’t tolerate linked container restarts. While the new link functionality in user defined networks are dynamic in nature and supports linked container restarts including tolerating ip-address changes on the linked container.

    Based on your next question, if you want to run containers on multiple hosts, you will probably want to set up an overlay network. Those definitely don’t support legacy links.

    The official docs have good examples, including what commands to run, to create links and user defined networks.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.