简体   繁体   中英

Docker run -p ?/? (what are this two port numbers and what they represents )

I used command "docker run -p 8080/8080 --name my_local_image...." and it failed saying Unable to find image '8080/8080:latest' locally

8080/8080 = what this two port represents exactly?

Replace this " -p 8080/8080 " by this " -p 8080:8080 "

  • The first port: the Docker host ( you can use this port to access to your container) to access to the container from the outside .
  • the second one: is the port used by your application.

Example: I want to run tomcat server in a docker container, the default port of tomcat is 8080 and I want to expose my docker on port 9000 so i have to write:

docker run -p 9000:8080 --name myTomcatContainer tomcat 

So with this configuration I can access to Tomcat from outside using: http://host-ip:9000

The -p option is used to expose the ports that are used for the image instance. The first one is the hostport and the second parameter after the ":" fullcolon is the container port to which it should be mapped. For more details read doc .

The error that you received while launching the instance using docker run tells us that the image name that you provided with the command is not locally available on the machine that you are using, it might also be an issue with the way that you provided the ports using '/' instead of ":".So, to be safe, use docker pull first to pull the latest image from repo and then run it with proper syntax.

--expose=[]: Expose a port or a range of ports inside the container. These are additional to those exposed by the EXPOSE instruction

-P: Publish all exposed ports to the host interfaces

-p=[]: Publish a container's port or a range of ports to the host format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort Both hostPort and containerPort can be specified as a range of ports. When specifying ranges for both, the number of container ports in the range must match the number of host ports in the range, for example: -p 1234-1236:1234-1236/tcp When specifying a range for hostPort only, the containerPort must not be a range. In this case the container port is published somewhere within the specified hostPort range. (eg, -p 1234-1236:1234/tcp ) (use 'docker port' to see the actual mapping)

--link="": Add link to another container (:alias or )

Just to add a little to the already good answers here, I like to use the analogy of finding a friends apartment in a city to explain port numbers.

In this analogy, the

  • Host OS == neighborhood where the apartment complex is

  • Your application == the apartment complex

  • Exposed port within the application == the specific apartment in the complex

If you're trying to find someone's address, you'll find the neighborhood of the user (in this case it would be the URL, most likely http://localhost if developing locally)

But then you need the street number of the apartment complex, that number is the first number in something like 5000 :8080, so your application is exposed on the host OS at port 5000, the friends street address is 5000, localHost street.

But once you get to the apartment complex, you still need to know which door to knock on. You've arrived at the apartment complex (your application), but where does your friend live specifically? That's the second number, the 5000: 8080 .

So your friend's network address is the combination of the URL, port within the host OS (if you're developing locally, this is your computer, if not, it's whatever server the app is running on) and port exposed within the application.

Note that an apartment complex usually has more than one apartment, but it's still at the same address, so it's very possible that your application exposes more than one port, something like 5000:8081 , or 5000:9000 .

It's also possible that two apartments in different buildings will have the same apartment number, so we can have 5000:8080 , as well as 8080:8080 , neither this nor the previous situation will cause a conflict in network traffic.

This image is an example of the last case I mentioned

主机上的端口

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