docker_practice/advanced_network/dns.md

49 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
Docker 没有为每个容器专门定制镜像,那么怎么自定义配置容器的主机名和 DNS 配置呢?
秘诀就是它利用虚拟文件来挂载到来容器的 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:17.10 cat etc/resolv.conf
nameserver 114.114.114.114
nameserver 8.8.8.8
```
如果用户想要手动指定容器的配置,可以利用下面的选项。
`-h HOSTNAME or --hostname=HOSTNAME`
设定容器的主机名,它会被写到容器内的 `/etc/hostname``/etc/hosts`。但它在容器外部看不到,既不会在 `docker ps` 中显示,也不会在其他的容器的 `/etc/hosts` 看到。
`--dns=IP_ADDRESS`
添加 DNS 服务器到容器的 `/etc/resolv.conf` 中,让容器用这个服务器来解析所有不在 `/etc/hosts` 中的主机名。
`--dns-search=DOMAIN`
设定容器的搜索域,当设定搜索域为 `.example.com` 时,在搜索一个名为 host 的主机时DNS 不仅搜索host还会搜索 `host.example.com`
注意:如果没有上述最后 2 个选项Docker 会默认用主机上的 `/etc/resolv.conf` 来配置容器。