简体   繁体   English

在Dockerfile(redis)中动态添加docker容器ip

[英]Dynamically add docker container ip in Dockerfile ( redis)

How do I dynamically add container ip in other Dockerfile ( I am running two container a) Redis b) java application .如何在其他 Dockerfile 中动态添加容器 ip(我正在运行两个容器 a)Redis b)java 应用程序。 I need to pass redis url on run time to my java arguments我需要在运行时将 redis url 传递给我的 java 参数

Currently I am manually checking the redis ip and copying it in Dockerfile.目前我正在手动检查 redis ip 并将其复制到 Dockerfile 中。 and later creating new image using redis ip for java application.然后使用 redis ip 为 java 应用程序创建新图像。

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

IN Dockerfile (java application)在 Dockerfile(Java 应用程序)中

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

Can I use any script to update the DockerFile or can use any environment variable.我可以使用任何脚本来更新 DockerFile 或可以使用任何环境变量。

you can assign a static ip address to your dokcer container when you run it, following the steps:您可以在运行时为您的 dokcer 容器分配一个静态 IP 地址,步骤如下:

1 - create custom network: 1 - 创建自定义网络:

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: 2 - 运行redis容器使用指定的网络,并分配ip地址:

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.到那时你就有了my-redis容器的静态 IP 地址172.17.0.2 ,你不需要再检查它了。

3 - now it is possible to run the java appication container but it must use the same network: 3 - 现在可以运行 java appication 容器,但它必须使用相同的网络:

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.当然,您可以通过使用 env 变量或您觉得方便设置的任何内容来优化解决方案。

More infos can be found in the official docs (search for --ip ):更多信息可以在官方文档中找到(搜索--ip ):

Edit (add docker-compose):编辑(添加 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.我刚刚发现也可以使用 docker-compose 分配静态 ip,这个答案给出了一个例子。

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/官方文档: https : //docs.docker.com/compose/networking/

hope this helps you find your way.希望这可以帮助您找到自己的方式。

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.因此在运行时它将被解析为容器的 ip 地址。

Follow these steps:按着这些次序:

  1. First, create a network for the containers: docker network create my-network首先,为容器创建一个网络: docker network create my-network

  2. Start redis: docker run -d --network=my-network --name=redis redis启动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.编辑 java 应用程序的 Dockerfile,将-Dspring.redis.host=172.17.0.2"替换为-Dspring.redis.host=redis"并再次构建。

  4. Finally start java application container: docker run -it --network=my-network your_image .最后启动java应用程序容器: 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.您可以选择为容器定义名称,但这不是必需的,因为您不从 redis 容器访问 java 应用程序的容器。

Alternatively you can use a docker-compose file.或者,您可以使用docker-compose文件。 By default docker-compose creates a network for running services.默认情况下, docker-compose会创建一个用于运行服务的网络。 I am not aware of your full setup, so I will provide a sample docker-compose.yml that illustrates the main concept.我不知道你的完整设置,所以我将提供一个示例docker-compose.yml来说明主要概念。

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.在这两种方式中,您都可以使用容器的主机名而不是提供静态 ip 从 Java 应用程序动态访问 redis 容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM