简体   繁体   English

我怎么能从我的主机 ping 我的 docker 容器

[英]How could I ping my docker container from my host

I have created a ubuntu docker container on my mac我在我的 mac 上创建了一个 ubuntu docker 容器

CONTAINER ID  IMAGE   COMMAND      CREATED         STATUS         PORTS                 NAMES
5d993a622d23  ubuntu  "/bin/bash"  42 minutes ago  Up 42 minutes  0.0.0.0:123->123/tcp  kickass_ptolemy

I set port as 123.我将端口设置为 123。

My container IP is 172.17.0.2我的容器 IP 是172.17.0.2

docker inspect 5d993a622d23 | grep IP
"LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
                    "IPAMConfig": null,
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,

On my Mac I try to ping my container,在我的 Mac 上,我尝试 ping 我的容器,

Ping 172.17.0.2 , I got Request timeout for icmp_seq 0.... Ping 172.17.0.2 ,我收到 icmp_seq 0 的请求超时....

What should I do?我该怎么办? So my local machine can ping the container I installed.所以我的本地机器可以ping我安装的容器。 Did I missing some app installation on my container, which is a plain ubuntu system?我是否在我的容器上缺少一些应用程序安装,这是一个普通的 ubuntu 系统?

You can't ping or access a container interface directly with Docker for Mac . 您无法直接使用 Docker for Mac ping 或访问容器接口

The current best solution is to connect to your containers from another container.当前最好的解决方案是从另一个容器连接到您的容器。 At present there is no way we can provide routing to these containers due to issues with OSX that Apple have not yet resolved.目前,由于 Apple 尚未解决的 OSX 问题,我们无法为这些容器提供路由。 we are tracking this requirement, but we cannot do anything about it at present.我们正在跟踪此要求,但目前我们对此无能为力。

Docker Toolbox/VirtualBox Docker 工具箱/虚拟箱

When running Docker Toolbox, Docker Machine via VirtualBox or any VirtualBox VM (like a Vagrant definition ) you can setup a "Host-Only Network" and access the Docker VMs network via that.当通过 VirtualBox 或任何 VirtualBox VM(如Vagrant 定义)运行 Docker Toolbox、Docker Machine 时,您可以设置一个“Host-Only Network”并通过它访问 Docker VMs 网络。

If you are using the default boot2docker VM, don't change the existing interface as you will stop a whole lot of Docker utilities from working, add a new interface.如果您使用default boot2docker VM,请不要更改现有接口,因为您将停止大量 Docker 实用程序的工作,请添加新接口。

You will also need to setup routing from your Mac to the container networks via your VM's new IP address.您还需要通过 VM 的新 IP 地址设置从 Mac 到容器网络的路由。 In my case the Docker network range is 172.22.0.0/16 and the Host Only adapter IP on the VM is 192.168.99.100 .在我的例子中,Docker 网络范围是172.22.0.0/16 ,VM 上的 Host Only 适配器 IP 是192.168.99.100

sudo route add 172.22.0.0/16 192.168.99.100

Adding a permanent route to osx is bit more complexosx添加永久路由有点复杂

Then you can get to containers from your Mac然后你可以从你的 Mac 访问容器

machost:~ ping -c 1 172.22.0.2
PING 172.22.0.2 (172.22.0.2): 56 data bytes
64 bytes from 172.22.0.2: icmp_seq=0 ttl=63 time=0.364 ms

--- 172.22.0.2 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.364/0.364/0.364/0.000 ms

Vagrant + Ansible setup Vagrant + Ansible 设置

Here's my running config...这是我的运行配置...

Vagrant.configure("2") do |config|
  config.vm.box = "debian/contrib-buster64"
  config.vm.hostname = "docker"
  config.vm.network "private_network", ip: "10.7.7.7", hostname: true
  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "4000"
    vb.cpus = "4"
  end
  config.vm.provision "ansible" do |ansible|
    ansible.verbose = "v"
    ansible.playbook = "tasks.yaml"
  end
