Configuring FirewallD for Docker Containers on Raspberry Pi

Running services like Nextcloud on your Raspberry Pi using Docker is an efficient way to manage your personal cloud. However, ensuring that your services are secure while remaining accessible requires careful firewall configuration. Here’s a guide on how to use FirewallD to manage network traffic for Docker containers.

Prerequisites

  • A Raspberry Pi with Docker installed and running.
  • FirewallD installed on your Raspberry Pi.
  • Sudo privileges on your Raspberry Pi.

Step-by-Step Guide

Adjusting FirewallD for Docker

Opening Ports for Nextcloud

If you’ve accidentally removed port 8181/tcp from the public zone, it would have blocked access to your Nextcloud instance. To restore access, you need to add the port back to the public zone:

sudo firewall-cmd --permanent --zone=public --add-port=8181/tcp

Then apply the changes:

sudo firewall-cmd --reload

Your access to Nextcloud should now be restored, assuming the rest of your configuration is correct. Adding the Docker Interface to a New Zone

To manage Docker traffic effectively, it’s advisable to create a dedicated zone in FirewallD. Add the Docker interface, usually named docker0, to a new zone named docker:

sudo firewall-cmd --permanent --zone=docker --add-interface=docker0

Allowing Inter-Container Communication

Allow communication between containers within this zone:

sudo firewall-cmd --permanent --zone=docker --add-source=172.18.0.0/24
sudo firewall-cmd --permanent --zone=docker --add-port=3306/tcp

Here, 172.18.0.0/24 is the subnet used by Docker, and port 3306/tcp is typically used by MySQL, which Nextcloud might require. Reloading FirewallD Configuration

Apply your firewall changes:

sudo firewall-cmd --reload

Verifying Configuration

Ensure your new zone is correctly configured and active:

sudo firewall-cmd --get-active-zones

You should see docker listed as an active zone along with the interfaces and sources you’ve added.

Conclusion

With these steps, you’ve configured FirewallD to handle Docker traffic on your Raspberry Pi, creating a secure environment for your Nextcloud instance. Remember that maintaining a firewall is an ongoing task, and you should update your rules as your network configuration evolves.

For more information on FirewallD and Docker integration, refer to the official documentation for FirewallD and Docker.


This template provides a foundation for writing about integrating FirewallD with Docker on Raspberry Pi. Adjust the content to reflect your setup and add any additional insights or tips you find helpful.