Docker 沒有為每個容器專門定制映像檔,那麽怎麽自訂設定容器的主機名和 DNS 設定呢? 秘訣就是它利用虛擬檔案來掛載到來容器的 3 個相關設定檔案。
在容器中使用 mount 命令可以看到掛載訊息:
$ 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 檔案立刻得到更新。