Docker容器没有网络访问。

huangapple go评论90阅读模式
英文:

Docker Container don't have network access

问题

我已经租用了来自Strato的VPS Linux VC4-8服务器,并想在上面部署一些Docker容器。不幸的是,已部署的容器无法访问互联网或解析DNS。因此,我无法在构建过程中运行像 RUN apk update 这样的命令。

我已经尝试了各种解决方案,但不幸的是都没有成功。以下是我已经阅读并尝试过的一些帖子:

以下是从完全干净的服务器(Ubuntu 22.04)开始的示例,描述了我的问题:

  1. ping 8.8.8.8ping google.com 在主机上运行正常。

  2. 使用官方说明(使用apt存储库)安装Docker。

  3. 运行 docker pull alpinedocker run --rm -it alpine:latest

  4. 连接到alpine容器

    1. PING 8.8.8.8 (8.8.8.8): 56 data bytes
      ^C
      --- 8.8.8.8 ping statistics ---
      30 packets transmitted, 0 packets received, 100% packet loss
      
    2. / # ping google.com
      ping: bad address 'google.com'
      

我还尝试了在另一台服务器(来自Hetzner)上执行这些步骤,那里一切正常。文件,如 /etc/resolv.conf 在两台服务器上看起来几乎一样。

Strato服务器上的resolv.conf:

nameserver 127.0.0.53 
options edns0 trust-ad 
search .

Hetzner服务器上的resolv.conf:

nameserver 127.0.0.53
options edns0 trust-ad

默认的 'bridge' 网络在运行 docker inspect bridge 时看起来完全相同。当我使用 docker inspect <container name> 比较两台机器上的容器时,一切看起来都一样。唯一的区别是运行示例的 CgroupnsMode 属性设置为 host,而在Strato服务器上设置为 private(但我不确定这是否与连接有关)。

如果我使用 --network host 选项启动容器,我可以成功设置容器并从容器内部访问互联网,但之后我的Django容器无法找到Postgres容器。我也认为这不是首选的方法。

有人知道我在Strato服务器上使用Docker遇到这些问题的原因吗?
我真的不知道还能尝试什么。

提前感谢任何帮助!
Brian

英文:

I have rented a VPS Linux VC4-8 server from Strato and would like to deploy a few Docker containers on it.
Unfortunately, the deployed containers do not have internet access or cannot resolve DNS.
As a result, I can't run commands like RUN apk update
in the build process.

I have already tried various solutions, but unfortunately without success. Here are a few threads that I have already read and tried:

The following is an example of my problem starting from a completely clean server (Ubuntu 22.04):

  1. ping 8.8.8.8 or ping google.com works flawlessly (on the host)

  2. Install Docker using the official instructions (Install using the apt repository)

  3. Run docker pull alpine and docker run --rm -it alpine:latest

  4. Attach to the alpine Container

    1. PING 8.8.8.8 (8.8.8.8): 56 data bytes
      ^C
      --- 8.8.8.8 ping statistics ---
      30 packets transmitted, 0 packets received, 100% packet loss
      
    2. / # ping google.com
      ping: bad address &#39;google.com&#39;
      

I also tried these steps on another server (from Hetzner) and everything worked normally there.
The files, such as /etc/resolv.conf look almost the same on both servers.

resolv.conf on the Strato Server:

nameserver 127.0.0.53 
options edns0 trust-ad 
search .

resolv.conf on the Hetzner Server

nameserver 127.0.0.53
options edns0 trust-ad

The default 'bridge' network looks exactly the same when I run docker inspect bridge.
And when I compare the Container from both machines with docker inspect &lt;container name&gt; everything looks the same. Only the CgroupnsMode property is set to host on the running example and to private on the Strato Server (but I am not sure if this is a problem regarding the connectivity).

If I start the container with the --network host option, I can successfully set up the containers and also access the internet from inside the containers, but after that my Django container cannot find the Postgres container. I also think that this is not the preferred way to do this.

Does anyone have any idea what could be the reason that I have these problems with Docker on the Strato server?
I really don't know what else to try.

Thanks in advance for any help!

br, Brian

答案1

得分: 4

来自Docker论坛的信息链接: https://forums.docker.com/t/docker-bridge-networking-does-not-work-in-ubuntu-22-04/136326/6

问题出在netplan上,Ubuntu 22.04使用netplan通过DHCP分配IP地址。您需要修改文件/etc/netplan/50-cloud-init.yaml,在*前面添加en

network:
    ethernets:
        all:
            dhcp4: true
            dhcp6: true
            match:
                name: 'en*'
    renderer: networkd
    version: 2

这将强制netplan仅在en*接口上使用DHCP,而Docker接口是docker0。

接下来应用配置并重新启动网络:

netplan apply
sudo systemctl restart systemd-networkd

需要重新启动Docker以请求新的IP地址:

sudo systemctl restart docker

对我的Strato V-Server上的Ubuntu 22.04有效。

英文:

from the docker forum: https://forums.docker.com/t/docker-bridge-networking-does-not-work-in-ubuntu-22-04/136326/6

> The problem is netplan, ubuntu 22.04 use netplan to adress ip via
> DHCP. You need to modify the file /etc/netplan/50-cloud-init.yaml and
> add en in front of the * :
>
> network:
> ethernets:
> all:
> dhcp4: true
> dhcp6: true
> match:
> name: 'en*'
> renderer: networkd
> version: 2
>
> this will force netplan to use DHCP only on interface en* and the
> docker interface is docker0
>
> next apply the configuration and restart network:
>
> netplan apply
> sudo systemctl restart systemd-networkd
>
> #Docker need to be restarted to request a new ip
> sudo systemctl restart docker

Worked for my Strato V-Server with ubuntu 22.04.

huangapple
  • 本文由 发表于 2023年6月9日 07:23:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定