Get the App
SLTechnology News&Howtos  ›  Servers  › 

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

Shulou Source: shulou.com Published: 2022-06-02 01:12:14 10月25日 Update

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.

Tags: Port container host application can be passed through lowercase network support information content parameters slogans commands uppercase that is instructions methods is in tag format Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Huawei MySQL NVidia