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

查看:72
本文介绍了如何配置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天全站免登陆