Skip to content

[BUG] docker engine and desktop expose random ports differently #12846

Description

@alexanderankin

Description

exposing ports with a random mapping should be able to be described using docker compose language portably across docker engine and docker desktop environments. however, different options need to be supplied in order to achieve the goal.

services:
  alpine:
    image: nginx:alpine-slim
    init: true
    ports:
      - '81'
      - '82'
      - target: 80
        host_ip: 127.0.0.1
        protocol: tcp
    command:
      - sh
      - -c
      - 'd=/etc/nginx/conf.d; echo "server { listen 81; location / { return 202; } }" > $$d/81.conf && echo "server { listen 82; location / { return 204; } }" > $$d/82.conf && nginx -g "daemon off;"'

  alpine2:
    image: nginx:alpine-slim
    init: true
    ports:
      - target: 80
        host_ip: 127.0.0.1
        protocol: tcp
    command:
      - sh
      - -c
      - 'd=/etc/nginx/conf.d; echo "server { listen 81; location / { return 202; } }" > $$d/81.conf && echo "server { listen 82; location / { return 204; } }" > $$d/82.conf && nginx -g "daemon off;"'

e.g. this works on mac but on linux i also have to add (next to both host_ip fields) a published field and give it a range.

Steps To Reproduce

  1. copy this compose.yaml file onto two computers, one linux (ubuntu 24/Docker version 28.0.4, build b8034c0), one mac (Docker version 27.5.1, build 9f9e405)
  2. cd into the respective directories and docker compose up -d in both
  3. observe linux not works (until adding published)
  4. observe mac works

Compose Version

Docker Compose version v2.32.4-desktop.1
Docker Compose version v2.34.0

Docker Environment

$ docker info | tr '\n' '|' && echo
Client: Docker Engine - Community| Version:    28.0.4| Context:    default| Debug Mode: false| Plugins:|  buildx: Docker Buildx (Docker Inc.)|    Version:  v0.22.0|    Path:     /usr/libexec/docker/cli-plugins/docker-buildx|  compose: Docker Compose (Docker Inc.)|    Version:  v2.34.0|    Path:     /usr/libexec/docker/cli-plugins/docker-compose||Server:| Containers: 2|  Running: 0|  Paused: 0|  Stopped: 2| Images: 10| Server Version: 28.0.4| Storage Driver: overlay2|  Backing Filesystem: extfs|  Supports d_type: true|  Using metacopy: false|  Native Overlay Diff: true|  userxattr: false| Logging Driver: json-file| Cgroup Driver: systemd| Cgroup Version: 2| Plugins:|  Volume: local|  Network: bridge host ipvlan macvlan null overlay|  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog| Swarm: inactive| Runtimes: io.containerd.runc.v2 runc| Default Runtime: runc| Init Binary: docker-init| containerd version: 753481ec61c7c8955a23d6ff7bc8e4daed455734| runc version: v1.2.5-0-g59923ef| init version: de40ad0| Security Options:|  apparmor|  seccomp|   Profile: builtin|  cgroupns| Kernel Version: 6.8.0-59-generic| Operating System: Ubuntu 24.04.2 LTS| OSType: linux| Architecture: x86_64| CPUs: 16| Total Memory: 27.33GiB| Name: mc-nuc-1| ID: 66bd24bd-1a62-420e-b7c9-05e7bca3d18e| Docker Root Dir: /var/lib/docker| Debug Mode: false| Experimental: false| Insecure Registries:|  ::1/128|  127.0.0.0/8| Live Restore Enabled: false||
$ docker info | tr '\n' '|' && echo
WARNING: daemon is not using the default seccomp profile
Client:| Version:    27.5.1| Context:    desktop-linux| Debug Mode: false| Plugins:|  ai: Ask Gordon - Docker Agent (Docker Inc.)|    Version:  v0.7.3|    Path:     /Users/toor/.docker/cli-plugins/docker-ai|  buildx: Docker Buildx (Docker Inc.)|    Version:  v0.20.1-desktop.2|    Path:     /Users/toor/.docker/cli-plugins/docker-buildx|  compose: Docker Compose (Docker Inc.)|    Version:  v2.32.4-desktop.1|    Path:     /Users/toor/.docker/cli-plugins/docker-compose|  debug: Get a shell into any image or container (Docker Inc.)|    Version:  0.0.38|    Path:     /Users/toor/.docker/cli-plugins/docker-debug|  desktop: Docker Desktop commands (Beta) (Docker Inc.)|    Version:  v0.1.4|    Path:     /Users/toor/.docker/cli-plugins/docker-desktop|  dev: Docker Dev Environments (Docker Inc.)|    Version:  v0.1.2|    Path:     /Users/toor/.docker/cli-plugins/docker-dev|  extension: Manages Docker extensions (Docker Inc.)|    Version:  v0.2.27|    Path:     /Users/toor/.docker/cli-plugins/docker-extension|  feedback: Provide feedback, right in your terminal! (Docker Inc.)|    Version:  v1.0.5|    Path:     /Users/toor/.docker/cli-plugins/docker-feedback|  init: Creates Docker-related starter files for your project (Docker Inc.)|    Version:  v1.4.0|    Path:     /Users/toor/.docker/cli-plugins/docker-init|  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)|    Version:  0.6.0|    Path:     /Users/toor/.docker/cli-plugins/docker-sbom|  scout: Docker Scout (Docker Inc.)|    Version:  v1.16.1|    Path:     /Users/toor/.docker/cli-plugins/docker-scout||Server:| Containers: 9|  Running: 2|  Paused: 0|  Stopped: 7| Images: 31| Server Version: 27.5.1| Storage Driver: overlay2|  Backing Filesystem: extfs|  Supports d_type: true|  Using metacopy: false|  Native Overlay Diff: true|  userxattr: false| Logging Driver: json-file| Cgroup Driver: cgroupfs| Cgroup Version: 2| Plugins:|  Volume: local|  Network: bridge host ipvlan macvlan null overlay|  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog| CDI spec directories:|  /etc/cdi|  /var/run/cdi| Swarm: inactive| Runtimes: io.containerd.runc.v2 runc| Default Runtime: runc| Init Binary: docker-init| containerd version: bcc810d6b9066471b0b6fa75f557a15a1cbf31bb| runc version: v1.1.12-0-g51d5e946| init version: de40ad0| Security Options:|  seccomp|   Profile: unconfined|  cgroupns| Kernel Version: 6.12.5-linuxkit| Operating System: Docker Desktop| OSType: linux| Architecture: aarch64| CPUs: 2| Total Memory: 3.828GiB| Name: docker-desktop| ID: 197e6f69-ae34-44ec-85b6-fc01767f361b| Docker Root Dir: /var/lib/docker| Debug Mode: false| HTTP Proxy: http.docker.internal:3128| HTTPS Proxy: http.docker.internal:3128| No Proxy: hubproxy.docker.internal| Labels:|  com.docker.desktop.address=unix:///Users/toor/Library/Containers/com.docker.docker/Data/docker-cli.sock| Experimental: false| Insecure Registries:|  hubproxy.docker.internal:5555|  127.0.0.0/8| Live Restore Enabled: false||

