Expose same port to two containers?

Run first continaer in port 5432:

docker run --name my_first_postgres_9.6 -d -p myaccount/my_postgres_9.6:1.0.0


Now I stop container:

docker stop my_first_postgres_9.6

Now create second container

docker run --name my_first_postgres_9.6 -d -p myaccount/my_postgres_9.6:1.0.0

But I get error:

docker: Error response from daemon: Ports are not available: exposing port TCP -> listen tcp bind: address already in use.

I need to start second container on the same port.

Is it possible.

Smt like this:

Start first container on port 5432:
Work with first container.
Stop first container on port 5432
Start second container

Is it possible?

P.S. Delete first container is not my case.

Please, share the output of the following commands:

docker info
docker version

Stopped containers shouldn’t be a problem when you start another container using the same port, you will just not be able to run both containers at the same time. I couldn’t reproduce your issue with Docker 23.0.1 and Docker 20.10.23 (Docke Desktop for Mac).

1 Like

docker version
Client: Docker Engine - Community
 Cloud integration: v1.0.29
 Version:           23.0.1
 API version:       1.41 (downgraded from 1.42)
 Go version:        go1.19.5
 Git commit:        a5ee5b1
 Built:             Thu Feb  9 19:46:56 2023
 OS/Arch:           linux/amd64
 Context:           desktop-linux

Server: Docker Desktop 4.16.2 (95914)
  Version:          20.10.22
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.9
  Git commit:       42c8b31
  Built:            Thu Dec 15 22:26:14 2022
  OS/Arch:          linux/amd64
  Experimental:     false
  Version:          1.6.14
  GitCommit:        9ba4b250366a5ddde94bb7c9d1def331423aa323
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
  Version:          0.19.0
  GitCommit:        de40ad0
docker info
 Context:    desktop-linux
 Debug Mode: false
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.10.2
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.15.1
    Path:     /usr/lib/docker/cli-plugins/docker-compose
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.0.5
    Path:     /usr/lib/docker/cli-plugins/docker-dev
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.17
    Path:     /usr/lib/docker/cli-plugins/docker-extension
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     /usr/lib/docker/cli-plugins/docker-sbom
  scan: Docker Scan (Docker Inc.)
    Version:  v0.23.0
    Path:     /usr/lib/docker/cli-plugins/docker-scan

 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.22
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 9ba4b250366a5ddde94bb7c9d1def331423aa323
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
   Profile: default
 Kernel Version: 5.15.49-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 6
 Total Memory: 3.724GiB
 Name: docker-desktop
 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
 Registry: https://index.docker.io/v1/
 Experimental: false
 Insecure Registries:
 Live Restore Enabled: false

So I can see the following:

  • You are using Docker Desktop for Linux
  • The client version is not he same as the server version
  • The version of Docker Desktop (v4.16.2) is not the latest (v4.17)

My guess is the cause of the issue is the incompatible client or you actually have Docker CE and Docker Desktop on the same machine and somehow you also ran a container in a different context. I reecommend you to upgrade Docker Desktop and downgrade the client to Docker 20.10.*

Before that you can also check the contexts

docker context ls

if you have multiple contexts, switch to the other contexts

docker context use CONTEXTNAME

and check running containers.