Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of the difference between docker-compose ports and expose

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

There are two ways to expose container ports in docker-compose: ports and expose.

Ports

Ports exposes the container port to any port or designated port of the host, usage:

Ports:-"80:80" # bind the container's port 80 to the host's port 80-"9000 8080" # bind the container's port 8080 to the host's port 9000-"443" # bind the container's port 443to any port of the host, and randomly assign the bound host port number when the container starts

Using ports exposes the port to the host, regardless of whether or not the host port is specified.

Some network applications can be run in the container, and to make them accessible externally, you can specify the port mapping with the-P (uppercase) or-p (lowercase) parameters.

(1) when using the-P flag, Docker will randomly map a port of 49000 to 49900 to a network port open in the internal container.

Using docker ps, you can see that 49155 of the local host is mapped to port 5000 of the container. At this point, you can access the interface provided by the web application in the container by accessing port 49155 of this machine.

$sudo docker run-d-P training/webapp python app.py $sudo docker ps-l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0pur49155-> 5000/tcp nostalgic_morse

Similarly, you can view the information of the application through the docker logs command.

$sudo docker logs-f nostalgic_morse * Running on http://0.0.0.0:5000/ 10.0.2.2-- [23/May/2014 20:16:31] "GET / HTTP/1.1" 200-10.0.2.2-[23/May/2014 20:16:31] "GET / favicon.ico HTTP/1.1" 404-

(2)-p (lowercase) can specify the IP and port to be mapped, but only one container can be bound on a specified port. Supported formats are hostPort:containerPort, ip:hostPort:containerPort, and ip::containerPort.

Expose

Expose exposes the container to link to the current container, usage:

Expose:- "3000"-"8000"

The above directive exposes ports 3000 and 8000 of the current container to the container from link to this container.

Unlike ports, expose does not expose ports to hosts.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report