Search code examples
dockertcpdocker-composemultiple-instancesports

docker-compose referencing wrong ports


I am running multiple docker-compositions on one host (identical images for different usecases).

For that reason I use different HTTPS (+REST) ports the compositions are available remotely under. However, docker will reference the port range of the first composition in every other composition as well, but not use it. Although I cannot see any negative implication atm, I would like to get rid of it, fearing that some implication might eventually arise.

docker ps shows this

PORTS     
Second container:                                        
**8643-8644/tcp**, 0.0.0.0:8743-8744->8743-8744/tcp   
0.0.0.0:27020->27020/tcp                          

First container:
0.0.0.0:**8643-8644->8643-8644**/tcp                  
0.0.0.0:27019->27019/tcp 

First docker-compose file (excerpt):

version: '2'
services:
  mongo:
    image: *****
    ports:
      - "27019:27019"
    tty: true
    volumes:
      - /data/mongodb
      - /data/db
      - /var/log/mongodb
    entrypoint: [ "/usr/bin/mongod", "--port", "27019" ]
  rom:
    image: *****
    links:
      - mongo
    ports:
      - "8643:8643"
      - "8644:8644"
    environment:
      WEB_PORT_SECURE: 8643
      REST_PORT_SECURE: 8644
      MONGO_PORT: 27019
      MONGO_INST: mongod
    entrypoint: [ "node", "/usr/src/app/app.js" ]

Second docker-compose file (excerpt):

version: '2'
services:
    mongo:
        image: *****
        ports:
          - "27020:27020"
        tty: true
        volumes:
          - /data/mongodb
          - /data/db
          - /var/log/mongodb
        entrypoint: [ "/usr/bin/mongod", "--port", "27020" ]
      rom:
        image: *****
        links:
          - mongo
        ports:
          - "8743:8743"
          - "8744:8744"
        environment:
          WEB_PORT_SECURE: 8743
          REST_PORT_SECURE: 8744
          MONGO_PORT: 27020
          MONGO_INST: mongod
        entrypoint: [ "node", "/usr/src/app/app.js" ]

and finally, docker inspect shows this for the second container

"Config": {
    "Hostname": *****,
    "Domainname": "",
    "User": "",
    "AttachStdin": false,
    "AttachStdout": false,
    "AttachStderr": false,
    "ExposedPorts": {
        "8643/tcp": {},
        "8644/tcp": {},
        "8743/tcp": {},
        "8744/tcp": {}
    },
    "Tty": false,
    "OpenStdin": false,
    "StdinOnce": false,
    "Env": [
        "MONGO_PORT=27020",
        "MONGO_INST=mongodb",
        "WEB_PORT_SECURE=8743",
        "REST_PORT_SECURE=8744",
        "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
        "NPM_CONFIG_LOGLEVEL=info",
        "NODE_VERSION=4.7.2",
        "WORK_DIR=/usr/src/app"
    ],

"NetworkSettings": {
"Ports": {
    "8643/tcp": null,
    "8644/tcp": null,
    "8743/tcp": [
        {
            "HostIp": "0.0.0.0",
            "HostPort": "8743"
        }
    ],
    "8744/tcp": [
        {
            "HostIp": "0.0.0.0",
            "HostPort": "8744"
        }
    ]
},

The last block clearly shows that docker is not doing anything with the 8643 and 8644 port, but still references it there.

"8643/tcp": null,
"8644/tcp": null,

Any idea why this happens and how to avoid it?


Solution

  • They are there because the image exposes them (built with EXPOSE).

    This is not a problem, it's totally normal. You won't have a problem unless you try to export the same port on the outside host more than once. Here, none of your exported ports are in conflict.

    0.0.0.0:8743-8744->8743-8744/tcp   
    0.0.0.0:27020->27020/tcp                          
    0.0.0.0:8643-8644->8643-8644/tcp                  
    0.0.0.0:27019->27019/tcp 
    

    You are exporting 8643-8644, 8743-8744, 27019, 27020. No conflicts.

    A container can expose whatever ports it wants, it is only important that exposed ports are not in conflict with one another.