Search code examples
dockernetwork-programmingdocker-composeport

running multiple services on same port in docker-compose


We have multiple microservices that are run on port 8080. As far as I know that only 1 service can run on port 8080.

Would it mean running the microservice as a port: 8080:8081 or 8081:8081?

Below are the services that we are trying to implement in docker-compose

reference-service:
  image: **
  ports:
    - "8080:8080"

test-service:
    image: **
    ports:
      - "8080:8081"

Solution

  • There are two kind of ports: container port and host port. Two processes cannot hold the same container port inside one container. You also cannot expose services' container ports to the same host port.

    However each service runs inside its own container so that both can use container port 8080.

    So that the following configuration is acceptable: you have two services, each is running in its own container and have container port 8080. Each of container ports are exposed to different host ports like this:

    reference-service:
      image: **
      ports:
        - "8080:8080"
    
    test-service:
        image: **
        ports:
          - "8081:8080"