I tried to do the following:
docker run --expose 8765 --publish-all -it nginx
But this also exposes 80
along with port 8765
[root@centos7]# docker port f4b608998815
80/tcp -> 0.0.0.0:49156
80/tcp -> :::49156
8765/tcp -> 0.0.0.0:49155
8765/tcp -> :::49155
How to publish port 8765
to one of the random available ports in the host without specifying where to?
CodePudding user response:
The nginx
base image already declares EXPOSE 80
and there's no way to un-expose a port, so if you use the docker run -P
or --publish-all
option to publish every exposed port, it will always be published alongside your manually-exposed port.
You can use the lowercase docker run -p
option with only a single port number to publish that port on an arbitrary host port instead:
docker run -p 8765 -d nginx
Since Docker containers internally won't have port conflicts with each other, you may want to just use the default HTTP port 80, matching the standard Nginx config. The --expose --publish-all
combination is pretty much the only actual effect of docker run --expose
, and you can get the same thing with --port
; you pretty much never need the docker run --expose
option.