143

Can I specify a port range in a Dockerfile

EXPOSE 7000-8000

and when running the container bind all these exposed ports to the same ports on the host machine?

docker run -p 7000-8000:7000-8000
Jonathan Argentiero
  • 5,687
  • 8
  • 29
  • 34
DarVar
  • 16,882
  • 29
  • 97
  • 146
  • 2
    Possible duplicate of [Docker: Expose a range of ports](http://stackoverflow.com/questions/28022656/docker-expose-a-range-of-ports) – Michael Irigoyen Nov 10 '15 at 14:57

1 Answers1

186

Since Docker 1.5 you can now expose a range of ports to other linked containers using:

The Dockerfile EXPOSE command:

EXPOSE 7000-8000

or The Docker run command:

docker run --expose=7000-8000

Or instead you can publish a range of ports to the host machine via Docker run command:

docker run -p 7000-8000:7000-8000

max23_
  • 6,531
  • 4
  • 22
  • 36
DarVar
  • 16,882
  • 29
  • 97
  • 146
  • 22
    Check output of "iptables -L -n" on the host machine and you will see hundreds of lines each for single port mapping. If you give 5000-50000 , then practically you have to wait for a long time.Is this the supposed behaviour? – ᐅdevrimbaris Jul 22 '15 at 07:49
  • 11
    @ᐅdevrimbaris There is also a [docker process being launched per port](http://stackoverflow.com/q/37770567/1318694). I believe this is what takes up most of the work time. – Matt Jun 12 '16 at 03:53
  • 2
    the docs talk about the `--publish-all , -P` flag which "Publish all exposed ports to random ports". http://docs.docker.com/engine/reference/commandline/run/ – nils petersohn Jun 25 '18 at 10:56