从一个 Docker 容器记录到另一个

Logging from one Docker Container to Another

当尝试在我的 ubuntu 16.04 主机上连接两个容器时,我无法将消息从一个容器发送到另一个容器,而地址在主机上可用。

我启动了一个容器(它通过 syslog-ng 提供系统日志服务):

docker run -d -p 127.0.0.1:515:514/udp --name syslog-ng bobrik/syslog-ng

此容器定义于:https://github.com/bobrik/docker-syslog-ng . According to https://www.balabit.com/documents/syslog-ng-ose-latest-guides/en/syslog-ng-ose-guide-admin/html/configuring-sources-network.html系统日志中的 udp(ip(0.0.0.0), port(514));-ng.conf 应该可以接受所有 ip 连接。

现在我可以使用以下方法从主机登录到容器:

logger -n 127.0.0.1 -P 515 test123

第二个容器开始于(一次使用 [=19=,一次不使用)

docker run -it --link syslog-ng ubuntu /bin/bash

并且在这两种情况下,容器都无法使用日志记录,即

logger -n 127.0.0.3 -P 515

returns 没有错误消息,但也没有消息添加到日志中。 (在链接容器的情况下,也尝试使用 127.0.0.1 和 syslog-ng)。

那么问题来了:为什么不能登录容器?

如果我启动另一个容器,两者都在同一个网络上,docker network inspect bridge returns:

[
    {
        "Name": "bridge",
        "Id": "6836c8a52555f30f27001daf9b111ad41a035a31783250e043c34602ea83cfe3",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Containers": {
            "3acef55e018280571410b63b0cb7314ba354b67fb6523662b48ad09be8424423": {
                "Name": "syslog-ng",
                "EndpointID": "d6f2ad9cc9b5f6a5030e6061c4abb600ff7b0f16711f169954dd446f1351cb08",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "4f7d0101f97dca63b160e16a5e298c45f2ff51aa35085d0cdec96497a598be1a": {
                "Name": "goofy_lamarr",
                "EndpointID": "e357d6a99f67964bac4619d1ac984cd361a9c39137ea8d67a1ebc641e498919b",
                "MacAddress": "02:42:ac:11:00:05",
                "IPv4Address": "172.17.0.5/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

这是 ubuntu 容器上的 /etc/hosts 我尝试记录的容器:

cat /etc/hosts 
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3  syslog-ng 3acef55e0182
172.17.0.5  4f7d0101f97d

因此 syslog-ng 应该可用于 syslog-ng 或 172.17.0.3。

如果我理解https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/ correctly, than linking should make it possible to log directly to syslog-ng, and without the link, I should still be able to send messages from the one container to the container which exposed a port by ip, according to https://docs.docker.com/engine/userguide/networking/

这似乎与:Logging from one docker container to another相同,但没有提供具体的容器。

有谁知道为什么无法进行此日志记录?

有效。问题出在端口号 (515),使用此命令:

docker run -d -p 127.0.0.1:515:514/udp --name syslog-ng bobrik/syslog-ng

您说可以使用 127.0.0.1:515 从主机访问内部端口 514。但是如果你想直接从另一个容器到达容器,你可以用 syslog-ng:514 或用 172.17.0.2:514:

到达它

因此正确的记录器将是:

logger -n syslog-ng -P 514 test123345

希望对你有帮助

Br,

米切克