Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out which random port Docker has chosen?

Tags:

docker

I ran this:

$ docker run -p 8080 --rm my_container 

which I guess maps the container port 8080 to some random available port on the host. But how do I find out which port?

like image 785
ijt Avatar asked Mar 02 '18 17:03

ijt


People also ask

How do I find the port of a Docker container?

The left-hand side of the port number mapping is the Docker host port to map to and the right-hand side is the Docker container port number. When you open the browser and navigate to the Docker host on port 8080, you will see Jenkins up and running.

What port is used by Docker by default?

The Docker client will default to connecting to unix:///var/run/docker.sock on Linux, and tcp://127.0.0.1:2376 on Windows. For example: tcp:// -> TCP connection to 127.0. 0.1 on either port 2376 when TLS encryption is on, or port 2375 when communication is in plain text.

What port does Docker container use?

Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.


1 Answers

You can use the docker port command:

docker port my_container

This command output like the following (example with MySQL image):

3306/tcp -> 0.0.0.0:3306

The value before -> specifies the port on the container side. The value after -> specifies the chosen port on the host machine.


You can also run the container with a specific port on the host machine (if available):

docker run -p "80:8080" --rm my_container

This would give the following output on docker port:

8080/tcp -> 0.0.0.0:80
like image 146
Sebastian Brosch Avatar answered Oct 12 '22 20:10

Sebastian Brosch