如何设置容器内服务的ip?默认网络中的静态IP

如何设置容器内服务的ip?默认网络中的静态IP

使用来自输出的容器化 Elasticsearch 的 IP 地址docker inspect使用我成功调用的弹性搜索在另一个容器内运行的 Jupyter 笔记本中运行的函数。两者均由docker compose up.

但就我而言IP地址变更在新的运行时(例如 docker compose up/down)。

ip可以设置吗?例如,通过向构建上下文中使用的elasticsearchdocker-compose.yml添加一些内容?Dockerfile

docker inspect *composed-container-name-here*

"NetworkSettings": {
      "Bridge": "",
      ...
      "Ports": {
          "9200/tcp": null,
          "9300/tcp": null
      },
      ...
      "IPAddress": "",
      "Networks": {
          "*composed-container-name-here*_default": {
              "IPAMConfig": null,
              ...
              "Gateway": "172.X.X.1",
              "IPAddress": "172.X.X.3",
          }

我试过子网划分docker-compose.yml,但它返回了Error response from daemon: user specified IP address is supported only when connecting to networks with user configured subnets

答案1

解决方案:

services:
  a-service-here:
    ...
    networks:
      default:
        ipv4_address: x.x.x.3 # desired-ip-here

networks:
  default:
    driver: bridge
    ipam:
      config:
        - subnet: x.x.x.0/16 # includes desired-ip

相关内容