Docker Community Forums

Share and learn in the Docker community.

Create command does not publish ports immediately

(Damien Coraboeuf) #1


I want to use the docker create --publish <containerPort> command in order to:

  1. Create a container without running it
  2. Get the published port on the host
  3. Change the configuration file in the container, through a mounted file, to include the published port (which I need for X-Forwarded-Port)
  4. Run the container

But the create command does not “reserve” any port yet - it’s done only when the container is actually started.

Is that the expected behaviour? Or do I miss something here?

Thanks for any help,

(Sven Dowideit) #2

yes, this is expected - you can have many containers set to use the same ports, but only one can run at a time -

so you have:

docker run -p 80:80 busybox true
docker run -p 80:80 busybox true
docker run -p 80:80 busybox true

and each of those no-longer running containers has been created, had itself connected to the host’s port 80, and then disconnected when it stops.