Docker connects to a container through a port

Docker connects to a container through a port

Docker container connection

1. Network port mapping

We create a container for a python application

# docker run -d -P training/webapp python app.py

We create a container using the -P parameter and use docker ps to see that port 5000 is bound to host port 32768

We can also use the -p flag to specify that the container port should be bound to the host port.

The difference between the two methods is:

-P: The internal port of the container is randomly mapped to the high port of the host.

-p: Bind the container's internal port to the specified host port.

…… ...

# docker run -d -p 5000:5000 training/webapp python app.py

In addition, we can specify the network address bound to the container, such as binding 127.0.0.1

# docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py

In this way, we can access port 5000 of the container by accessing 127.0.0.1:5001

By default, containers are bound to TCP ports. If you want to bind to UDP ports, you can add /udp after the port.

# docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

The docker port <images id> [port] command allows us to quickly view the port binding status

docker port ce206e3c0ae6 5000

Now we shut down all other containers and start a new nginx container

# docker run -d -p 5000:80 --name web111 nginx

We forward the container's port 80 to the local port 5000 and then access it externally

You can see that we can access it successfully, indicating that the port is forwarded.

2. Docker container connection

Port mapping is not the only way to connect Docker to another container.

Docker has a connection system that allows multiple containers to be connected together and share connection information.

A docker connection creates a parent-child relationship where the parent container can see the child container's information.

3. Container naming

When we create a container, Docker will automatically name it. But we can also use the --name flag to name the container

docker run -d -P --name Peithon training/webapp python app.py

Additional knowledge: How to view which ports are open inside a Docker container

You don't need to enter the container bash, just enter the docker port container id in the Linux command line to view it

The above article about how to connect a container through a port in Docker is all I have to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Solution for multiple Docker containers not having the same port number
  • How to modify the port mapping of a running Docker container
  • How to dynamically modify container port mapping in Docker
  • Perfect solution to the problem of not being able to access the port of the docker container under Windows 10
  • Docker dynamically exposes ports to containers
  • Solution to the Docker container being unable to access the host port
  • Docker implements container port binding local port

<<:  JS realizes special effects of web page navigation bar

>>:  MySQL data archiving tool mysql_archiver detailed explanation

Recommend

How to add vim implementation code examples in power shell

1. Go to Vim's official website to download t...

JavaScript implements simple date effects

The specific code of JavaScript date effects is f...

Detailed explanation of JavaScript function this pointing problem

Table of contents 1. The direction of this in the...

Detailed explanation of nodejs built-in modules

Table of contents Overview 1. Path module 2. Unti...

Recommended 20 best free English handwriting fonts

Jellyka BeesAntique Handwriting [ank]* Jellyka Cut...

How to create a table by month in MySQL stored procedure

Without going into details, let's go straight...

How to deploy Redis 6.x cluster through Docker

System environment: Redis version: 6.0.8 Docker v...

Detailed tutorial for downloading, installing and configuring MySQL 5.7.27

Table of contents 1. Download steps 2. Configure ...

Drop-down menu implemented by HTML+CSS3+JS

Achieve results html <div class="containe...

Detailed explanation of Tomcat's Server Options

1. Configuration By default, the first two are no...

How to batch generate MySQL non-duplicate mobile phone number table example code

Preface In many MySQL test scenarios, some test d...

How to prevent website content from being included in search engines

Usually the goal of building a website is to have...

Detailed explanation of Vue3's sandbox mechanism

Table of contents Preface Browser compiled versio...

Reasons and solutions for failure to insert emoji expressions in MySQL

Failure Scenario When calling JDBC to insert emoj...