This article is a mirror article of machine translation, please click here to jump to the original article.

View: 7537|Reply: 4

Prohibiting Docker containers from accessing the Internet (extranet)

[Copy link]
Posted on 2022-12-14 22:53:18 | | | |
Requirements: A private home album system was built using Docker containers, which is limited to access and viewing on the home LAN, because the Docker image is developed by a third-party developer, and I don't know if it will be a security situationGenerate privacy breachesIn order to better solve this problem, we want to add a guarantee of privacy by prohibiting Docker containers from accessing the Internet (external network).

CentOS 7 prohibits an IP from accessing the server
https://www.itsvse.com/thread-10264-1-1.html

iptables firewall only allows certain IPs to access certain ports and specific websites
https://www.itsvse.com/thread-2535-1-1.html

CentOS7 View and turn off the firewall
https://www.itsvse.com/thread-7771-1-1.html

This article uses a CentOS7 system, Docker version: 20.10.20, and uses nginx images to provide tests.

Internet access (extranet) is allowed by default

Create a new nginx container that provides web services that can be accessed by18080 Port access, the container can access the Internet (external network) by default, the command is as follows:

Enter the container and request the resources of this site through the curl command, which can be obtained normally, as shown in the figure below:



Disabling Docker containers from accessing the internet (extranet)

First, we need to create a custom network through docker and create a virtual network card, with the following command:



Set a rule on the virtual NIC through iptables to prohibit access to the Internet with the following command:

Regardless of whether the iptables service is available or not, regardless of whether the firewall is turned on or not,iptables rules added by command take effect immediately!!!

Note: This plan is appropriateIf different containers join the same network, the containers will not be able to communicate with each other!!!!

Look at the DOCKER-USER rule as shown below:



Create a new nginx container again, the container provides the web service, and the18081 The port provides services to the outside world, and the container is prohibited from accessing the Internet (public network), the command is as follows:



We have achieved our goal, but,After restarting the server, the rules we created in iptables will disappearHow can we automatically load our custom rules after the server is restarted?

The iptables-save command is used to export the iptables table in the Linux kernel to a standard output vendor, usually using the I/O redirection function in the shell to save its output to a specified file.

Save the existing iptables rule

Add the restore iptables rule to the boot boot, the boot boot file is /etc/rc.d/rc.local, edit the file, and add the following command at the end:

Finally, add the execution permission with the following command:

After the settings are complete, the iptables rules will still take effect even if the server is restarted.

All containers in Docker are prohibited from accessing the Internet (extranet),Not tested, the command is as follows:

(End)






Previous:The asset file "project.assets.json" cannot be found. Run NuGet package restore...
Next:BBR vs BBRplus vs BBR2 General network speed comparison
Posted on 2022-12-16 21:14:15 |
Learn it
 Landlord| Posted on 2023-1-2 17:43:19 |
The case is as follows:

【Practical Combat】Build a Gitea private Git repository with soft routing
https://www.itsvse.com/thread-10403-1-1.html


Posted on 2023-4-7 18:55:08 |
This feature is very important, come and learn it
Posted on 2023-4-21 12:00:24 |
Thank you for sharing, I learned.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com