Docker port mapping and external inaccessibility issues

Docker port mapping and external inaccessibility issues

The Docker container provides services and listens to port 8888. To make it accessible from the outside, port mapping is required.

docker run -it --rm -p 8888:8888 server:v1

A problem occurs at this point. After deployment on virtual machine A, the 8888 port service can be accessed in A, but not in B.

This should be due to the request being intercepted.

1. Check firewall-cmd --state

If the output is "not running", FirewallD is not running and all protection policies are not started. In this case, the firewall can be ruled out as blocking the connection.

If the output is "running", it means that FirewallD is currently running. You need to enter the following command to view which ports and services are currently open:

firewall-cmd --list-ports
firewall-cmd --list-services

There are two solutions:

1. Turn off the FirewallD service:

If you don't need a firewall, just turn off the FirewallD service.

systemctl stop firewalld.service

2. Add a policy to open the specified port to the outside world:

For example, if we want to open the external 5000/tcp port, we can use the following command:

firewall-cmd --add-port=5000/tcp --permanent
firewall-cmd --reload

If you only want to open the port temporarily, remove the "--permanent" parameter in the first line of the command. Then when you restart the FirewallD service again, this policy will become invalid.

2. IP forwarding is not turned on

sysctl net.ipv4.ip_forward

If net.ipv4.ip_forward=0 is displayed, it is not enabled.

3. Service iptables is turned on and blocked

You can turn off service iptables

service iptables stop

If an error occurs when running docker:

iptables: No chain/target/match by that name.

Then just restart the docker service

service docker restart

or:

#Set the iptables firewall as a startup item systemctl enable iptables.service

#Start the firewall to make the configuration file effective systemctl start iptables.service

#Stop the firewall systemctl stop iptables.service

#Restart the firewall to make the configuration file take effect systemctl restart iptables.service

Final version:

After starting docker and mapping ports, docker will add DNAT rules in iptables to convert the packets received from the corresponding port to IP and forward them. At the same time, rules will be added to convert all IPs from the docker domain.

However, on Centos7, docker can access the external network normally, but the request sent from the external network cannot be delivered to docker0 after being received and forwarded by eth1, or it is delivered but the (oui Unknown) situation appears. It is not clear why the data cannot be delivered to docker0 after DNAT.

The final solution is to restart iptables after starting docker

service iptables restart

Clear all rules added by docker, and then add rules

iptables -t nat -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE

Replace the IP address of all packages 172.17.0.0/16 from Docker with the local IP address and send them to achieve the purpose of Docker accessing the external network.

This is the end of this article about docker port mapping and external inaccessibility. For more information about docker port mapping and external access, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to modify the port mapping of a running Docker container
  • Troubleshooting process for Docker container suddenly failing to connect after port mapping
  • Add port mapping after docker container starts
  • How to set port mapping for running container in Docker
  • Demonstration and analysis of four port mappings of docker containers

<<:  How does Vue solve the cross-domain problem of axios request front end

>>:  CSS to achieve scrolling image bar example code

Recommend

JavaScript recursion detailed

Table of contents 1. What is recursion? 2. Solve ...

Centos8 (minimum installation) tutorial on how to install Python3.8+pip

After minimizing the installation of Python8, I i...

An article tells you how to write a Vue plugin

Table of contents What is a plugin Writing plugin...

WeChat applet implements calculator function

This article shares the specific code for the WeC...

Join operation in Mysql

Types of joins 1. Inner join: The fields in the t...

MySQL string splitting example (string extraction without separator)

String extraction without delimiters Question Req...

How to connect to virtual machine MySQL using VScode in window environment

1. Virtual Machine Side 1. Find the mysql configu...

Mysql sorting to get ranking example code

The code looks like this: SELECT @i:=@i+1 rowNum,...

MySQL database aggregate query and union query operations

Table of contents 1. Insert the queried results 2...

js to realize payment countdown and return to the home page

Payment countdown to return to the home page case...

MySQL joint index effective conditions and index invalid conditions

Table of contents 1. Conditions for joint index f...

Install and build a server environment of PHP+Apache+MySQL on CentOS

Yum (full name Yellow dog Updater, Modified) is a...