Anything else?

No response

Activity

  1. ndeloof commented on May 19, 2025

    @ndeloof
    Contributor

    I can't reproduce

    root@consonant-teal:~# docker compose up -d
    [+] Running 3/3
     ✔ Network root_default      Created                                                                                                                                                                   0.0s 
     ✔ Container root-alpine-1   Started                                                                                                                                                                   0.2s 
     ✔ Container root-alpine2-1  Started                                                                                                                                                                   0.2s 
    root@consonant-teal:~# docker ps
    CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS         PORTS                                                                                                           NAMES
    97ba3c8f2a60   nginx:alpine-slim   "/docker-entrypoint.…"   3 seconds ago   Up 2 seconds   127.0.0.1:32775->80/tcp                                                                                         root-alpine2-1
    66c2abf9e45b   nginx:alpine-slim   "/docker-entrypoint.…"   3 seconds ago   Up 2 seconds   127.0.0.1:32774->80/tcp, 0.0.0.0:32776->81/tcp, [::]:32776->81/tcp, 0.0.0.0:32777->82/tcp, [::]:32777->82/tcp   root-alpine-1
    

    Are you able to reproduce with a plain docker run -p 127.0.0.1::80 ... ?

  2. alexanderankin commented on May 19, 2025

    @alexanderankin
    Author

    this works fine docker run -p 127.0.0.1::80 nginx:alpine-slim, but the compose file above does not.

  3. alexanderankin commented on May 19, 2025

    @alexanderankin
    Author

    maybe it is a race condition - two containers both vying for port 80?

  4. ndeloof commented on May 19, 2025

    @ndeloof
    Contributor

    ah ok, so your issue is related to 2 random ports being allocated by engine. This is similar to #8530, which has been fixed by making calls to ContainerStart sequential within a service with replica. Would need to do the same for all services.
    I haven't found a related issue in github.com/moby/moby but this deserves one

  5. robmry commented on May 22, 2025

    @robmry
    Contributor

    I don't think the issue was a race between containers starting, so #12851 is unlikely to help. But, moby/moby#50054 should fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions