Docker dynamically exposes ports to containers

Docker dynamically exposes ports to containers

View the IP address of the Container

docker inspect <container name or id>| grep IPAddress

View the mapped port of the Container

docker port <container name or id>
eg.
docker port d8dac7399647
docker port hfq-jedi-zxf-eden

Use iptables to view container mapping

iptables -t nat -nvL

iptables -t nat -nvL --line-number

Example of adding a new port mapping

##Map host port 31101 to container port 6379

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 31101 -j DNAT --to-destination 192.168.42.2:6379

Save iptables rules

iptables-save

illustrate

192.168.42.2 is the result of docker inspect <container name or id>| grep IPAddress

After the port mapping is completed, the result cannot be queried through docker port d8dac7399647

Can be

iptables -t nat -nvL | grep 192.168.42.2

Query mapping relationship

Additional knowledge: Mechanisms in Docker container communication and port exposure issues

The link method has always been used for communication between Docker containers. This limits the order in which each container is started, which always feels inflexible. Therefore, this time we explored accessing containers directly through the LAN allocated by the Docker's own network card.

Docker port exposure means forwarding the port service of the container itself to the exposed port through the forwarding of the docker0 network card, for example, executing:

docker run -dit -p 8080:12345 --name=container_name image_name

When accessing using the 172.17.0.x local area network assigned by the docker0 network card, use port 12345. When using 192.168.1.x or other local public IP to access, you need to use 8080 to access

Time is limited, so I won't analyze it in detail for now. I'll analyze it with pictures when I have time.

The above article about Docker dynamically exposing ports to containers 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:
  • Talk about the issue of replacing docker (shim) with containerd in kubernetes1.20
  • How to keep running after exiting Docker container
  • Docker removes abnormal container operations
  • Goodbye Docker: How to Transform to Containerd in 5 Minutes
  • Delete the image operation of none in docker images
  • Implementation of local migration of docker images
  • Solve the problem of docker images disappearing
  • Docker image cannot be deleted Error: No such image: xxxxxx solution
  • How to delete an image in Docker
  • Naming containers and images in Docker

<<:  Detailed explanation of Vue mixin usage and option merging

>>:  Change the MySQL database engine to InnoDB

Recommend

Why is it not recommended to use index as the key attribute value in Vue?

Table of contents Preface The role of key The rol...

HTML table markup tutorial (4): border color attribute BORDERCOLOR

To beautify the table, you can set different bord...

Detailed explanation of the solution to permission denied in Linux

Permission denied: The reason for this is: there ...

Docker connects to a container through a port

Docker container connection 1. Network port mappi...

CentOS6 upgrade glibc operation steps

Table of contents background Compile glibc 2.14 M...

How to use macros in JavaScript

In languages, macros are often used to implement ...

Install nodejs and yarn and configure Taobao source process record

Table of contents 1. Download nodejs 2. Double-cl...

A quick review of CSS3 pseudo-class selectors

Preface If CSS is the basic skill of front-end de...

MySQL log settings and viewing methods

MySQL has the following logs: Error log: -log-err...

Summary of learning Docker commands in one article

Table of contents Introduction Mirror repository ...

Summary of MySQL password modification methods

Methods for changing passwords before MySQL 5.7: ...

Vue+Bootstrap realizes a simple student management system

I used vue and bootstrap to make a relatively sim...