简体   繁体   English

从容器访问本地主机网络而不暴露任何端口?

[英]Access local host network from container without exposing any port?

CONTEXT语境

I can access to some localhosts of other computers in my local network.我可以访问本地网络中其他计算机的一些本地主机。 For instance, one of them is called pandahost , so if I browse pandahost in my browser, I can access it.例如,其中之一叫做pandahost ,所以如果我在浏览器中浏览pandahost ,我可以访问它。

Currently I have been working with docker, and I have the following Docker-compose.yml:目前我一直在使用 docker,我有以下 Docker-compose.yml:

version: "3"
services: 
  myproject: 
    build: 
      context: .
      dockerfile: Dockerfile-for-my-project-environment
    shm_size: 2gb
    volumes: 
      - "D:/project/myproject1/folder:/myproject1"
      - "D:/project/myproject2:/myproject2"


I use this docker-compose file to run multiple instances of the service myproject , so I don't want to expose any port in order to avoid port conflicts.我使用这个 docker-compose 文件来运行服务myproject的多个实例,所以我不想暴露任何端口以避免端口冲突。

Up to this point, everything works fine for my except that I can't connect to pandahost from the service containers .到目前为止,一切对我来说都很好,除了我无法从服务容器连接到pandahost

QUESTION问题

Is there any way of connecting the service myproject container to pandahost without exposing any port and without breaking the possibility of having multiple instances of myproject service containers?有没有办法将服务myproject容器连接到pandahost而不会暴露任何端口并且不会破坏拥有多个myproject服务容器实例的可能性?

if you know the port that your container is opening you can simply fetch the IP address of the container using the following command:如果您知道容器打开的端口,您可以使用以下命令简单地获取容器的 IP 地址:

alias docker_ip_address="docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'"

docker_ip_address $container_ID

or more simply或更简单地说

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_ID

In this case I suggest you name your container using the container_name block in your docker-compose.yml to simplify the previous operation.在这种情况下,我建议您使用 docker-compose.yml 中的container_name块命名您的容器,以简化前面的操作。

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

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