简体   繁体   English

如何将docker容器ip暴露给外网?

[英]How to expose the docker container ip to the external network?

i want to expose the container ip to the external network where the host is running so that i can directly ping the docker container ip from an external machine.我想将容器 ip 暴露给主机运行的外部网络,以便我可以直接从外部机器 ping docker 容器 ip。 If i ping the docker container ip from the external machine where the machine hosting the docker and the machine from which i am pinging are in the same network i need to get the response from these machines如果我从外部机器 ping docker 容器 ip,其中托管 docker 的机器和我从这些机器获得响应的机器在同一个网络中,我需要从这些机器获得响应

Pinging the container's IP (ie the IP it shows when you look at docker inspect [CONTAINER] ) from another machine does not work. 从另一台机器ping容器的IP(即,当您查看docker inspect [CONTAINER]时显示的IP)不起作用。 However, the container is reachable via the public IP of its host. 但是,可以通过其主机的公共IP访问容器。

In addition to Borja's answer, you can expose the ports of Docker containers by adding -p [HOST_PORT]:[CONTAINER_PORT] to your docker run command. 除了Borja的回答,您可以通过将-p [HOST_PORT]:[CONTAINER_PORT]-p [HOST_PORT]:[CONTAINER_PORT] docker run命令来公开Docker容器的端口。

Eg if you want to reach a web server in a Docker container from another machine, you can start it with docker run -d -p 80:80 httpd:alpine . 例如,如果您想从另一台机器到达Docker容器中的Web服务器,可以使用docker run -d -p 80:80 httpd:alpine启动它。 The container's port 80 is then reachable via the host's port 80 . 容器的端口80是然后通过主机的端口可达80 Other machines on the same network will then also be able to reach the webserver in this container (depending on Firewall settings etc. of course...) 然后,同一网络上的其他计算机也可以访问此容器中的Web服务器(当然,取决于防火墙设置等...)

Since you tagged this as 由于您将此标记为 :

You cannot directly send packets to individual Docker containers. 您无法直接将数据包发送到各个Docker容器。 You need to send them to somewhere else that's able to route them. 您需要将它们发送到能够路由它们的其他地方。 In the case of plain Docker, you need to use the docker run -p option to publish a port to the host, and then containers will be reachable via the published port via the host's IP address or DNS name. 在普通Docker的情况下,您需要使用docker run -p选项将端口发布到主机,然后可以通过主机的IP地址或DNS名称通过发布的端口访问容器。 In a Kubernetes context, you need to set up a Service that's able to route traffic to the Pod (or Pods) that are running your container, and you ultimately reach containers via that Service. 在Kubernetes上下文中,您需要设置一个服务,该服务能够将流量路由到运行容器的Pod(或Pod),并最终通过该服务访问容器。

The container-internal IP addresses are essentially useless in many contexts. 在许多情况下,容器内部IP地址基本上是无用的。 (They cannot be reached from off-host at all; in some environments you can't even reach them from outside of Docker on the same host.) There are other mechanisms you can use to reach containers ( docker run -p from outside Docker, inter-container DNS from within Docker) and you never need to look up these IP addresses at all. (根本不能从离开主机访问它们;在某些环境中,您甚至无法从同一主机上的Docker外部访问它们。)您可以使用其他机制来访问容器( docker run -p从Docker外部docker run -p ,来自Docker内的容器间DNS),您根本不需要查找这些IP地址。

Your question places a heavy emphasis on ping (1). 你的问题非常强调ping (1)。 This is a very-low-level debugging tool that uses a network protocol called ICMP. 这是一个非常低级的调试工具,它使用名为ICMP的网络协议。 If sending packets using ICMP is actually core to your workflow, you will have difficulty running it in Docker or Kubernetes. 如果使用ICMP发送数据包实际上是您工作流程的核心,那么在Docker或Kubernetes中运行它将会很困难。 I suspect you aren't actually. 我怀疑你实际上并不是。 Don't worry so much about being able to directly ping containers; 不要太担心能够直接ping容器; use higher-level tools like curl (1) if you need to verify that a request is reaching its container. 如果需要验证请求是否到达其容器,请使用curl (1)等更高级别的工具。

It's pretty easy actually, assuming you have control over the routing tables of your external devices (either directly, or via your LAN's gateway/router).实际上这很容易,假设您可以控制外部设备的路由表(直接或通过 LAN 的网关/路由器)。 Assuming your containers are using a bridge network of 172.17.0.0/16, you add a static entry for the 172.17.0.0/16 network, with your Docker physical LAN IP as the gateway.假设您的容器使用172.17.0.0/16的桥梁网络,则在172.17.0.0/16网络中添加static条目,带有您的ZC5214CDD2B3B3B3B3B42B3B42B42BBAR。 You might need to also allow this forwarding in your Docker OS firewall configuration.您可能还需要在 Docker OS 防火墙配置中允许此转发。

After that, you should be able to connect to your docker container using its bridge address (172.17.0.2 for example).之后,您应该能够使用其桥地址(例如 172.17.0.2)连接到您的 docker 容器。 Note however that it will likely not respond to pings, due to the container's firewall.但是请注意,由于容器的防火墙,它可能不会响应 ping。

If you're content to access your container using only the bridge IP (and never again use your Docker host IP with the mapped-port), you can remove port mapping from the container entirely.如果您满足于仅使用桥 IP 访问您的容器(并且不再使用您的 Docker 主机 IP 和映射端口),您可以完全从容器中删除端口映射。

You need to create a new bridge docker network and attach the container to this network.您需要创建一个新的网桥 docker 网络并将容器附加到该网络。 You should be able to connect by this way.您应该可以通过这种方式进行连接。

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

相关问题 如何仅将docker容器暴露给专用网络 - How to expose docker container only to private network 将我的Docker容器暴露于GCP上的外部IP - Expose my docker container to my external IP on GCP 将Docker容器公开给公共IP - Expose Docker container to public ip 如何将主机网络暴露给 docker-compose 中的 docker 容器? - How to expose host network to docker container in docker-compose? 如何暴露 Docker 容器 - How to expose a Docker container 如何将 static IP 设置到我的容器中,以便在 docker-compose 中预先存在 external.network? - How to set a static IP to my container for a pre-existent external network in docker-compose? 如何从Rancher的专用网络中公开docker容器? - How to expose docker container from Rancher's private network? 如何将外部IP添加到Docker容器? - How to add an external IP to a Docker container? 当容器连接到 multiple.networks 时,如何仅将 Docker 容器端口暴露给一个特定的 Docker.network? - How to expose a Docker container port to one specific Docker network only, when a container is connected to multiple networks? 如果docker容器ip和外部网络ip相同,那么如果执行telnet,则哪个响应? 任何想法 - If docker container ip and external network ip is same, then which one will get respond if will do telnet? any idea
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM