个人随笔
目录
docker-5-解决宿主机没有开放81端口却可以直接访问docker启动的81端口nginx容器的问题
2020-09-09 22:26:50

我以为经过前面四篇博文的学习,自己对docker的了解最起码入门了,但是当我用docker启动一个81端口的nginx后(宿主机:容器/81:80),在宿主机的firwall防火墙没有添加81端口的情况下,竟然可以直接访问成功,然后试下docker运行mysql容器用3308端口,发现也是在firwall没有添加端口的情况下,仿佛绕过了防火墙可以直接访问到容器,当时就懵逼了…

环境

  1. CentOS7
  2. firewall
  3. docker
  4. nginx

解决流程

1、以为是docker和firewall的启动顺序问题导致的

开始我以为是因为我先启动docker后面重启了firewall导致firewall不能够监控docker,然后我还美滋滋的觉得终于找到问题了。最后发现,太天真了,启动顺序完全没影响。

2、宿主机的防火墙和docker容器的防火墙冲突

后面我想到docker也是一个轻量级的linux操作系统,也有自己的防火墙,会不会是因为docker启动自己的防火墙后,权限级别直接绕过了宿主机的防火墙,因此百度了很多,后面按照网上的说法启动容器的时候加上—iptables=false,这样可能就关闭掉了容器的iptables防火墙,然而经过尝试并没有什么用,而且好像第二次启动容器直接就启动不了了,报什么iptables错误

3、直接修改配置文件不使用docker的iptables防火墙

因为我现在的操作系统是CentOS7,所以宿主机用的是firewall,而docker用的是iptables,百度查找说需要在文件/etc/default/docker中添加如下内容

  1. DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 --iptables=false"

然后我美滋滋的跟着执行,发现这个文件根本不存在,因此我又直接新建这个文件,保存,接着执行如下两条命令重新加载文件和重启docker

  1. #重载
  2. systemctl daemon-reload
  3. #重启docker服务
  4. service docker restart

启动容器,重新访问,喵的,还是可以直接访问,仿佛这个配置文件完全没有用
然后直接百度,发现需要在文件/usr/lib/systemd/system/docker.service中添加如下配置,一下是我的文件

  1. [Unit]
  2. Description=Docker Application Container Engine
  3. Documentation=https://docs.docker.com
  4. BindsTo=containerd.service
  5. After=network-online.target firewalld.service containerd.service
  6. Wants=network-online.target
  7. Requires=docker.socket
  8. [Service]
  9. Type=notify
  10. # the default is not to use systemd for cgroups because the delegate issues still
  11. # exists and systemd currently does not support the cgroup feature set required
  12. # for containers run by docker
  13. ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock $DOCKER_OPTS
  14. ExecReload=/bin/kill -s HUP $MAINPID
  15. TimeoutSec=0
  16. RestartSec=2
  17. Restart=always
  18. EnvironmentFile=-/etc/default/docker
  19. # Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
  20. # Both the old, and new location are accepted by systemd 229 and up, so using the old location
  21. # to make them work for either version of systemd.
  22. StartLimitBurst=3
  23. # Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
  24. # Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
  25. # this option work for either version of systemd.
  26. StartLimitInterval=60s
  27. # Having non-zero Limit*s causes performance problems due to accounting overhead
  28. # in the kernel. We recommend using cgroups to do container-local accounting.
  29. LimitNOFILE=infinity
  30. LimitNPROC=infinity
  31. LimitCORE=infinity
  32. # Comment TasksMax if your systemd version does not supports it.
  33. # Only systemd 226 and above support this option.
  34. TasksMax=infinity
  35. # set delegate yes so that systemd does not reset the cgroups of docker containers
  36. Delegate=yes
  37. # kill only the docker process, not all processes in the cgroup
  38. KillMode=process
  39. [Install]
  40. WantedBy=multi-user.target
a、添加配置文件,(-代表ignore error)
  1. EnvironmentFile=-/etc/default/docker
b、在ExecStart后面添加如下内容
  1. $DOCKER_OPTS

虽然搞不懂为什么,反正接下来修改完/etc/default/docker
后重载重启

  1. #重载
  2. systemctl daemon-reload
  3. #重启docker服务
  4. service docker restart

重新启动nginx容器,我的天,终于不能访问了。
执行如下命令开启81端口

  1. firewall-cmd --zone=public --add-port=81/tcp --permanent
  2. firewall-cmd --reload

我的天,终于可以访问了,测一下docker启动mysql的访问,也被firewall拦截了。
真是要命的东西

结语

虽然最终解决了,但是具体原理并不是太过明了,但是最起码docker启动的容器再也不会绕过firewall的限制,服了。

 1221

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2