Ubuntu

每次啟動或停止 Docker 容器時都會覆蓋主機上的 resolv.conf

  • April 7, 2022

每次我啟動或停止任何 Docker 容器時,/run/systemd/resolve/resolv.conf主機(Ubuntu 19.04)上的文件都會被以下內容覆蓋:

# Generated by dhcpcd from enp30s0.dhcp
# /etc/resolv.conf.head can replace this line
nameserver 127.0.0.1
# /etc/resolv.conf.tail can replace this line

結果,在每次重新啟動之後以及在容器啟動或停止的任何時間之後,我都必鬚髮布netplan generatenetplan apply修復 resolv.conf 文件。

如何讓 docker 停止提升主機的 resolv.conf 文件?

我的網路計劃如下所示:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
   renderer: networkd
   ethernets:
       enp30s0:
           addresses:
           - 10.0.10.20/24
           dhcp4: no
           gateway4: 10.0.10.1
           nameservers:
               addresses:
               - 10.0.10.1
   version: 2

當我執行時systemd-resolve --status

Global
      LLMNR setting: no
MulticastDNS setting: no
 DNSOverTLS setting: no
     DNSSEC setting: no
   DNSSEC supported: no
        DNS Servers: 10.0.10.1

這些是我擁有的各種docker 容器

docker network create -d macvlan --subnet=10.0.10.0/24 --gateway=10.0.10.1 -o parent=enp30s0 macvlan-services-network


docker container create --name="pihole-kids" \
   --volume /opt/docker/volumes/pihole-kids/etc/pihole:/etc/pihole \
   --volume /opt/docker/volumes/pihole-kids/etc/dnsmasq.d:/etc/dnsmasq.d \
   --volume /opt/docker/volumes/pihole-kids/var/log/pihole.log:/var/log/pihole.log \
   --net macvlan-services-network \
   --ip 10.0.10.21 \
   --publish 53:53/tcp \
   --publish 53:53/udp \
   --publish 80:80 \
   --publish 443:443 \
   --dns=127.0.0.1 \
   --dns=10.0.10.20 \
   --add-host=restrict.youtube.com:216.239.38.120 \
   --add-host=restrictmoderate.youtube.com:216.239.38.119 \
   --add-host=forcesafesearch.google.com:216.239.38.120 \
   --add-host=strict.bing.com:204.79.197.220 \
   --add-host=safe.duckduckgo.com:34.243.144.154 \
   --env IPv6=False \
   --env ServerIP=10.0.10.21 \
   --env TZ="America/Chicago" \
   --restart=unless-stopped \
   pihole/pihole:latest



docker container create --name="pihole" \
   --volume /opt/docker/volumes/pihole/etc/pihole:/etc/pihole \
   --volume /opt/docker/volumes/pihole/etc/dnsmasq.d:/etc/dnsmasq.d \
   --volume /opt/docker/volumes/pihole/var/log/pihole.log:/var/log/pihole.log \
   --net=host \
   --env IPv6=False \
   --env ServerIP=10.0.10.20 \
   --env TZ="America/Chicago" \
   --dns=127.0.0.1 \
   --dns=10.0.10.1 \
   --restart=unless-stopped \
   pihole/pihole:latest


docker container create --name="emby-server" \
   --network="host" \
   --volume /opt/docker/volumes/emby/config:/config \
   --volume /mnt/media:/mnt/media \
   --device /dev/dri:/dev/dri \
   --runtime=nvidia \
   --publish 8096:8096 \
   --publish 8920:8920 \
   --env UID=997 \
   --env GID=997 \
   --env GIDLIST=1000 \
   --env NVIDIA_VISIBLE_DEVICES=all \
   --env NVIDIA_DRIVER_CAPABILITIES=compute,utility,video \
   emby/embyserver:latest


docker container create --name="nzbhydra2" \
   --volume /opt/docker/volumes/nzbhydra2/config:/config \
   --volume /mnt/Downloads:/mnt/Downloads \
   --volume /etc/localtime:/etc/localtime:ro \
   --publish 5076:5076 \
   --env UMASK=000 \
   --env PUID=1004 \
   --env PGID=1004 \
   binhex/arch-nzbhydra2


docker container create --name="portainer" \
   --volume /opt/docker/volumes/portainer/data:/data \
   --volume /var/run/docker.sock:/var/run/docker.sock \
   --publish 9000:9000 \
   --restart=always \
   portainer/portainer

弄清楚了!

在某些時候,dhcpcd安裝在我的系統上。不知道為什麼。在我決定刪除它之前,我編輯了/etc/dhcpcd.conf文件並將其替換127.0.0.110.0.10.1看是否/etc/resolv.conf會改變。我重新啟動,它確實改變了。我刪除dhcpcdapt purge dhcpcd5,重新啟動,現在名稱伺服器與我的網路計劃中的內容保持一致。

希望這對遇到同樣問題的其他人有所幫助。

我們剛剛解決了一個類似的問題。每次部署 docker 容器時,DNS 設置都會回到過時的設置。

該文件/etc/resolv.conf指向# This file is managed by man:systemd-resolved(8). Do not edit.

通過systemd-resolve --status我們在網路適配器中發現錯誤的 DNS 設置,在配置文件中更改它/etc/systemd/network並通過systemctl restart systemd-networkd.

碼頭工人停止/啟動後一切正常。

引用自:https://unix.stackexchange.com/questions/542528