如何配置docker来避开特定地址或子网? [英] How to configure docker to avoid a specific address or subnet?

查看:24
本文介绍了如何配置docker来避开特定地址或子网?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的开发环境中,我们使用 docker-compose 文件管理 20 多个容器,但有时其中一个从我们的网络获取一个 IP 并且我们失去连接.发生这种情况时,我们会手动关闭容器和网络.

In our development environment, we manage 20+ containers with docker-compose files, but from time to time one of them acquires one IP from our network and we lost connectivity. When that happens, we manually shut down the container and the network.

当我们添加更多容器时,有没有办法配置 docker 以避开特定的 IP 地址或子网?

Is there a way to configure docker to avoid that specific IP address or subnet when we add more containers?

我们的 docker 版本是:

Our docker version is:

Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:45:36 2020
 OS/Arch:           linux/amd64
 Experimental:      false

我们的 docker-compose 版本是:

Our docker-compose version is:

docker-compose version 1.25.4, build 8d51620a
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

主机操作系统是:Linux lin_env_int_2 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux(Ubuntu 18.04.4 LTS)

The host OS is: Linux lin_env_int_2 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux (Ubuntu 18.04.4 LTS)

推荐答案

如果您在可能发生子网冲突时拥有更大的基础架构,那么我建议您创建自己的 docker 网络,然后托管这些容器.

If you have a larger infrastructure when this subnet collisions may occur then I would recommend creating your own docker network which would then host those containers.

您可以轻松地为该 docker 网络指定 IP 范围,以确保它不会与您的任何其他网络重叠.

You can easily specify IP range for that docker network such that you can be sure that it doesn't overlap with any of your other networks.

docker network create --subnet 10.10.0.0/16 mynet

并使用 --network mynet

docker container run -d -p 8080:80 --network mynet --name web nginx

使用docker container inspect

"IPAddress": "10.10.0.2",

在docker-compose中,你可以像这样定位之前创建的网络(mynet)

In docker-compose, you can target the previously created network (mynet) like this

version: "3.4"

services:
  web:
    image: nginx
    ports:
      - "8888:80"
    networks:
      - mynet

networks:
  mynet:
    external:
      name: mynet

这篇关于如何配置docker来避开特定地址或子网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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