Best Practices for Setting Up NFS with Docker Swarm
Asked by
TechyNerd42
I've been exploring Docker for a while now, starting with a straightforward Ubuntu VM that managed a few containers. Over time, I've scaled up my setup to about 20 containers, which is a lot to manage, especially during host maintenance when I have to stop everything to update and reboot. Recently, I set up my first Docker Swarm using four Dell machinesโone manager and three workers. Now, I'm faced with the challenge of moving all the configuration volumes that used to be on my local drive to shared storage. I'd prefer not to create individual NFS or SMB shares for each container. Is there a way to create a single SMB/NFS shareโlet's call it SwarmConfigโon my NAS that has subfolders for each container, allowing me to mount those subfolders as the config directories for my containers?
Answers
For setting up NFS with Docker Swarm, it really depends on your specific needs, but what you've described aligns well with how many setups operate. You would mount the NFS share to each host at the same mount point and then use bind mounts for resources. Here's a basic example:
```yaml
volumes:
- /swarmconfig/apache/httpd.conf:/usr/local/apache2/conf/httpd.conf
```
This keeps it efficient. However, I understand your concern about connection stabilityโI prefer handling it through configurations so that Docker manages reconnections automatically.
Reply to this answer
If you opt for handling it via the configurations instead of on the host level, it can indeed simplify things. If a connection drops, Docker will typically try to remount it when you restart the container, which is much more convenient than remounting manually.
Reply to this answer
You can definitely mount your NFS share directly from your NAS into Docker. For example, in your Docker Compose file, you could set it up like this:
```yaml
services:
app:
volumes:
- data:
volumes:
data:
driver: local
driver_opts:
type: "nfs"
o: "addr=,rw,noatime,rsize=8192,wsize=8192,tcp,timeo=14,nfsvers=4"
device: ":/export/"
```
You can create a subfolder under your export path for each service to store its data. Just be cautious, though; if you're running services using a SQLite database, there's a risk of database corruption with high interactions. Also, it might be beneficial to add another manager to your Swarm for improved stabilityโthree managers and five workers work well for me!
Reply to this answer
Add your answer
Answers are reviewed before publication.
NewbieContainer1: Thanks for the insight! Iโm still figuring out Swarm. What happens if my manager goes down, like during maintenance?
DataGuruX: About that SQLite noteโwhat's the best fix? Should I switch to Postgres or MariaDB, or just deal with the risks?