docker_practice/network/dns.md

45 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

## 配置 DNS
如何自定义配置容器的主机名和 DNS 呢?秘诀就是 Docker 利用虚拟文件来挂载容器的 3 个相关配置文件。
在容器中使用 `mount` 命令可以看到挂载信息:
```bash
$ mount
/dev/disk/by-uuid/1fec...ebdf on /etc/hostname type ext4 ...
/dev/disk/by-uuid/1fec...ebdf on /etc/hosts type ext4 ...
tmpfs on /etc/resolv.conf type tmpfs ...
```
这种机制可以让宿主主机 DNS 信息发生更新后,所有 Docker 容器的 DNS 配置通过 `/etc/resolv.conf` 文件立刻得到更新。
配置全部容器的 DNS ,也可以在 `/etc/docker/daemon.json` 文件中增加以下内容来设置。
```json
{
"dns" : [
"114.114.114.114",
"8.8.8.8"
]
}
```
这样每次启动的容器 DNS 自动配置为 `114.114.114.114``8.8.8.8`。使用以下命令来证明其已经生效。
```bash
$ docker run -it --rm ubuntu:18.04 cat etc/resolv.conf
nameserver 114.114.114.114
nameserver 8.8.8.8
```
如果用户想要手动指定容器的配置,可以在使用 `docker run` 命令启动容器时加入如下参数:
`-h HOSTNAME` 或者 `--hostname=HOSTNAME` 设定容器的主机名,它会被写到容器内的 `/etc/hostname``/etc/hosts`。但它在容器外部看不到,既不会在 `docker container ls` 中显示,也不会在其他的容器的 `/etc/hosts` 看到。
`--dns=IP_ADDRESS` 添加 DNS 服务器到容器的 `/etc/resolv.conf` 中,让容器用这个服务器来解析所有不在 `/etc/hosts` 中的主机名。
`--dns-search=DOMAIN` 设定容器的搜索域,当设定搜索域为 `.example.com` 时,在搜索一个名为 host 的主机时DNS 不仅搜索 host还会搜索 `host.example.com`
>注意如果在容器启动时没有指定最后两个参数Docker 会默认用主机上的 `/etc/resolv.conf` 来配置容器。