end

The ansible tasks.yaml to configure a fixed network. tasks.yaml配置固定网络。

- hosts: all
  become: yes
  vars:
    ansible_python_interpreter: auto_silent
    docker_config:
      bip: 10.7.2.1/23
      host: ["tcp://10.7.7.7:2375"]
      userland-proxy: false
  tasks:

  - ansible.builtin.apt:
      update_cache: yes
      force_apt_get: yes
      pkg:
      - bridge-utils
      - docker.io
      - python3-docker
      - python-docker
      - iptables-persistent

  - ansible.builtin.hostname:
      name: docker

  - ansible.builtin.copy:
      content: "{{ docker_config | to_json }}"
      dest: /etc/docker/daemon.json

  - ansible.builtin.lineinfile:
      line: 'DOCKER_OPTS="{% for host in docker_config.host %} -H {{ host }} {% endfor %}"'
      regexp: '^DOCKER_OPTS='
      path: /etc/default/docker

  - ansible.builtin.systemd:
      name: docker.service
      state: restarted
  
  - ansible.builtin.iptables:
      action: insert
      chain: DOCKER-USER
      destination: 10.7.2.0/23
      in_interface: eth1
      out_interface: docker0
      jump: ACCEPT
  - ansible.builtin.shell: iptables-save > /etc/iptables/rules.v4

Add the route for the docker bridge network via the VM to the mac通过VM添加docker bridge网络的路由到mac

$ sudo /sbin/route -n -v add -net 10.7.2.0/23 10.7.7.7

Then set DOCKER_HOST=10.7.7.7 in the environment to use the new VM.然后在环境中设置DOCKER_HOST=10.7.7.7使用新的VM。

$ export DOCKER_HOST=10.7.7.7 
$ docker run --name route_test --rm -d node:14-slim node -e "require('http').createServer((req, res) => {
 res.writeHead(200, {'Content-Type':'text/plain'})
 res.end('hello')
}).listen(3000)"
$ docker container inspect route_test -f '{{ .NetworkSettings.Networks.bridge.IPAddress }}'
$ curl http://10.7.2.3:3000
hello
$ docker rm -f route_test

You don't get volumes mapped from the host to the vm, but as a bonus it uses a lot less cpu than the Docker 2.5.x release.您不会将卷从主机映射到虚拟机,但作为奖励,它使用的 CPU 比 Docker 2.5.x 版本少得多。

As an alternative, if your container has a bash shell incorporated, you can access it through作为替代方案,如果您的容器包含一个 bash shell,您可以通过以下方式访问它

docker exec -it <CONTAINER ID> bash

and then you can ping your virtual ip然后你可以ping你的虚拟IP

It works in this scenario:它适用于这种情况:

  1. Windows host视窗主机
  2. Linux VM installed on Windows host安装在 Windows 主机上的 Linux VM
  3. Docker container installed on Linux VM host安装在 Linux VM 主机上的 Docker 容器

Now you have to note this.现在你必须注意这一点。 Containers are in a isolated network but connected to the internet throught your Docker container host adapter.So you have to tell kernel linux to be available in your network then in your Linux VM:容器位于隔离网络中,但通过 Docker 容器主机适配器连接到 Internet。因此,您必须告诉内核 linux 在您的网络中可用,然后在您的 Linux VM 中:

# sysctl net.ipv4.conf.all.forwarding=1
# sudo iptables -P FORWARD ACCEPT

Now in you Windows host you have to add a route for our container network: route add "Docker container network" "Linux VM IP" for example现在在您的 Windows 主机中,您必须为我们的容器网络添加一个路由:例如,路由添加“Docker 容器网络”“Linux VM IP”

# route add 172.17.0.0/16 192.168.1.20

setup:设置:

