0

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

1
  • May I know which web server you are using? Commented Apr 24, 2019 at 8:43

1 Answer 1

5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.