Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker port mapping syntax

Tags:

docker

port

I am new to docker, and I am a bit confused about what the following command options do specifically for the command I came across.

 --name : appname is the name of the image?
 -t : Run in terminal?
 -d : run as daemon?
 -p : for somebody outside the container to talk to port 9090 they have to connect on port 9000?
 Same for port 15501 but it is a udp port?    
 appname2: name assigned to running image?

 docker run -t --name=appname -p 9090:9000 -p 15501:15501/udp -d appname2
like image 470
user2399453 Avatar asked Mar 08 '23 20:03

user2399453


2 Answers

 docker run -t --name=appname -p 9090:9000 -p 15501:15501/udp -d appname2

Q: --name : appname is the name of the image?

No. It's the name of the container that you are creating (optional).

--name string           Assign a name to the container

Q: -t : Run in terminal?

-t, --tty             Allocate a pseudo-TTY

Q: -d : run as daemon?

Sort of. It means that you want to run your container detached from your terminal.

-d, --detach          Run container in background and print container ID

Q: -p : for somebody outside the container to talk to port 9090 they have to connect on port 9000?

9090:9000 means: port 9090 on the host machine binded to port 9000 on the container. To talk to the container port someone outside should talk to 9090.

-p, --publish list       Publish a container's port(s) to the host (default [])

Q: Same for port 15501 but it is a udp port?

Right.


Q: appname2: name assigned to running image?

That is the image that you are running on. The container is based on top of it.


Bonus! You can find all of this info here: docker help run

Bonus 2! Try it yourself:

docker run -d -it --name my-container alpine sh
docker inspect my-container
# See all this funny output. It's all about the container that you've created
like image 69
Robert Avatar answered Mar 27 '23 09:03

Robert


From https://docs.docker.com/engine/reference/run/

The -d flag means detached. When you run a docker container, you can either run a container in foreground, or you can run it in the background. The choice of how to run your container really depends on your use case. If, for example, you run an OS container with some functionality, you would probably want run the container in foreground in order to use this functionality. But if you run a DB server, you may want to run it in the background.

The -p flag, when used, publishes all exposed ports to the host interfaces. If for example you run a DB server inside a container which has some ports exposed, and you wish to communicate to the server from a distance, you may want to map the ports inside the container to a single or multiple ports of choice on your host system. That way when you connect to the port on your host, you connect to the docker server running inside of it (I hope this is clear). The mapping format is as follows:

ip:hostPort:contain`enter code here`erPort | ip::containerPort | hostPort:containerPort | containerPort

The --name flag gives the running container a nice name. If not used, it would generate a name. It can be used, for example, if you executed a container in detached mode, and then you wanted to get inside the container using the attach command.

The -t flag allocates a text console for the container.

appname2 is the name of the docker image.

like image 43
vrommer Avatar answered Mar 27 '23 09:03

vrommer