简体   繁体   中英

Start Docker container with random host port and return the port to use it elsewhere

I want to start a docker container with ,let's say jenkins as a service, and let docker allocate a random host port. I know how to do this .

How can I grab the randomly selected port from the running container, or tell docker to return it, in order to use it in the construction of the jenkins github plugin url

http://host-ip:random-host-port/web-hook

Once the docker run -P is done, the container is running, with the EXPOSE(d) port mapped to an high port number on the host .

To get any information on a running container, use docker inspect .

If you know the container internal port number (that was then mapped), you can type ( from this comment ):

 docker inspect --format '{{ (index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort }}' <Container-Name/ID> 

The offical Jenkins image uses EXPOSE 8080 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM