Search code examples
dockerdocker-composeports

Will docker-compose allow mapping a port to two ports or do I need an ambassador?


Suppose I have an application that listens on 8888 - other parts of the application want to continue to access it on 8888 - but the external users need to access it at a port range above 50000 - eg 50888.

What I'd like to do in my docker-compose.yml is:

ports:
  - "8888:8888"
  - "50888:8888"

Will this work?

My other alternative is to add an ambassador in there like this:

blah:
  image: blah:6
  ports:
    - "8888:8888"
  container_name: blah
  networks:
    default: {}
blah_ambassador:
  image: svendowideit/ambassador
  links:
    - blah
  ports:
    - "50888:8888"
  environment:
    - BLAH_PORT_8888_TCP:tcp://blah:8888 
  container_name: ops_ambassador
  networks:
    default: {}

My question is: Will docker-compose allow mapping a port to two ports or do I need an ambassador?


Solution

  • Some time ago, docker-compose used a dictionary to store the mapping ports and the key was the internal port, so one value overrode the other.

    This was fixed here using a list. So, currently, docker-compose allows mapping an internal port to two ports. Maybe you are using an older docker-compose version.

    Example:

    → docker-compose -v
    docker-compose version 1.8.0, build f3628c7
    

    Docker-compose file content (docker-compose.yml):

    backend:
      image: your_image
      ports:
        - 3000:3000
        - 8888:3000
    

    docker inspect command: docker inspect your_container_id

    "Ports": {
                "3000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8888"
                    },
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "3000"
                    }
                ]
            },