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

  • hostname返回quickstart
  • hostname -i返回172.17.0.3

  • 但是,host quickstart返回Host quickstart not found: 3(NXDOMAIN)
    [root@quickstart /]# host quickstart.cloudera返回
    Host quickstart.cloudera 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 --name my_cloudera --hostname=quickstart.cloudera --privileged=true -td -p 8888:8888 -p 7180:7180 -p 6680:80 -v ~/cloudera_data/lib:/var/lib my_cloudera /usr/bin/docker-quickstart启动

    容器/etc/hosts显示:
    [root@quickstart /]# cat /etc/hosts127.0.0.1 localhost::1 localhost ip6-localhost ip6-loopbackfe00::0 ip6-localnetff00::0 ip6-mcastprefixff02::1 ip6-allnodesff02::2 ip6-allrouters172.17.0.3 quickstart.cloudera quickstart
    docker daemon在MacOS上运行

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

    最佳答案

    启动容器时,在--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
    
    

    关于docker - 在docker容器内运行的host命令无法解析容器主机名,但ping可以,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62196300/

    10-11 07:58