简体   繁体   中英

Docker Per Network Port Mapping

I'm looking for a way to map the same port into 2 different ports, each for another container in a different network. consider the below docker-compose scenario:

services:

  web:
    build: .
    ports:
      - "8080:8080"
    networks:
      Net1:
      Net2:

  serv1:
    image: tomcat:7.0.92-jre8
    networks:
      Net1:
  serv2:
    image: tomcat:7.0.92-jre8
    networks:
      Net2:

Now what I would really like to do is to actually map the "web" service port 8080 so that serv1 could consume it as 8081 and serv2 will be using it as 8082.

Is that even possible?

Thanks

Ports are published to the host, not to docker networks, and not to other docker containers. So the above "8080:8080" maps port 8080 on the docker host into that container's port 8080.

For container-to-container communication, that happens using docker's internal DNS for service discovery, and the container port. So both serv1 and serv2 can connect to http://web:8080 to reach the web service on its container port. That in no way prevents serv1 and serv2 from listening within their own container on any ports they wish.

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