在 Dockerfile (redis) 中动态添加 docker 容器 ip [英] Dynamically add docker container ip in Dockerfile ( redis)

查看:107
本文介绍了在 Dockerfile (redis) 中动态添加 docker 容器 ip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在其他 Dockerfile 中动态添加容器 ip(我正在运行两个容器 a)Redis b)java 应用程序.我需要在运行时将 redis url 传递给我的 java 参数

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

目前我正在手动检查redis ip并将其复制到Dockerfile中.然后使用 redis ip 为 java 应用程序创建新图像.

Currently I am manually checking the redis ip and copying it in Dockerfile. and later creating new image using redis ip for java application.

docker run --name my-redis -d redis
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-redis 

IN Dockerfile(java 应用程序)

IN Dockerfile (java application)

CMD ["-Dspring.redis.host=172.17.0.2", "-jar", "/apps/some-0.0.1-SNAPSHOT.jar"]

我可以使用任何脚本来更新 DockerFile 还是可以使用任何环境变量.

Can I use any script to update the DockerFile or can use any environment variable.

推荐答案

您可以在运行时为您的 dokcer 容器分配一个静态 IP 地址,具体步骤如下:

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

1 - 创建自定义网络:

1 - create custom network:

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

2 - 运行redis容器使用指定网络,并分配ip地址:

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

到那时你已经有了 my-redis 容器的静态 IP 地址 172.17.0.2,你不需要再检查它了.

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 - 现在可以运行 java 应用程序容器,但它必须使用相同的网络:

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.

更多信息可以在官方文档中找到(搜索--ip):

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

编辑(添加 docker-compose):

我刚刚发现也可以使用 docker-compose 分配静态 ip,而 this answer 给出了一个举例说明.

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

官方文档:https://docs.docker.com/compose/networking/

希望这可以帮助您找到自己的方式.

hope this helps you find your way.

这篇关于在 Dockerfile (redis) 中动态添加 docker 容器 ip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