Ubuntu - 主机上的防火墙规则 MySQL | Docker 个要托管的容器

Ubuntu - firewall rules MySQL on host | Docker containers to host

是否有任何可能的方法来设置主机防火墙以允许连接 仅用于 Docker 容器使用的网络?

简介

我能够从我的新 Docker 容器连接到 OS 托管 MySQL 实例的唯一方法是打开端口:

sudo ufw allow 3310

在 Docker 容器内,我可以使用以下方式进行连接:

 (Docker container): mysql -u testuser -p -h 172.100.0.1 -P 3310

然而,通过这种方式,端口也对互联网开放,我宁愿使用 SSH 使用凭据进行连接。

注意:MySQL 绑定地址=0.0.0.0 已设置

信息

注意:仅显示适用的 Docker 网络信息 - 我的容器使用自定义命名网络 (d-custom-network)

  ifconfig

  d-custom-network: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 172.100.0.1  netmask 255.255.0.0  broadcast 172.100.255.255
    inet6 fe80::42:b8ff:fe7f:c4bf  prefixlen 64  scopeid 0x20<link>
    ether 02:42:b8:7f:c4:bf  txqueuelen 0  (Ethernet)
    RX packets 473  bytes 34668 (34.6 KB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 403  bytes 119797 (119.7 KB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

  docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
    inet6 fe80::42:13ff:fefc:301f  prefixlen 64  scopeid 0x20<link>
    ether 02:42:13:fc:30:1f  txqueuelen 0  (Ethernet)
    RX packets 28509  bytes 1593290 (1.5 MB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 33351  bytes 173437123 (173.4 MB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

部分 iptables 列表(Docker 个目的地)- : 我在 MySQL 测试中尝试连接时从容器内部连接时注意到这些连接信息登录失败

   FROM Within container:

   ERROR 1045 (28000): Access denied for user 'testuser'@'cpe-172-100-0-4.twcny.res.rr.com'
   ping cpe-172-100-0-4.twcny.res.rr.com
   PING cpe-172-100-0-4.twcny.res.rr.com (172.100.0.4) 56(84) bytes of data.
   64 bytes from 54195d3486eb (172.100.0.4): icmp_seq=1 ttl=64 time=0.063 ms

   iptables -L 

   Chain DOCKER (2 references)
   target     prot opt source               destination
   ACCEPT     tcp  --  anywhere             cpe-172-100-0-2.twcny.res.rr.com  tcp dpt:6082
   ACCEPT     tcp  --  anywhere             cpe-172-100-0-2.twcny.res.rr.com  tcp dpt:6081
   ACCEPT     tcp  --  anywhere             cpe-172-100-0-3.twcny.res.rr.com  tcp dpt:6379
   ACCEPT     tcp  --  anywhere             cpe-172-100-0-4.twcny.res.rr.com  tcp dpt:http-alt
   ACCEPT     tcp  --  anywhere             cpe-172-100-0-5.twcny.res.rr.com  tcp dpt:https
   ACCEPT     tcp  --  anywhere             cpe-172-100-0-5.twcny.res.rr.com  tcp dpt:http

我试过的

我想我可以使用这里提到的方法:这也是我研究如何确保主机使用的 docker 网络名称的原因(即 ifconfig 输出) ,在我的示例中,我创建了 d-custom-network

Direct link

To allow access on a specific port let’s say port 3360 only to specific network interface eth2, then you need to specify allow in on and the name of the network interface:

 $ sudo ufw allow in on eth2 to any port 3306

 $ sudo ufw reload

所以我尝试了sudo ufw allow in on d-custom-network to any port 3310

那时我注意到 docker cpe-172-100-0-2.twcny.res.rr.com 在尝试强制登录失败以测试我连接的位置时的输出。

我认为我没有正确理解用法,并假设我可以使用我的 docker 容器连接 to/from.

的内部网络 172.100.0.*

我该怎么做?

我有同样的问题,以下对我有用(主机上的所有命令 运行):

  1. 确定您的容器连接到哪个网络:

    $ docker inspect mycontainer
    ...
        "Networks": {
            "bridge": {
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null,
                "NetworkID": "b8ecee4683b22db9154f8c3e56b48f371b2bdf663f25d092112fc7eb410a4816",
                "EndpointID": "56cd2b50116f92d49c643457059a0ef0350e14509a16d1b615f806bb62eb4ad5",
                ...
            }
        }
    ...
    

    我的容器 mycontainer 连接到名为 bridge 的网络:

    $ docker network ls
    NETWORK ID     NAME      DRIVER    SCOPE
    b8ecee4683b2   bridge    bridge    local
    f9a4ecf5db2b   host      host      local
    cc168ad8f868   none      null      local
    
  2. 识别哪个主机的网络接口对应于bridge网络:

     $ docker network inspect bridge
     ...
     "Options": {
         ...
         "com.docker.network.bridge.name": "docker0",
         ...
     },
     ...
    

    我的网络bridge对应主机网络接口docker0:

     $ ip addr
     ...
     3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
         link/ether 02:42:c8:b4:a4:ba brd ff:ff:ff:ff:ff:ff
         inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
            valid_lft forever preferred_lft forever
         inet6 fe80::42:c8ff:feb4:a4ba/64 scope link 
            valid_lft forever preferred_lft forever
     ...
    
  3. 允许从已识别的主机网络接口 docker0 到任意位置的端口 3310 的流量:

    $ sudo ufw allow in on docker0 to any port 3310
    

我测试了一下,主机上的数据库端口现在是:

  • 从 docker 容器内部连接时可访问,并且
  • 尝试从主机外部的网络连接时被防火墙过滤。

此外,由于我们正在讨论使用 UFW 保护 docker 主机,我觉得有必要提及 note/warning 关于将 UFW 与 docker 一起使用的问题,以防你尚未意识到:有关详细信息,请参阅 ,总而言之,默认情况下 Docker 可能会以典型用户可能不期望的方式覆盖 UFW 的防火墙规则。特别是,我认为通过 docker -p 公开容器上的端口实际上会在该端口上的防火墙中打一个洞,UFW 看不到或控制它。