我有一个目录apkmirror-scraper-compose
,其结构如下:
.
├── docker-compose.yml
├── privoxy
│ ├── config
│ └── Dockerfile
├── scraper
│ ├── Dockerfile
│ ├── newnym.py
│ └── requirements.txt
└── tor
└── Dockerfile
我试图运行下面的 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
其中 tor
的 Dockerfile
是
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"]
privoxy "的 "Dockerfile "是
FROM alpine:latest
EXPOSE 8118
RUN apk --update add privoxy
COPY config /etc/privoxy/config
CMD ["privoxy", "--no-daemon"]
其中,"config "由以下两行组成
listen-address 0.0.0.0:8118
forward-socks5 / tor:9050 .
而 scraper
的 Dockerfile
是
FROM python:2.7-alpine
ADD . /scraper
WORKDIR /scraper
RUN pip install -r requirements.txt
CMD ["python", "newnym.py"]
其中,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)))
docker-compose构建 "成功构建,但如果我尝试 "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
我试着搜索关于此错误信息的帮助,但没有找到。是什么导致了这个错误?
根据 Peter Hauge's 评论,在运行 docker network ls
时,我看到(除其他行外)以下内容:
NETWORK ID NAME DRIVER SCOPE
dc6a83d13f44 bridge bridge local
ea98225c7754 docker_gwbridge bridge local
107dcd8aa889 host host local
NAME "和 "DRIVER "都是 "host "的那一行似乎就是他所说的 "已在您的主机上创建的网络"。于是,我按照 https://gist.github.com/bastman/5b57ddb3c11942094f8d0a97d461b430 运行了命令
docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')
现在,"docker-compose up "可以工作了(不过 "newnym.py "会出错)。
我也遇到了同样的问题,也有同样的错误信息,但删除未使用的 docker 网络的解决方案并没有帮到我。我删除了所有非默认的 docker 网络(以及所有镜像和容器),但还是无济于事 - docker 仍然无法创建新的网络。
问题的原因在于安装 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)
...
我发现可以通过几个命令来删除它们:
ip link delete tun0
ip link delete tun1
之后问题就消失了。