英文:
Docker Container don't have network access
问题
我已经租用了来自Strato的VPS Linux VC4-8服务器,并想在上面部署一些Docker容器。不幸的是,已部署的容器无法访问互联网或解析DNS。因此,我无法在构建过程中运行像 RUN apk update 这样的命令。
我已经尝试了各种解决方案,但不幸的是都没有成功。以下是我已经阅读并尝试过的一些帖子:
- 
https://stackoverflow.com/questions/20430371/my-docker-container-has-no-internet
 - 
https://superuser.com/questions/1130898/no-internet-connection-inside-docker-containers
 - 
https://forums.docker.com/t/no-internet-access-in-the-docker-containers/108223
 - 
https://askubuntu.com/questions/1445229/no-network-access-from-within-docker-container
 
以下是从完全干净的服务器(Ubuntu 22.04)开始的示例,描述了我的问题:
- 
ping 8.8.8.8或ping google.com在主机上运行正常。 - 
使用官方说明(使用apt存储库)安装Docker。
 - 
运行
docker pull alpine和docker run --rm -it alpine:latest - 
连接到alpine容器
- 
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 - 
/ # 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:
- 
https://stackoverflow.com/questions/20430371/my-docker-container-has-no-internet
 - 
https://superuser.com/questions/1130898/no-internet-connection-inside-docker-containers
 - 
https://forums.docker.com/t/no-internet-access-in-the-docker-containers/108223
 - 
https://askubuntu.com/questions/1445229/no-network-access-from-within-docker-container
 
The following is an example of my problem starting from a completely clean server (Ubuntu 22.04):
- 
ping 8.8.8.8orping google.comworks flawlessly (on the host) - 
Install Docker using the official instructions (Install using the apt repository)
 - 
Run
docker pull alpineanddocker run --rm -it alpine:latest - 
Attach to the alpine Container
- 
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 - 
/ # ping google.com ping: bad address 'google.com' 
 - 
 
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 <container name> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论