docker - docker容器上的自定义名称解析失败

标签 docker ubuntu dns

我想为新容器自定义 DNS 配置。我正在使用 --hostname 标志来设置
新容器的主机名。下面是用于测试主机名的 docker 命令:

docker run --rm --hostname rando alpine:latest nslookup rando
输出如下:
Server:     xxx.xxx.xxx.2
Address:    xxx.xxx.xxx.2:53

** server can't find rando.localdomain: NXDOMAIN

** server can't find rando.localdomain: NXDOMAIN

我使用的是 ubuntu 19.10 版本。以下是来自/etc/resolv.conf的内容文件:
# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver xxx.xxx.xx.2
search localdomain
我将不胜感激解决此问题的任何帮助。
问候,
兰多。
附言
我试图明确地创建桥接网络,但仍然没有工作。下面是执行的结果:
enter image description here
以下是来自/etc/resolv.conf的内容容器内的文件:
enter image description here
以下是 docker 版本:
enter image description here
最新试验:
enter image description here
enter image description here

最佳答案

您的尝试有两个小错误:--hostname选项不会创建 DNS 条目。它只是在容器中设置主机名。
此外,使用默认桥接网络的容器,获取 /etc/resolv.conf 的副本来自主机的文件(这个 NS 对容器名称一无所知),而使用自定义网络的容器使用 Docker 的嵌入式 DNS 服务器,它将外部 DNS 查找转发到主机上配置的 DNS 服务器。
您可以使用 --name和/或 --net-alias以创建 DNS 条目。
三个命令优于 1000 个单词:

// Creating a custom docker bridge network
docker network create -d bridge so-demo

// Running the container in the network created above
docker run -it --network so-demo --name foo --net-alias bar --hostname foobar alpine:latest sh


// Check if the container name is resolved:
/ # nslookup foo
Server:     127.0.0.11
Address:    127.0.0.11:53
Non-authoritative answer:
Non-authoritative answer:
Name:   foo
Address: 172.22.0.2


// Check if the net-alias is resolved
/ # nslookup bar
Server:     127.0.0.11
Address:    127.0.0.11:53
Non-authoritative answer:
Non-authoritative answer:
Name:   bar
Address: 172.22.0.2


// The hostname is not resolved
/ # nslookup foobar
Server:     127.0.0.11
Address:    127.0.0.11:53
** server can't find foobar: NXDOMAIN
** server can't find foobar: NXDOMAIN


// ...the hostname is just set internally in the container: 
/ # hostname -f
foobar
/ # cat /etc/resolv.conf 
nameserver 127.0.0.11


关于docker - docker容器上的自定义名称解析失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63568043/

相关文章:

c# - 502 Bad Gateway nginx/1.14.1 elasticbeanstalk .net 核心应用程序在 docker 上运行

android - 我正在尝试编译我的 jnicode,它显示错误 _JNIEnv::GetShortArrayElements(JNIEnv*&, _jshortArray*&, int)'

linux - 使用 sendmail 脚本时获取空文件作为附件

java - Tomcat 缓存单点登录系统的旧 dns 条目

c# - Entity Framework 协会 : Error Because the Dependent Role properties are not the key properties

perl - 无法在 docker 中安装 perl 模块 PerlIO::locale

php - Windows上的Docker “Operation not permitted”问题

mysql - Dockerized spring boot 应用程序连接到主 MySQL

java - 在 ubuntu 中解析不同的 java 和 javac 版本

google-cloud-platform - 如何将 Google Cloud Platform 中的 'localhost' 添加为 OAuth2 的授权域