PC-A a is docker host, PC-B is a another PC in the network. PC-A a 是 docker 主机, PC-B是网络中的另一台 PC。 To ping/access docker's container from PC-B , run the below iptables -rules in the host.要从PC-B ping/访问 docker 的容器,请在主机中运行以下iptables -rules。

iptables -A FORWARD -i docker0 -o eth0 -j ACCEPT

iptables -A FORWARD -i eth0 -o docker0 -j ACCEPT

note: eth0 is host's interface and docker0 is docker's virtual default bridge.注意: eth0是主机的接口, docker0docker0的虚拟默认网桥。

Now add route in PC-B现在在PC-B添加路由

route add -net <dockerip> netmask <net mask> gw <docker's host>

ping/access container services directly.直接ping/访问容器服务。

Let's say you have W-> windows machine, L-Linux Vbox VM (eth0,eth1) and docker app (using port 8989) running on this L-Linux Vbox VM.假设您有 W-> windows 机器、L-Linux Vbox VM(eth0、eth1)和 docker 应用程序(使用端口 8989)在这个 L-Linux Vbox VM 上运行。 Provider does not have to Vbox anyway or W-> a win.You want to type http://app:8989 on your browser.There are two methods afak; Provider 无论如何都不需要 Vbox 或 W-> a win。你想在你的浏览器上输入http://app:8989 。有两种方法 afak; easy way to run vagrant automatically or manually configure Vbox VM with port forwarding through "Host-only Adapter" which is actually eth1;通过“仅主机适配器”(实际上是 eth1)使用端口转发自动或手动配置 Vbox VM 的简单方法; normally eth0 is Vbox's default reserved 10.0.2.15 IP assignment.Or on command prompt on win/lin/mac through "VBoxManage" command you can set up networks or automate through scripts.通常 eth0 是 Vbox 的默认保留 10.0.2.15 IP 分配。或者在 win/lin/mac 上的命令提示符下通过“VBoxManage”命令您可以设置网络或通过脚本自动化。

webtier.vm.network "forwarded_port", guest: 8989, host: 8989

run docker app运行泊坞窗应用

sudo docker run -p 8989:8989 ...

on windows explorer(W-> windows machine) browse your app在 Windows 资源管理器(W-> Windows 机器)上浏览您的应用

http://app:8989

You still can not ping "172.17.0.2" which is docker container IP in this situation from W-> windows machine.This could run cross-platform win/lin/mac.You might want to look into Vbox Manual and Vagrant Manual, particularly networks.在这种情况下,您仍然无法从 W-> windows 机器 ping docker 容器 IP“172.17.0.2”。这可以运行跨平台的 win/lin/mac。您可能需要查看 Vbox 手册和 Vagrant 手册,特别是网络。

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

相关问题 如何从Mac中的Docker容器外部访问? - How can I access from outside my docker container in mac? 在以--network host模式运行时,为什么不能从容器主机访问我的服务? - Why can't I access my service from my container host when running with `--network host` mode? docker attach:为什么我不能从Docker容器中分离出来? - docker attach: Why can't I detach from my Docker container? 如何从docker容器内访问OSX主机上的USB驱动器? - How do I access a USB drive on a OSX host from inside a docker container? 如何连接到在Mac上其他主机上运行的Docker容器? - How do I connect to a docker container running on a different host on a mac? Docker for mac 1.12.0:如何从容器连接到主机 - Docker for mac 1.12.0: how to connect to host from container 如何从其他主机访问在MacOSX上运行的docker容器? - How to access a docker container running on MacOSX from another host? 如何将数据从Docker容器获取到主机的共享文件夹中? - How to get data from docker container into shared folder at host? 如何在主机中查看容器内的真实进程? - How can I see the real process inside the container in my host machine? 我可以在MacBook Pro上使用Docker容器中的GPU吗? (AMD Radeon GPU) - Can I use my GPU from a docker container on a MacBook Pro ? (AMD Radeon GPU)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM