Python Docker“错误:在分配给网络的默认值中找不到可用的、非重叠的 IPv4 地址池”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43720339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:19:31  来源:igfitidea点击:

Docker "ERROR: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network"

pythondockerdocker-compose

提问by Kurt Peek

I have a directory apkmirror-scraper-composewith the following structure:

我有一个apkmirror-scraper-compose具有以下结构的目录:

.
├── docker-compose.yml
├── privoxy
│?? ├── config
│?? └── Dockerfile
├── scraper
│?? ├── Dockerfile
│?? ├── newnym.py
│?? └── requirements.txt
└── tor
    └── Dockerfile

I'm trying to run the following docker-compose.yml:

我正在尝试运行以下命令docker-compose.yml

version: '3'

services:
  privoxy:
    build: ./privoxy
    ports:
      - "8118:8118"
    links:
      - tor

  tor:
    build:
      context: ./tor
      args:
        password: ""
    ports:
      - "9050:9050"
      - "9051:9051"

  scraper:
    build: ./scraper
    links:
      - tor
      - privoxy

where the Dockerfilefor toris

其中,Dockerfile用于tor

FROM alpine:latest
EXPOSE 9050 9051
ARG password
RUN apk --update add tor
RUN echo "ControlPort 9051" >> /etc/tor/torrc
RUN echo "HashedControlPassword $(tor --quiet --hash-password $password)" >> /etc/tor/torrc
CMD ["tor"]

that for privoxyis

privoxy

FROM alpine:latest
EXPOSE 8118
RUN apk --update add privoxy
COPY config /etc/privoxy/config
CMD ["privoxy", "--no-daemon"]

where configconsists of the two lines

其中config由两行组成

listen-address 0.0.0.0:8118
forward-socks5 / tor:9050 .

and the Dockerfilefor scraperis

Dockerfileforscraper

FROM python:2.7-alpine
ADD . /scraper
WORKDIR /scraper
RUN pip install -r requirements.txt
CMD ["python", "newnym.py"]

where requirements.txtcontains the single line requests. Finally, the program newnym.pyis designed to simply test whether changing the IP address using Tor is working:

其中requirements.txt包含单行requests。最后,该程序newnym.py旨在简单地测试使用 Tor 更改 IP 地址是否有效:

from time import sleep, time

import requests as req
import telnetlib


def get_ip():
    IPECHO_ENDPOINT = 'http://ipecho.net/plain'
    HTTP_PROXY = 'http://privoxy:8118'
    return req.get(IPECHO_ENDPOINT, proxies={'http': HTTP_PROXY}).text


def request_ip_change():
    tn = telnetlib.Telnet('tor', 9051)
    tn.read_until("Escape character is '^]'.", 2)
    tn.write('AUTHENTICATE ""\r\n')
    tn.read_until("250 OK", 2)
    tn.write("signal NEWNYM\r\n")
    tn.read_until("250 OK", 2)
    tn.write("quit\r\n")
    tn.close()


if __name__ == '__main__':
    dts = []
    try:
        while True:
            ip = get_ip()
            t0 = time()
            request_ip_change()
            while True:
                new_ip = get_ip()
                if new_ip == ip:
                    sleep(1)
                else:
                    break
            dt = time() - t0
            dts.append(dt)
            print("{} -> {} in ~{}s".format(ip, new_ip, int(dt)))
    except KeyboardInterrupt:
        print("Stopping...")
        print("Average: {}".format(sum(dts) / len(dts)))

The docker-compose buildbuilds successfully, but if I try docker-compose up, I get the following error message:

docker-compose build成功建立,但如果我尝试docker-compose up,我得到以下错误信息:

Creating network "apkmirrorscrapercompose_default" with the default driver
ERROR: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network

I tried searching for help on this error message, but couldn't find any. What is causing this error?

我尝试搜索有关此错误消息的帮助,但找不到任何帮助。是什么导致了这个错误?

采纳答案by Kurt Peek

Following Peter Hauge's comment, upon running docker network lsI saw (among other lines) the following:

根据Peter Hauge评论,在运行时docker network ls我看到(除其他外)以下内容:

NETWORK ID          NAME                                    DRIVER              SCOPE
dc6a83d13f44        bridge                                  bridge              local
ea98225c7754        docker_gwbridge                         bridge              local
107dcd8aa889        host                                    host                local

The line with NAMEand DRIVERas both hostseems to be what he is referring to with "networks already created on your host". So, following https://gist.github.com/bastman/5b57ddb3c11942094f8d0a97d461b430, I ran the command

NAMEDRIVERas的行host似乎是他所指的“已在您的主机上创建的网络”。因此,按照https://gist.github.com/bastman/5b57ddb3c11942094f8d0a97d461b430,我运行了命令

docker network rm $(docker network ls | grep "bridge" | awk '/ / { print  }')

Now docker-compose upworks (although newnym.pyproduces an error).

现在docker-compose up可以工作(虽然newnym.py会产生错误)。

回答by bbeecher

I've seen it suggested docker may be at its maximum of created networks. The command docker network prunecan be used to remove all networks not used by at least one container.

我已经看到它建议 docker 可能处于已创建网络的最大值。该命令docker network prune可用于删除至少一个容器未使用的所有网络。

