简体   繁体   English

将 docker 容器连接到互联网

[英]Connect docker container to internet

I have created my docker image and container using the Dockerfile in this blog.我在此博客中使用 Dockerfile 创建了我的 docker 映像和容器。 https://hackernoon.com/raspberry-pi-cluster-emulation-with-docker-compose-xo3l3tyw https://hackernoon.com/raspberry-pi-cluster-emulation-with-docker-compose-xo3l3tyw

I am able to ssh into the rpi and ifconfig returns the following status:我能够 ssh 进入 rpi 并且 ifconfig 返回以下状态:

pi@raspberrypi:~ $ ifconfig

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        ether 52:54:00:12:34:56  txqueuelen 1000  (Ethernet)
        RX packets 561  bytes 49862 (48.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 386  bytes 47311 (46.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 41  base 0x1000    dma 0xff

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 2  bytes 100 (100.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2  bytes 100 (100.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Now I want to be able to connect this to internet.现在我希望能够将其连接到互联网。 As per docker docs, I tried to connect a running container to the internet using:根据 docker 文档,我尝试使用以下方法将正在运行的容器连接到互联网:

docker network connect multi-host-network 008796f5316a

It returns the error.它返回错误。 Error response from daemon: network multi-host-network not found.来自守护程序的错误响应:找不到网络多主机网络。

How can I connect to the internet from inside the docker?如何从 docker 内部连接到互联网?

Edit: The blog talks about running qemu on docker and installing a modified raspbian on top of that(for compatibility with qemu).编辑:该博客讨论了在 docker 上运行 qemu 并在其上安装修改后的 raspbian(为了与 qemu 兼容)。 When I access the container using docker exec command,当我使用 docker exec 命令访问容器时,

docker exec -it testnode bash

ifconfig returns the following: ifconfig 返回以下内容:

root@1f210520938c:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 637  bytes 356778 (356.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 404  bytes 39482 (39.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

And the ifconfig command for pi returns the results as shown above. pi 的 ifconfig 命令返回如上所示的结果。 So, ping google.com is working on the root@ and not on pi@raspberry.所以,ping google.com 是在 root@ 上工作,而不是在 pi@raspberry 上工作。

I need to connect the pi directly to the internet.我需要将 pi 直接连接到互联网。

This error occurs because you haven't created a Docker network called multi-host-network .发生此错误是因为您尚未创建名为multi-host-network的 Docker 网络。 To create it, you may refer to Docker docs .要创建它,您可以参考Docker 文档

By default, all docker containers are connected to the default bridge network (the name matches driver type).默认情况下,所有 docker 容器都连接到默认bridge网络(名称与驱动程序类型匹配)。 As for docker-compose, bridge network with name docker-compose-directory-name_default will be created, as described [here][2]. You can check it by running对于docker-compose, bridge network with name will be created, as described [here][2]. You can check it by running will be created, as described [here][2]. You can check it by running docker network inspect docker-compose-directory-name_default and find your container in Containers` section. will be created, as described [here][2]. You can check it by running and find your container in

Since your container is already connected to the internet, it should have internet access without explicitly connecting it to a network.由于您的容器已经连接到 Internet,因此它应该可以访问 Internet,而无需明确将其连接到网络。

If there is no internet connection, please try solutions proposed in this question .如果没有互联网连接,请尝试此问题中提出的解决方案。 I would suggest starting with我建议从

sysctl -w net.ipv4.ip_forward=1 # both on host and container
sudo service docker restart # (or sudo systemctl restart docker) on host

The instructions you're following set up QEMU to use "user-mode networking".您遵循的说明将 QEMU 设置为使用“用户模式网络”。 This is (sort of) like QEMU itself emulating a little NAT-router and private network that the VM lives behind.这(有点)就像 QEMU 本身模拟一个小型 NAT 路由器和虚拟机所在的专用网络。 This in turn is inside the docker container and talking to whatever the docker container's networking setup is, so you have two layers here -- you'll want to be clear about this double-layer setup so you don't get confused about which of the two might be the source of any problems you have.这又位于 docker 容器内,并与 docker 容器的网络设置进行通信,因此您在这里有两层——您需要清楚这个双层设置,以免混淆这两个可能是您遇到任何问题的根源。

User-mode networking has some limitations: notably, ping doesn't work, and you can't connect from the outside into the VM except where you have specific port-forwarding set up (the instructions include a port-forward from host port 2222 to the VM's ssh port 22).用户模式网络有一些限制:值得注意的是,ping 不起作用,并且您无法从外部连接到 VM,除非您设置了特定的端口转发(说明包括来自主机端口 2222 的端口转发到 VM 的 ssh 端口 22)。

So you need to figure out whether you can live with user-mode networking's limits, and make sure that whatever testing you're doing is testing what you care about and not things you probably don't really care about like whether ping packets in particular work.因此,您需要弄清楚您是否可以忍受用户模式网络的限制,并确保您所做的任何测试都是在测试您关心的内容,而不是您可能并不真正关心的事情,例如是否特别是 ping 数据包工作。 If you don't want user-mode networking, you'll need to set up a QEMU tap (bridge) network backend, which is a lot more complicated but does let you make a QEMU VM truly visible as a machine on the host network.如果您不想要用户模式网络,则需要设置 QEMU Tap(桥接)网络后端,这要复杂得多,但可以让 QEMU VM 真正作为主机网络上的机器可见.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM