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

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

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

ports

Ports exposes the container port to any port or specified 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 443 to any port on the host. The bound host port number is randomly assigned when the container starts

Using ports will expose the port to the host regardless of whether the host port is specified or not.

Some network applications can be run in the container. To make these applications accessible to the outside world, you can specify port mapping using the -P (uppercase) or -p (lowercase) parameters.

(1) When the -P flag is used, Docker will randomly map a port between 49000 and 49900 to the network port opened inside the container.

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

$ 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.0:49155->5000/tcp nostalgic_morse

Similarly, you can use the docker logs command to view application information.

$ 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 to a specified port. The supported formats are hostPort:containerPort, ip:hostPort:containerPort, ip::containerPort.

expose

Expose the container to the container linked to the current container. Usage:

expose:
- "3000"
- "8000"

The above instructions expose ports 3000 and 8000 of the current container to the container linked to this container.

The difference from ports is that expose does not expose the port to the host.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed tutorial on docker-compose deployment and configuration of Jenkins
  • Docker compose custom network to achieve fixed container IP address
  • How to use Docker-compose to build an ELK cluster
  • Detailed installation and use of docker-compose
  • Docker Compose network settings explained
  • How to run MySQL using docker-compose
  • Two simplest ways to install docker-compose
  • Solution to the timeout problem when installing docker-compose with PIP

<<:  Webpack file packaging error exception

>>:  Detailed explanation of mysql scheduled tasks (event events)

Recommend

Detailed explanation of Mysql transaction processing

1. MySQL transaction concept MySQL transactions a...

Detailed explanation of the use of Vue.js render function

Vue recommends using templates to create your HTM...

An example of implementing a simple infinite loop scrolling animation in Vue

This article mainly introduces an example of Vue ...

Native js implements custom scroll bar component

This article example shares the specific code of ...

The process of building and configuring the Git environment in Docker

Configure Git environment in Docker At work, I en...

Common JavaScript memory errors and solutions

Table of contents 1. Timer monitoring 2. Event mo...

Basic usage details of Vue componentization

Table of contents 1. What is componentization? 2....

Detailed explanation of Vue lazyload picture lazy loading example

Documentation: https://github.com/hilongjw/vue-la...

VSCode configuration Git method steps

Git is integrated in vscode, and many operations ...

CSS pixels and solutions to different mobile screen adaptation issues

Pixel Resolution What we usually call monitor res...