My issue ended up being, as Robertcommented about: an issue with openvpn service openvpn stop'solved' the problem.

正如罗伯特评论的那样,我的问题最终变成了:openvpnservice openvpn stop的一个问题“解决”了这个问题。

回答by DrDamnit

I ran into this problem because I had OpenVPN running. As soon as I killed OpenVPN, docker-compose upfired right up, and the error disappeared.

我遇到了这个问题,因为我运行了 OpenVPN。我一杀死 OpenVPN,立即docker-compose up启动,错误消失了。

回答by Arenim

I ran in this problem with OpenVPN working as well and I've found a solution where you should NOT stop/start OpenVPN server.

我在 OpenVPN 工作时也遇到了这个问题,我找到了一个解决方案,您不应该停止/启动 OpenVPN 服务器。

Idea that You should specify what exactly subnet you want to use. In docker-compose.ymlwrite:

您应该指定要使用的确切子网。在docker-compose.yml写:

networks:
  default:
    driver: bridge
    ipam:
      config:
        - subnet: 172.16.57.0/24

That's it. Now, defaultnetwork will be used and if your VPN did not assign you something from 172.16.57.*subnet, you're fine.

就是这样。现在,default网络将被使用,如果您的 VPN 没有从172.16.57.*子网分配给您一些东西,那您就没事了。

回答by madjardi

I have the same problem. I ran docker system prune -a --volumes, docker network prune, but neither helped me.

我也有同样的问题。我跑了docker system prune -a --volumesdocker network prune但都没有帮助我。

I use a VPN, I turned off the VPN and, after it docker started normal and was able to create a network. After that, you can enable VPN again.

我使用 VPN,我关闭了 VPN,然后 docker 正常启动并能够创建网络。之后,您可以再次启用 VPN。

回答by Carlos Segarra

As other answers mentioned, Docker's default local bridgenetwork only supports 30 different networks (each one of them uniquely identifiable by their name). If you are not using them, then docker network prunewill do the trick.

正如其他答案所提到的,Docker 的默认本地bridge网络仅支持 30 个不同的网络(每个网络都可以通过其名称唯一标识)。如果您不使用它们,那么docker network prune就可以解决问题。

However, you might be interested in establishing more than 30 containers, each with their own network. Were you interested in doing so then you would need to define an overlaynetwork. This is a bit more tricky but extremely well documented here.

但是,您可能对建立 30 多个容器感兴趣,每个容器都有自己的网络。如果您有兴趣这样做,那么您需要定义一个overlay网络。这有点棘手,但这里记录得非常好。

EDIT (May 2020): Link has become unavailable, going through the docs there's not an exact replacement, but I would recommend starting from here.

编辑(2020 年 5 月):链接已不可用,通过文档没有确切的替代品,但我建议从这里开始。

回答by Rara

I had an identical problem with the same error message but the solution with removal of unused docker networks didn't help me. I've deleted all non-default docker networks (and all images and containers as well) but it didn't help - docker still was not able to create a new network.

我遇到了相同的错误消息和相同的问题,但是删除未使用的 docker 网络的解决方案对我没有帮助。我已经删除了所有非默认 docker 网络(以及所有图像和容器),但它没有帮助 - docker 仍然无法创建新网络。

The cause of the problem was in network interfaces that were left after OpenVpn installation. (It was installed on the host previously.) I found them by running ifconfigcommand:

问题的原因在于 OpenVpn 安装后留下的网络接口。(它以前安装在主机上。)我通过运行ifconfig命令找到它们:

...
tun0  Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
      inet addr:10.8.0.2  P-t-P:10.8.0.2  Mask:255.255.255.0
      UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
      RX packets:75 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:100 
      RX bytes:84304 (84.3 KB)  TX bytes:0 (0.0 B)

tun1  Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
      inet addr:10.8.0.2  P-t-P:10.8.0.2  Mask:255.255.255.0
      UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
      RX packets:200496 errors:0 dropped:0 overruns:0 frame:0
      TX packets:148828 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:100 
      RX bytes:211583838 (211.5 MB)  TX bytes:9568906 (9.5 MB)
...

I've found that I can remove them with a couple of commands:

我发现我可以使用几个命令删除它们:

ip link delete tun0
ip link delete tun1

After this the problem has disappeared.

在此之后,问题就消失了。

回答by Nandini Chaurasiya

  1. Check if any other container is running, If yes, do: docker-compose down
  2. If VPN is connected, then disconnect it and try again to up docker container:

    docker-compose up -d container_name
    
  1. 检查是否有其他容器正在运行,如果是,请执行以下操作: docker-compose down
  2. 如果 VPN 已连接,则断开它并再次尝试启动 docker 容器:

    docker-compose up -d container_name
    

回答by Ivan Mishur

You can try

你可以试试

$sudo service network-manager restart

Worked for me.

为我工作。

回答by Amine Benkeroum

I encoutered the same problem, the reason why is that you reached the max of networks:

我遇到了同样的问题,原因是你达到了网络的最大值:

do an : docker network lsChoose one to remove using: docker network rm networkname_default

做一个:docker network ls选择一个删除使用:docker network rm networkname_default