by Devin Yang
(This article was automatically translated.)

Published - 2 years ago ( Updated - 2 years ago )

First of all, in the Linux environment, Docker uses iptables rules to provide network isolation.
However, in the environment of Docker swarm mode, we cannot identify the connection port under 127.0.0.1 of the host.
At this time, we can customize the rules through the DOCKER-USER chain in iptables.

Docker installs two custom iptables chains named DOCKER-USER and DOCKER, which ensures that incoming packets are always inspected first by these two chains.

All iptables rules for Docker are added to the DOCKER chain. Do not manipulate this chain manually.

If you need to add rules that load before Docker rules,

use the DOCKER-USER chain. These rules are enabled before any rules created automatically by Docker.

For example, my nginx has opened port 8081, which represents the input host's ip, adding port 8081 can access the host.

cyh3692mbacl   dlaravel_web          replicated   4/4        nginx:latest      *:8081->80/tcp

But in fact, I prefer to access the host through HAProyx, instead of opening the host's ip and port 8081 to access it. The solution is to adjust iptables.

We can use the following command to view the previous rules

iptables -nL DOCKER-USER
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

Basically it is completely open, we can write a bash as follows, please note that this is just an example, please adjust it according to your own needs.
This example includes different network segments or specific IP and network card.

#!/bin/bash
iptables -F DOCKER-USER
#全檔只能用本機的ip存取
iptables -I DOCKER-USER -p tcp -m multiport --dports 8025,8084,30001,30020 -j DROP
#鎖區網在可以連
iptables -I DOCKER-USER -i eno1 ! -s 10.0.0.26 -p tcp -m multiport --dports 8080,8081 -j DROP
iptables -I DOCKER-USER -i eno2 ! -s 192.168.88.0/24 -p tcp -m multiport --dports 3306,2222,1024 -j DROP
iptables -A DOCKER-USER -j RETURN
#列出規則
iptables -nL DOCKER-USER

At the beginning I cleared all rules first

iptables -F DOCKER-USER

I won't say much about the rest, I believe everyone can guess it.

 

 

Tags: docker

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


docker,phpenv

Unable to ping host.docker.internal on Linux

You can check with docker version, confirm that the version is Docker v20.10+, you can add extra_hosts as follows:

wordpress, d-laravel, docker, docker-compose

How to install Wordpress with D-Laravel

An 8-minute teaching video, introducing the installation of Wordpress on D-Laravel. D-Laravel is a docker-compose Laravel framework development environment, but it does not mean that it cannot be used to install other things. This video introduces the use of D-Laravel to install non-Laravel PHP projects, You can learn the usage of related instructions on D-Laravel: ./create --host usage, docker-compose up -d and ./console mysql usage, The database connection setting method after D-Laravel v1.0.0.

docker,dlaravel

D-Laravel 1.5.5 Change Notes

D-Laravel's fpm image was rebuilt using the official dockerfile of docker php before php 7.2.1. And so I can specify that the default owner of fpm is dlaravel, --with-fpm-user=USER Set the user for php-fpm to run as. (default: nobody) --with-fpm-group=GRP Set the group for php-fpm to run as.