如何到达docker-compose中的链接服务?

根据https://docs.docker.com/compose/compose-file/#links ,如果我指定docker-compose links下的另一个服务的名称,我应该能够达到该服务的主机名相同服务名称。

为了testing这个,我尝试了下面docker-compose.yml

 version: '3' services: tor: build: ./tor use_tor: build: ./use_tor links: - tor 

toruse_tor目录包含Dockerfile

 . ├── docker-compose.yml ├── tor │  └── Dockerfile └── use_tor └── Dockerfile 

对于tor

 FROM alpine:latest EXPOSE 9050 RUN apk --update add tor CMD ["tor"] 

和for use_tor

 FROM alpine:latest CMD ["nc", "-z", "tor", "9050"] 

但是,如果我执行docker-compose build然后是docker-compose up ,我会从日志中看到use_tor服务以状态码1退出:

 Starting scrapercompose_tor_1 Recreating scrapercompose_use_tor_1 Attaching to scrapercompose_tor_1, scrapercompose_use_tor_1 tor_1 | May 02 15:36:34.123 [notice] Tor v0.2.8.12 running on Linux with Libevent 2.0.22-stable, OpenSSL LibreSSL 2.4.4 and Zlib 1.2.8. tor_1 | May 02 15:36:34.123 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning tor_1 | May 02 15:36:34.123 [notice] Configuration file "/etc/tor/torrc" not present, using reasonable defaults. tor_1 | May 02 15:36:34.129 [notice] Opening Socks listener on 127.0.0.1:9050 tor_1 | May 02 15:36:34.000 [notice] Parsing GEOIP IPv4 file /usr/share/tor/geoip. tor_1 | May 02 15:36:34.000 [notice] Parsing GEOIP IPv6 file /usr/share/tor/geoip6. tor_1 | May 02 15:36:34.000 [warn] You are running Tor as root. You don't need to, and you probably shouldn't. tor_1 | May 02 15:36:34.000 [notice] We were built to run on a 64-bit CPU, with OpenSSL 1.0.1 or later, but with a version of OpenSSL that apparently lacks accelerated support for the NIST P-224 and P-256 groups. Building openssl with such support (using the enable-ec_nistp_64_gcc_128 option when configuring it) would make ECDH much faster. tor_1 | May 02 15:36:34.000 [notice] Bootstrapped 0%: Starting scrapercompose_use_tor_1 exited with code 1 tor_1 | May 02 15:36:35.000 [notice] Bootstrapped 80%: Connecting to the Tor network tor_1 | May 02 15:36:36.000 [notice] Bootstrapped 85%: Finishing handshake with first hop tor_1 | May 02 15:36:36.000 [notice] Bootstrapped 90%: Establishing a Tor circuit tor_1 | May 02 15:36:36.000 [notice] Tor has successfully opened a circuit. Looks like client functionality is working. tor_1 | May 02 15:36:36.000 [notice] Bootstrapped 100%: Done 

显然,命令nc -z tor 9050不会在use_tor容器上返回预期的状态码0 。 但是,在我看来,这应该工作。 例如,如果我修改tor服务以将容器上的端口9050映射到主机,如下所示,

 services: tor: build: ./tor ports: - "9050:9050" 

然后在我的普通terminal,我确实看到nc -z localhost 9050产生一个退出代码0

 kurt@kurt-ThinkPad:~$ nc -z localhost 9050 kurt@kurt-ThinkPad:~$ echo $? 0 

总之,我希望主机名tor在端口映射后的localhost上像localhost主机一样运行,但这似乎并非如此。 为什么这不起作用?

这个问题让我一度耿耿于怀。 虽然我克隆了这个例子,但无法得到解决scheme。 根据docker文件

EXPOSE指令通知Docker 在运行时侦听指定的networking端口。 EXPOSE不会使容器的端口可以被主机访问 。 要做到这一点, 您必须使用-p标志来发布一系列的端口,或者使用-P标志来发布所有暴露的端口 。 您可以公开一个端口号并在外部使用另一个号码进行发布。

所以我认为这可能是因为tor服务运行在127.0.0.1而不是0.0.0.0(对于他们之间的差异,你可以看看这里 )

tor_1 | 五月02 15:36:34.129 [notice] 127.0.0.1:9050打开Socks侦听器

它可以通过terminal访问,因为docker-compose.yml中的ports参数与-p参数相同。

总而言之,如果tor服务在0.0.0.0侦听,那么它应该按照预期工作。