简体   繁体   English

docker-compose 暴露远程容器上的端口

[英]docker-compose exposing ports on remote container

I run my docker-compose service remotely on another physical machine on my local network via -H=ssh://user@192.168.0.x .我通过-H=ssh://user@192.168.0.x在本地网络上的另一台物理机器上远程运行 docker-compose 服务。

I'm exposing ports using docker-compose:我正在使用 docker-compose 公开端口:

    ports:
      - 8001:8001

However, when I start the service, the port 8001 is not exposed to my localhost.但是,当我启动服务时,端口 8001 不会暴露给我的本地主机。 I can ssh into the machine running the container, and port 8001 is indeed listening there.我可以 ssh 进入运行容器的机器,并且端口 8001 确实在那里监听。

How do I instruct docker-compose to tunnel this port from the remote machine running the container, to my local docker client?如何指示 docker-compose 将此端口从运行容器的远程计算机隧道传输到我的本地 docker 客户端?

Docker doesn't have the ability to do this. Docker 没有能力做到这一点。 But from your ssh client's point of view, the container is no different from any other program running on the remote host, and you can use the ssh -L option to forward a local port to the remote system.但是从您的 ssh 客户端的角度来看,容器与远程主机上运行的任何其他程序没有什么不同,您可以使用ssh -L选项将本地端口转发到远程系统。

# Tell ssh to forward local port 8001 to remote port 8001
ssh -L 8001:localhost:8001 user@192.168.0.x \
  # Incidentally the remote port happens to be via a Docker container
  docker run -p 127.0.0.1:8001:8001 ...

Whenever you set DOCKER_HOST or use the docker -H option, you're giving instructions to a remote Docker daemon which interprets them relative to itself.每当您设置DOCKER_HOST或使用docker -H选项时,您都会向远程 Docker 守护程序发出指令,该守护程序相对于自身解释它们。 docker -H... -v... mounts a directory on the same system as the Docker daemon into a container; docker -H... -v...将目录与 Docker 守护程序安装到容器中; docker -H... -p... publishes a port on the same system as the Docker daemon. docker -H... -p...在与 Docker 守护程序相同的系统上发布端口。 Docker has no ability to somehow take into account the content or network stack of the local system when doing this. Docker 在执行此操作时无法以某种方式考虑本地系统的内容或网络堆栈。

(The one exception is docker -H... build , which actually creates a tar file of the local directory and sends it across the network to use as the build context, so you can have a remote Docker daemon build an image of a local source tree.) (一个例外是docker -H... build ,它实际上创建了一个本地目录的 tar 文件并将其通过网络发送以用作构建上下文,因此您可以让远程 Docker 守护进程构建本地目录的映像源树。)

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

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