LoginSignup
9
8

More than 5 years have passed since last update.

Docker と UFW を共存させる

Last updated at Posted at 2017-04-11

Docker はなんだか UFW を無視するそうで。
以下の様に味付けをする必要があるみたい。

# /etc/default/ufw

...

# Set the default forward policy to ACCEPT, DROP or REJECT.  Please note that
# if you change this you will most likely want to adjust your rules
DEFAULT_FORWARD_POLICY="ACCEPT"

...
# /etc/default/docker

...

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

...
# /etc/docker/daemon.json

{"iptables": false}
sudo service ufw restart
sudo service docker restart

で、なんとなく UFW 動いてる風なんだけど、 docker-compose で複数の(dockerネットワーク)環境を作ったりすると、コンテナ内からインターネットに繋がらなくなったりする。
で、調べると IPマスカレードがうんたら とか出て来るので、以下の様に書くとなんか元気に動き出す。

# /etc/ufw/before.rules

#
# rules.before
#
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
#   ufw-before-input
#   ufw-before-output
#   ufw-before-forward
#

# こっから ============================

*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING ! -o docker0 -s 172.16.0.0/12 -j MASQUERADE

# don't delete the 'COMMIT' line or these rules won't be processed
COMMIT

# ここまでを追加 ========================

# Don't delete these required lines, otherwise there will be errors
*filter
:ufw-before-input - [0:0]
:ufw-before-output - [0:0]
:ufw-before-forward - [0:0]
:ufw-not-local - [0:0]
# End required lines

...

ネットワークまじで無知故、正しいのかどうか大分怪しい。
ので、自己責任で。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8