Dynamically add docker container ip in Dockerfile ( redis)

后端 未结 2 1567
一个人的身影
一个人的身影 2021-01-27 11:50

How do I dynamically add container ip in other Dockerfile ( I am running two container a) Redis b) java application . I need to pass redis url on run time to my java arguments

相关标签:
2条回答
  • 2021-01-27 12:09

    you can assign a static ip address to your dokcer container when you run it, following the steps:

    1 - create custom network:

    docker network create --subnet=172.17.0.0/16 redis-net
    

    2 - run the redis container to use the specified network, and assign the ip address:

    docker run --net redis-net --ip 172.17.0.2 --name my-redis -d redis
    

    by then you have the static ip address 172.17.0.2 for my-redis container, you don't need to inspect it anymore.

    3 - now it is possible to run the java appication container but it must use the same network:

    docker run --net redis-net my-java-app
    

    of course you can optimize the solution, by using env variables or whatever you find convenient to your setup.

    More infos can be found in the official docs (search for --ip):

    • docker run
    • docker network

    Edit (add docker-compose):

    I just find out that it is also possible to assign static ips using docker-compose, and this answer gives an example how.

    This is a similar example just in case:

    version: '3'
    
    services:
      redis:
        container_name: redis
        image: redis:latest
        restart: always
        networks:
          vpcbr:
            ipv4_address: 172.17.0.2
    
      java-app:
        container_name: java-app
        build: <path to Dockerfile>
        networks:
          vpcbr:
            ipv4_address: 172.17.0.3
        depends_on:
         - redis
    
    networks:
      vpcbr:
        driver: bridge
        ipam:
         config:
           - subnet: 172.17.0.0/16
             gateway: 172.17.0.1
    

    official docs: https://docs.docker.com/compose/networking/

    hope this helps you find your way.

    0 讨论(0)
  • 2021-01-27 12:25

    You should add your containers in the same network . Then at runtime you can use that name to refer to the container with its name. Container's name is the host name in the network. Thus at runtime it will be resolved as container's ip address.

    Follow these steps:

    1. First, create a network for the containers: docker network create my-network

    2. Start redis: docker run -d --network=my-network --name=redis redis

    3. Edit java application's Dockerfile, replace -Dspring.redis.host=172.17.0.2" with -Dspring.redis.host=redis" and build again.

    4. Finally start java application container: docker run -it --network=my-network your_image. Optionally you can define a name for the container, but it is not required as you do not access java application's container from redis container.

    Alternatively you can use a docker-compose file. By default docker-compose creates a network for running services. I am not aware of your full setup, so I will provide a sample docker-compose.yml that illustrates the main concept.

    version: "3.7"
    services:
      redis:
        image: redis
      java_app_image:
        image: your_image_name
    

    In both ways, you are able to access redis container from java application dynamically using container's hostname instead of providing a static ip.

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