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 ?

  • Install an sql dump file to a docker container with mariaDB
  • Dockerized Django with webpackDevServer
  • Is there a way to access a running docker container on a remote server from my local development enviroment(Sublime)
  • Is there a way to repopulate with command-line a postgres db that I can't drop?
  • Docker - Prometheus container dies immediatly
  • “The headers or library files could not be found for jpeg” installing Pillow on Alpine Linux
  • Sharing volume between docker containers
  • Why Docker/Containers has it's own networking concept(CNM/CNI) instead of using VM networking?
  • Dockerfile contains a python script that writes to an output file, but the output file is not being created on the container
  • Where can I get the newest “Setting Up a Dev Environment” for Docker?
  • Setup secured Jenkins master with docker
  • Continuous Delivery on Wordpress using Docker
  • 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 (172.25.0.5): 56 data bytes
    64 bytes from 172.25.0.5: 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.