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 ?

  • MobileFirst Container CLI Extensions are not installed
  • Connection refused when try to connect http server in docker daemon container
  • Sporadic Robolectric test failures when running inside Docker container
  • how to get the layers of a docker image on local host
  • Using fs.stat to get stats from a file inside a docker container directory
  • Docker : oci runtime error: exec: “/bin/bash”: stat /bin in windows 7
  • List containers in the same docker-compose
  • Can fail2ban run in a separate docker container while somehow still implement iptables rules for nginx?
  • Docker stats from java
  • Destination Host Unreachable while communicating between container using consul in docker overlay network
  • Cannot remove all containers in a row
  • How to restart multiple containers in a docker swarm
  • 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.