简体   繁体   中英

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.

Currently I have been working with docker, and I have the following 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.

Up to this point, everything works fine for my except that I can't connect to pandahost from the service containers .

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?

if you know the port that your container is opening you can simply fetch the IP address of the container using the following command:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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