如何在docker-compose中访问链接服务?

时间:2017-05-02 15:52:35

标签: bash docker docker-compose netcat

根据https://docs.docker.com/compose/compose-file/#links,如果我在links中的docker-compose下指定其他服务的名称,我应该能够以与服务名称相同的主机名访问该服务。< / p>

为了测试这一点,我尝试了以下docker-compose.yml

version: '3'

services:
  tor:
    build: ./tor

  use_tor:
    build: ./use_tor
    links:
      - tor

toruse_tor目录包含Dockerfile s:

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

对于tor

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

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并未在0容器上返回预期的状态代码use_tor。但是,在我看来这应该有效。例如,如果我修改tor服务以将容器上的端口9050映射到主机,如下所示,

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

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

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

简而言之,我希望主机名tor在端口映射后在我的主机上表现得像localhost,但似乎并非如此。为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

这个问题让我愣了一下。虽然我克隆了这个例子但是无法得到解决方案。根据码头docs

  

EXPOSE指令通知Docker 容器侦听   运行时指定的网络端口。 EXPOSE不会创建端口   主机可以访问的容器。要做到这一点,你必须使用   -p标志用于发布一系列端口或-P标志   发布所有暴露的端口。您可以公开一个端口号和   在另一个号码下向外发布。

所以我认为这可能是因为tor服务运行在127.0.0.1而不是0.0.0.0(对于它们之间的区别,你可以看here

  

tor_1 | 5月2日15:36:34.129 [通知]打开袜子听众   127.0.0.1:9050

可通过终端访问是因为ports中的docker-compose.yml参数与-p参数相同。

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