Home > Mobile >  Docker port mapping weirdness
Docker port mapping weirdness

Time:01-10

I am using this docker-compose file:

version: "3.7"

services:
  mautrix-wsproxy:
    container_name: mautrix-wsproxy
    image: dock.mau.dev/mautrix/wsproxy
    restart: unless-stopped
    ports:
      - 29331
    environment:
      #LISTEN_ADDRESS: ":29331"
      APPSERVICE_ID: imessage
      AS_TOKEN: put your as_token here
      HS_TOKEN: put your hs_token here
      # These URLs will work as-is with docker networking
      SYNC_PROXY_URL: http://mautrix-syncproxy:29332
      SYNC_PROXY_WSPROXY_URL: http://mautrix-wsproxy:29331
      SYNC_PROXY_SHARED_SECRET: ...

  mautrix-syncproxy:
    container_name: mautrix-syncproxy
    image: dock.mau.dev/mautrix/syncproxy
    restart: unless-stopped
    environment:
      #LISTEN_ADDRESS: ":29332"
      DATABASE_URL: postgres://user:pass@host/mautrixsyncproxy
      HOMESERVER_URL: http://localhost:8008
      SHARED_SECRET: ...

But then docker ps shows

... dock.mau.dev/mautrix/wsproxy ... 0.0.0.0:49156->29331/tcp, :::49156->29331/tcp

And I have to use external port 49156 to connect to its internal port 29331.

Where the heck did this 49156 come from? How do I map it so it's 29331->29331 ?

docker inspect shows:

        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": ...,
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "29331/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "49156"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "49156"
                    }
                ]
            },

CodePudding user response:

In addition to the provided answers, if you want a fix port mapping, you can do this by providing 2 ports separated by a colon with the publish flag or in the ports array when using compose. The left part is the mapped port available on the host system, and the right part is the port inside the container.

# make port 29331 inside the container available on port 8080 on the host system
docker run --publish 8080:29331 busybox

In your case, to answer your question.

How do I map it so it's 29331->29331 ?

services:
  mautrix-wsproxy:
    ports:
      - "29331:29331"
    ...

CodePudding user response:

From the docs for ports:

There are three options:

  • Specify both ports (HOST:CONTAINER)
  • Specify just the container port (an ephemeral host port is chosen for the host port).
  • ....

You're using the 2nd option, and just specifying the container port that you want to expose. Since you didn't specify a host port to map that to, "an ephemeral host port is chosen".

CodePudding user response:

Looking at the documentation for ports in a compose file (docs.docker.com) we can read the following:

Short syntax

There are three options:

...

  • Specify just the container port (an ephemeral host port is chosen for the host port).

...

This means in essence that a random, free host port is chosen.

To explicitly map the container-port on a known host-port (even if it s the same as the container port), we use the HOST:CONTAINER syntax (see link above):

version: "3.7"

services:
  mautrix-wsproxy:
    ...
    ports:
      - "29331:29331"
    ...
  •  Tags:  
  • Related