如何配置docker以避免和特定的地址或子网? [英] How to configure docker to avoid and specific address or subnet?
问题描述
在我们的开发环境中,我们使用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个通用#113-Ubuntu SMP周四9月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屋!