运行容器时如何使用docker主机名

时间:2018-12-26 20:13:15

标签: docker

我正在docker中运行RabbitMQ

const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
  case '--squirrel-install':
  case '--squirrel-updated':
    // Optionally do things such as:
    // - Add your .exe to the PATH
    // - Write to the registry for things like file associations and
    // explorer context menus

    // Install desktop and start menu shortcuts
    spawnUpdate(['--createShortcut', exeName]);

    setTimeout(app.quit, 1000);
    return true;

  case '--squirrel-uninstall':
    // Undo anything you did in the --squirrel-install and
    // --squirrel-updated handlers

    // Remove desktop and start menu shortcuts
    spawnUpdate(['--removeShortcut', exeName]);

    setTimeout(app.quit, 1000);
    return true;

  case '--squirrel-obsolete':
    // This is called on the outgoing version of your app before
    // we update to the new version - it's the opposite of
    // --squirrel-updated

    app.quit();
    return true;
  }
}
  • 我的容器>docker run -d --hostname my-rabbit --name some-rabbit --rm --network my-bridge rabbitmq 一些兔子,而我的name我的兔子
  • 该容器已连接到我的桥网络
  • 我的应用还连接到我的桥网络

hostname产生:

docker network inspect my-bridge

[ { "Name": "my-bridge", "Id": "c996f5318b2b3ff907b6680e41c4551f184f2c43b0df43fd6916fa8d1707e427", "Created": "2018-12-26T19:40:46.6982856Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.21.0.0/16", "Gateway": "172.21.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "98d81ba991febbc9dc0775effc79a2b083f62fe563aad3ce146e56bab5b09f4c": { "Name": "some-rabbit", "EndpointID": "acb3da3e91d655a3ed245fa3938edd681318129e514b83db07e1b4efd4f2bfc2", "MacAddress": "02:42:ac:15:00:02", "IPv4Address": "172.21.0.2/16", "IPv6Address": "" }, "9fdf456e352e932b13530223f736028f357e63ead5ace042a13200f6d5744d62": { "Name": "dockercompose8897046891659778892_publisher_1", "EndpointID": "f45ba7f9c7832d3514d22ddf29bc19f1e74529cb371b335542c9eb54469fe7a4", "MacAddress": "02:42:ac:15:00:04", "IPv4Address": "172.21.0.4/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ] 产生:

docker inspect some-rabbit

结论:

  • rabbitmq和我的应用程序都在同一网络中运行
  • RabbitMQ有一个自定义的 ... "Config": { "Hostname": "my-rabbit", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "25672/tcp": {}, "4369/tcp": {}, "5671/tcp": {}, "5672/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/lib/rabbitmq/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.10", "RABBITMQ_LOGS=-", "RABBITMQ_SASL_LOGS=-", "RABBITMQ_GPG_KEY=0A9AF2115F4687BD29803A206B73A36E6026DFCA", "RABBITMQ_VERSION=3.7.8", "RABBITMQ_GITHUB_TAG=v3.7.8", "RABBITMQ_DEBIAN_VERSION=3.7.8-1", "LANG=C.UTF-8", "HOME=/var/lib/rabbitmq" ], "Cmd": [ "rabbitmq-server" ], "ArgsEscaped": true, "Image": "rabbitmq", "Volumes": { "/var/lib/rabbitmq": {} }, "WorkingDir": "", "Entrypoint": [ "docker-entrypoint.sh" ], "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "bac2e155323c1ec94e145a37f45f8bc0d32e28a2b363cb1c9f3d4ed922a91a56", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "25672/tcp": null, "4369/tcp": null, "5671/tcp": null, "5672/tcp": null }, "SandboxKey": "/var/run/docker/netns/bac2e155323c", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "my-bridge": { "IPAMConfig": null, "Links": null, "Aliases": [ "98d81ba991fe" ], "NetworkID": "c996f5318b2b3ff907b6680e41c4551f184f2c43b0df43fd6916fa8d1707e427", "EndpointID": "acb3da3e91d655a3ed245fa3938edd681318129e514b83db07e1b4efd4f2bfc2", "Gateway": "172.21.0.1", "IPAddress": "172.21.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:15:00:02", "DriverOpts": null } } } } ] 我的兔子,可以在检查容器时观察到:hostname

但是,当我的应用尝试访问地址"Hostname": "my-rabbit"上的RabbitMQ时,连接失败。仅当我使用容器名称作为地址(my-rabbit)时,它才有效。

我对some-rabbit的理解是错误的还是我使用的错误?何时使用自定义--hostname有用?

1 个答案:

答案 0 :(得分:5)

主机名仅对同一容器内的应用程序可见。 Docker的嵌入式DNS不会解析容器主机名。您可以使用Docker的DNS解决的问题是:

  • 容器名称
  • 容器ID
  • 容器的网络别名(包括撰写文件中的服务名称)