Make Docker play nicely with UFW

I’ve been spending a lot of time working with Docker over the last year, primarily in Ubuntu environments. So long in fact, that I seem to have forgotten this blog exists 🙂

Something it took me a while to figure out was how to stop Docker from bypassing UFW and exposing mapped ports to the world (due to specifying its own iptables chain). More often than not, I want containers to be restricted to private network access only. One option is to specify an ip address when mapping ports, but that’s a bit clunky and doesn’t work so well when you want to be able access the ports via multiple private ip addresses.

The challenge was making sure not to block outbound or inter-container connectivity in the process.

Having had to set up a number of servers and keep finding myself forgetting one of the steps, I figured it was about time I put this blog to good use and list the necessary commands here:

sudo ufw allow in on docker0
sudo sed -i s/DEFAULT_FORWARD_POLICY=\"DROP\"/DEFAULT_FORWARD_POLICY=\"ACCEPT\"/ /etc/default/ufw
sudo ufw enable
iptables -t nat -A POSTROUTING ! -o docker0 -s 172.17.0.0/16 -j MASQUERADE
This entry was posted in Fixes and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments


  1. Fatal error: Uncaught Error: Call to undefined function ereg() in /var/www/html/wp-content/themes/thematic/library/extensions/comments-extensions.php:262 Stack trace: #0 /var/www/html/wp-content/themes/thematic/library/extensions/discussion.php(30): thematic_commenter_link() #1 /var/www/html/wp-includes/class-walker-comment.php(180): thematic_comments(Object(WP_Comment), Array, 1) #2 /var/www/html/wp-includes/class-wp-walker.php(146): Walker_Comment->start_el('', Object(WP_Comment), 1, Array) #3 /var/www/html/wp-includes/class-walker-comment.php(140): Walker->display_element(Object(WP_Comment), Array, '5', 0, Array, '') #4 /var/www/html/wp-includes/class-wp-walker.php(371): Walker_Comment->display_element(Object(WP_Comment), Array, '5', 0, Array, '') #5 /var/www/html/wp-includes/comment-template.php(2040): Walker->paged_walk(Array, '5', 0, 0, Array) #6 /var/www/html/wp-content/themes/thematic/comments.php(80): wp_list_comments('type=comment&ca...') #7 /var/www/html/wp-includes/comment-template.php(1430): require('/var/www in /var/www/html/wp-content/themes/thematic/library/extensions/comments-extensions.php on line 262