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

How to install and configure Docker nginx

Download Nginx image in Docker docker pull nginx ...

Common Linux English Error Chinese Translation (Newbies Must Know)

1.command not found command not found 2. No such ...

MySQL replication mechanism principle explanation

Background Replication is a complete copy of data...

Docker renames the image name and TAG operation

When using docker images, images with both REPOSI...

javascript input image upload and preview, FileReader preview image

FileReader is an important API for front-end file...

Tutorial on installing MySQL 5.7.18 decompressed version on Windows

1. Installation process MySQL version: 5.7.18 1. ...

The difference between distinct and group by in MySQL

Simply put, distinct is used to remove duplicates...

Summary of MySQL common SQL statements including complex SQL queries

1. Complex SQL queries 1.1. Single table query (1...

Zabbix monitors the process of Linux system services

Zabbix automatically discovers rules to monitor s...

Native js to achieve star twinkling effect

This article example shares the specific code of ...

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...

How to elegantly implement WeChat authorized login in Vue3 project

Table of contents Preface Prepare Implementation ...

Docker custom network container interconnection

Table of contents Preface –link Custom Network As...

Detailed explanation of MySQL user and permission management

This article uses examples to describe the manage...

Use CSS3 to implement button hover flash dynamic special effects code

We have introduced how to create a waterfall layo...