Day 5518 关于 UFW 防火墙和 Bitwarden docker 容器在网络连接管理方面冲突的笔记

注:本文不是教程,只是为了在特定环境下实现特定需求的笔记,仅供参考。

最近被人推荐了 Bitwarden 的服务端开源实现 bitwarden-rs,于是在自己的服务器上做实验。

由于 Chrome 的限制,Web Crypto API 在非 HTTPS 连接上无法使用,所以根据官方建议及其它教程配置 HAProxy 以开启 https 服务,但配置好后,发现一个有点吓人的事情:在 ufw 防火墙没有允许访问 80 端口的情况下,容器的 80 端口仍能正常访问。

经过检索,发现是由于 docker 会直接修改 iptables 配置容器的网络连接,而这部分修改并不会体现在 ufw status 中。

修正笔记如下:

1、停止 docker 服务:systemctl stop docker.socket && systemctl stop docker

2、修改 bitwarden 容器的绑定配置:在 /var/lib/docker/containers/bitwarden容器对应的hash/hostconfig.json 中,找到 PortBindings 段,修改HostIp 为 127.0.0.1(以使服务不发布在本机之外)、HostPort 为 80 以外的其它值(不冲突的话,愿意留着80也可以),如8888。修改完成后保存。

3、重启 docker 服务:systemctl start docker.socket && systemctl start docker

4、修改 HAProxy 中对应的转发端口。

5、修改 ufw 配置,使 HAProxy 的对外端口能被外网访问。

参考资料:

1、https://askubuntu.com/questions/652556/uncomplicated-firewall-ufw-is-not-blocking-anything-when-using-docker

2、https://stackoverflow.com/questions/19335444/how-do-i-assign-a-port-mapping-to-an-existing-docker-container

注:所有评论将在审核通过后显示,请不要在评论内容的任何位置出现链接,否则您的评论将被自动移入回收站,且永远不会被复审。

All comments will be available after being manually reviewed, please do not include any links anywhere in your comment, otherwise your comment will be automatically deleted and are not eligible for review.

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注