在docker容器内运行的主机命令无法解析容器主机名,但ping可以

问题描述 投票:1回答:1

我正在运行以下命令在docker容器内[root@quickstart /]

  • hostname返回quickstart

  • [hostname -i返回172.17.0.3

但是,host quickstart返回Host quickstart not found: 3(NXDOMAIN)

  • ping -q -c 1 quickstart

返回

PING quickstart.cloudera (172.17.0.3) 56(84) bytes of data.

--- quickstart.cloudera ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.053/0.053/0.053/0.000 ms

容器已使用docker run --hostname=quickstart.cloudera...启动

容器/etc/hosts显示:

[root@quickstart /]# 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 quickstart.cloudera quickstart

docker守护程序在MacOS上运行

如何使host解析quickstart容器的主机名?

docker dns ping hostname docker-networking
1个回答
0
投票

启动容器时,在--hostname命令中添加标志docker run,您将能够看到它。看下面的例子:-

$ docker run -it --hostname test alpine /bin/sh

/ # apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
v3.11.6-71-gb45d3b45cc [http://dl-cdn.alpinelinux.org/alpine/v3.11/main]
v3.11.6-68-gf6abc2afac [http://dl-cdn.alpinelinux.org/alpine/v3.11/community]
OK: 11276 distinct packages available
/ # apk add bind-tools
(1/16) Installing fstrm (0.6.0-r1)
(2/16) Installing libgcc (9.2.0-r4)
(3/16) Installing krb5-conf (1.0-r1)
(4/16) Installing libcom_err (1.45.5-r0)
(5/16) Installing keyutils-libs (1.6.1-r0)
(6/16) Installing libverto (0.3.1-r1)
(7/16) Installing krb5-libs (1.17.1-r0)
(8/16) Installing json-c (0.13.1-r1)
(9/16) Installing libstdc++ (9.2.0-r4)
(10/16) Installing libprotobuf (3.11.2-r1)
(11/16) Installing libprotoc (3.11.2-r1)
(12/16) Installing protobuf-c (1.3.2-r3)
(13/16) Installing xz-libs (5.2.4-r0)
(14/16) Installing libxml2 (2.9.10-r3)
(15/16) Installing bind-libs (9.14.12-r0)
(16/16) Installing bind-tools (9.14.12-r0)
Executing busybox-1.31.1-r9.trigger
OK: 18 MiB in 30 packages
/ # hostname
test

/ #  host -v test
Trying "test"
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 49171
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;test.              IN  A

;; ANSWER SECTION:
test.           15  IN  A   127.0.0.1

© www.soinside.com 2019 - 2024. All rights reserved.