Guides

Data in host folders

By default the database and the files live in Docker volumes. This guide keeps them in folders you choose instead, owned by one dedicated local user, and runs both containers as that user, so only it can read the data.

Optional

Named volumes work fine; choose host folders when you want the data on a particular disk, to back it up with your usual tools, or to keep it readable by one user only. Do this right after step 2 of the install, before the first start, or see Moving existing data.

Where everything goes

PathHoldsOwner
/opt/homespace/compose.yamlThe compose file from the install guide, unchanged.root
/opt/homespace/compose.override.yamlThe changes below: folders and container user.root
/opt/homespace/.envSecrets and settings, mode 600.root
/srv/homespace/dbThe PostgreSQL files.homespace, mode 700
/srv/homespace/filesThe uploaded files and their versions.homespace, mode 700
/etc/nginx/conf.d/homespace.confThe site from the nginx guide.root
/srv/homespace is only an example

Put the data wherever suits you, for example /mnt/data/homespace on a separate disk. If you choose another location, use it everywhere this guide says /srv/homespace: when creating the folders, in the SELinux commands, and in HOMESPACE_DB_DIR and HOMESPACE_DATA_DIR in .env. The compose override needs no change, since it reads the folders from .env. Keep the database on a local disk, not on a network share.

1. Create the local user

A system user with no home and no login shell. It only owns the data:

shell
sudo useradd --system --user-group --no-create-home --shell /usr/sbin/nologin homespace
id homespace

id prints its uid and gid, for example uid=990(homespace) gid=990(homespace). The numbers differ per machine; the steps below read them with id -u and id -g.

2. Create the folders

Create both folders, owned by homespace and closed to everyone else:

shell
sudo install -d -m 700 -o homespace -g homespace /srv/homespace
sudo install -d -m 700 -o homespace -g homespace /srv/homespace/db /srv/homespace/files

With SELinux enforcing (Fedora, RHEL and their relatives), also allow containers to use them:

SELinux
sudo semanage fcontext -a -t container_file_t '/srv/homespace(/.*)?'
sudo restorecon -R /srv/homespace

Do not use the :Z volume option instead: on the /etc/passwd mount below it would relabel a system file.

3. Add the compose override

Download compose.override.yaml next to compose.yaml. Docker Compose reads a file with this name on its own and merges it into compose.yaml, so every command in the other guides stays the same:

shell
cd /opt/homespace
sudo curl -fsSLO https://home-space.app/docs/compose.override.yaml
compose.override.yaml
# Home Space: keep the database and the files in host folders, owned by one local user.
# Guide: https://home-space.app/docs/host-folders/
#
# Put this file next to compose.yaml: docker compose reads compose.override.yaml on its own,
# so the usual commands stay the same (docker compose up -d, pull, logs ...).
#
# Add to .env:
#   HOMESPACE_DB_DIR     host folder for the Postgres files, e.g. /srv/homespace/db
#   HOMESPACE_DATA_DIR   host folder for the files,          e.g. /srv/homespace/files
#   HOMESPACE_UID        uid of the owning local user  (id -u homespace)
#   HOMESPACE_GID        gid of the owning local user  (id -g homespace)
services:
  db:
    user: "${HOMESPACE_UID:?set HOMESPACE_UID}:${HOMESPACE_GID:?set HOMESPACE_GID}"
    volumes:
      - ${HOMESPACE_DB_DIR:?set HOMESPACE_DB_DIR}:/var/lib/postgresql
      # Postgres looks its running user up by uid; an arbitrary uid has no passwd entry in the image.
      - /etc/passwd:/etc/passwd:ro

  server:
    user: "${HOMESPACE_UID:?set HOMESPACE_UID}:${HOMESPACE_GID:?set HOMESPACE_GID}"
    volumes:
      - ${HOMESPACE_DATA_DIR:?set HOMESPACE_DATA_DIR}:/data

If you prefer one file, copy the same user: and volumes: lines into the two services of compose.yaml and delete the volumes: block at its end.

4. Add the folders and the user to .env

Append the four values, reading the uid and gid from the user you created:

shell
cd /opt/homespace
sudo tee -a .env > /dev/null <<EOF

# Data in host folders, owned by the homespace user
# Your own locations, if you chose others
HOMESPACE_DB_DIR=/srv/homespace/db
HOMESPACE_DATA_DIR=/srv/homespace/files
HOMESPACE_UID=$(id -u homespace)
HOMESPACE_GID=$(id -g homespace)
EOF

Check the merged configuration, then start as in step 3 of the install:

shell
sudo docker compose config | grep -E 'user:|/srv/homespace'
sudo docker compose up -d
sudo docker compose logs -f server

Compose stops with set HOMESPACE_… if one of the four values is missing.

Check it

shell
sudo ls -ln /srv/homespace/db /srv/homespace/files
sudo docker compose exec server id

Everything in both folders should belong to the uid and gid of homespace, and the server should report the same numbers. If Postgres exits with Permission denied, the folder is owned by someone else or, on SELinux, is missing its label.

Back up

Dump the database as before, and copy the files folder with any tool you like:

shell
cd /opt/homespace
sudo docker compose exec -T db pg_dump -U homespace -Fc homespace > homespace-db.dump
sudo tar -C /srv/homespace/files -czf homespace-files.tar.gz .

Copy /srv/homespace/db itself only while the database is stopped; the dump is the safe way while it runs.

Moving existing data

Already running with the named volumes? Stop the containers, copy the volumes into the folders, and give them to homespace. Do steps 1 and 2 first and keep the same Postgres version.

shell
cd /opt/homespace
sudo docker compose down
sudo cp -a "$(sudo docker volume inspect -f '{{ .Mountpoint }}' homespace_db)/." /srv/homespace/db/
sudo cp -a "$(sudo docker volume inspect -f '{{ .Mountpoint }}' homespace_files)/." /srv/homespace/files/
sudo chown -R homespace:homespace /srv/homespace
sudo restorecon -R /srv/homespace   # SELinux only

Then do steps 3 and 4 and start again. Once everything works from the folders, the old volumes can go: docker volume ls lists them, and removing them deletes their data for good.