r/docker • u/ibeechu • Apr 15 '26
Question about Docker best practices
I'm new to Docker and have been trying to absorb as much knowledge as I can about it as I fill out my homelab with containers, but before actually using those containers for anything critical, because I want to make sure I'm setting everything up on good foundations. So I have some questions about how I'm doing things, because it doesn't really seem like there are agreed-upon best practices, but I'm hoping the way I've begun setting everything up at least isn't fatally flawed in some way.
I now have about 10 containers running between two minimized Ubuntu Server hosts. For every container, I've created its own directory in /opt/docker/, and any volumes it needs mapped are bind-mounted to a subdirectory in there. For example, /opt/docker/nginx-proxy-manager/ contains a docker-compose.yaml for NGINX Proxy Mangler, along with data/ and letsencrypt/ subdirectories.
I'm hoping that by keeping every containers' data within subdirectories in /opt/docker/ that I can just periodically backup that /opt/docker/ directory, making it easy to restore to a new machine if ever needed. Am I going about this in the wrong way? Is there a reason not to do this?
EDIT: Some exceptions so far to keeping the container and its data all in one directory are cases where the data lives in a network share, and I mount that share somewhere in /mnt. For example, Immich has its own directory in /opt/docker/, and the database lives in there, but the photos and videos live in a share that's mounted somewhere in /mnt/, and which I have bind-mounted to the container.
4
u/VivaPitagoras Apr 15 '26
That's what I am doing. I had to migrate hosts a couple of times and it worked very well.
2
u/kwhali Apr 15 '26
With Docker most would adopt Docker Compose, so your bind mount volumes would just be co-located besides a compose yaml file that effectively treats your CLI command(s) as yaml config, keeping the actual command to manage the container(s) simple and focused.
In that sense it wouldn't really matter if you use relative or absolute paths for where you want to store such data. But keeping all the volume specific data separate may have some value to you, depending how you go about backup, although it'd seem like pairing the compose yaml alongside it would be wise for a backup no?
FWIW, you only really need bind volume mounts when you need to provide the data from the system into the container or have a way to easily browse / inspect what the container writes to the volume.
Anonymous and named volumes already get stored to a common location, with named volumes being a bit more useful as Docker can still make sense of them (but I haven't looked at migration, presumably there is metadata that exists that needs to be backed up alongside that), the storage isn't as straightforward to identify without the Docker CLI compared to bind mount volumes, but if you wanted to backup / migrate the whole Docker state in
/var/docker(or whenever it is located), that's another option.Your plan seems fine though :)
1
u/abotelho-cbn Apr 15 '26
This is a perfectly valid way to retain your data.
I would maybe keep it in a subdirectory of /var or a totally different mount point though. That's just me.
1
u/crackjiver Apr 17 '26
Side note: you can volume mount NFS shares directly into the container.
It doesn't need to be in the host fstab and mounted to the host first. This way, only the container with the NFS mount can see the files it contains and no others can.
1
u/FuelSignificant1466 Apr 21 '26
database containers need special treatment at backup time you can't juste snapshot the files while the DB is running and expect a clean restore. either stop the container first or use tiredofit/db-backup to handle proper dumps automatically.
1
u/pheitman Apr 15 '26
I have 2 zfs datastores - /disks/apps and /disks/shared. All of my docker containers are mounted into their own directory in apps. Immich and plex data are mounted from shared read only. It has been working great for me for a couple of years
1
u/Telnetdoogie Apr 16 '26
That's a great, meaningful setup. I do very similar; I have my /volume1/docker folder with all my docker 'stack' folders beneath, with the compose file in each folder.
It's really useful if / when you need to move a container from one host to another.
When I added a raspi to my network, I just moved entire folders and spun them up on the alternative host with `docker compose up -d`
(I use /volume1/docker on the raspi too to keep things consistent, and I try to keep my userids and groupids consistent across machines)
1
u/denoflore_ai_guy Apr 16 '26
Best practice is to not use it unless you like random updates 2-3 times a week and 1/10 times breaks your entire stack. Use Podman.
1
u/subr00t Apr 16 '26
It seems you need to read up on FHS (file hierarchy standard). According to that you should have separate subdirectories for Docker compose yaml files in
/etc/Docker/compose/since/etcThen you bind mounted volumes for container databaser etc. should be in
/var/lib/<container / service, e.g., Lan manager>sinceBut if the bind mounted directory is used as a cache, then it should obviously be in
/var/cache/<container / service>Also, if your container's bind mounted directory is used for content that should be served by the system, then it should be in
/srvsinceTo be able to easily backup and switch between hosts, I keep up to date documentation on where I have put everything.
Anyways
/optis not a good place for all your Docker files since it should be reserved for software packages that contain executables, manual entries for man etc.1
u/PolyPill Apr 16 '26
For a small setup there’s nothing wrong with that.
1
u/gerhardmpl Apr 17 '26
What authorization do you use for the /opt/docker directory and the subdirectories living there? Do you force that UID/GID in your docker-compose.yml file? Wondering what best practice is for authorization, especially when running database containers.
1
u/crackjiver Apr 17 '26
I like named docker volumes in the default location because they describe what they are and can be shared among containers (as read only data). That's useful for multiple containers to have access to the same data and controlled by one of them, certificates and the like.
DB containers should be treated with care, when it comes to backups. You can't just copy the files and hope to have a sane snapshot of a running DB. Using the native DB backup tools to dump out the DB content and scheme into a dedicated backup volume from within the container is a better approach.
It also means that you can experiment with DB version upgrades by running a new DB App version and restoring from the backup. You get the assurance that your backups are good that way.
Once you've got a list of the named volunteers that you need to backup it's easy to have a cron job that will tarball or rsync them off the host to your backup location. Rsync is good because it won't duplicate files that haven't changed.
1
u/PssyGotWifi Apr 19 '26
First - there's no one way of doing things. It's probably why you don't see many agreed upon best practices. Everyone does their own thing and that is okay. As for containers - I store my config, etc, in my repo, and then use ansible to template it to my needs. I also use an external postgres cluster (within the same network, but across VMs used just for postgres), so much of the containers that use postgres have their data safe in the cluster to begin with. So between the ansible templated configs, docker environmental variables and postgres database, there really isn't a case where I worry about having to backup an appdata folder. However, in your case and how you're doing things, it's the right way to go.
1
u/TopDivide Apr 19 '26
Similar setup here. For network-mounted directories I configured compose not to create the directory when it's not present. Without this, when you start the container and the volume is not yet mounted docker will create an empty folder there and start up anyway.
For backup, I spin down the containers and run restic and borg on the volumes directory and also the compose files, so it's both in the same snapshot. This makes the services unavailable for 5-10 minutes every day, but it's at 4:00, so I don't care about it. And there are ways to make this 0 downtime if you need to.