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

Solution to the problem that VC6.0 cannot be used when installed on WIN10

VC6.0 is indeed too old VC6.0 is a development to...

Detailed tutorial on installing MySQL 5.7.20 on RedHat 6.5/CentOS 6.5

Download the rpm installation package MySQL offic...

An article to understand the advanced features of K8S

Table of contents K8S Advanced Features Advanced ...

Causes and solutions for MySQL master-slave synchronization delay

For historical reasons, MySQL replication is base...

Use CSS content attr to achieve mouse hover prompt (tooltip) effect

Why do we achieve this effect? ​​In fact, this ef...

Vue implements horizontal beveled bar chart

This article shares the specific code of Vue to i...

JavaScript Document Object Model DOM

Table of contents 1. JavaScript can change all HT...

Linux operation and maintenance basics httpd static web page tutorial

Table of contents 1. Use the warehouse to create ...

A brief summary of my experience in writing HTML pages

It has been three or four months since I joined Wo...

How to build a standardized vmware image for kubernetes under rancher

When learning kubernetes, we need to practice in ...

Summarize how to optimize Nginx performance under high concurrency

Table of contents Features Advantages Installatio...

How to implement responsiveness in Vue source code learning

Table of contents Preface 1. Key Elements of a Re...

Optimizing the slow query of MySQL aggregate statistics data

Written in front When we operate the database in ...

js implementation of verification code case

This article example shares the specific code of ...