Gyaaniguy_
self-made · nerd
hosting 2 min read July 2024

Persistent storage with Dokku

How to add permanent storage to a dokku application

Applications hosted using Dokku run in docker containers, and all the data is lost every time the app is re-deployed. For permanent storage we either use a database or mount a volume, as described below.

# Create a Storage volume in /var/lib/dokku/data/storage/fallinprices
dokku storage:ensure-directory fallinprices

# Mount this directory into a folder on the container
dokku storage:mount fallinprices /var/lib/dokku/data/storage/fallinprices:/app/storage

# The folder is now available in the container at /app/storage
# Copy your database sqlite files into this folder. I had to scp them in my case

# restart to take effect
dokku ps:restart fallinprices

How to use files

Inside the container, files are at /app/storage/. Personally I set an environment variable: dokku config:set fallinprices DB_DIR=/app/storage/. In PHP the directory path is accessible at $_ENV['DB_DIR']. Use a different value in your local environment.

Permission issues

If files are created outside the container, you may run into permission issues. For example, a file created in /var/lib/dokku/data/storage/fallinprices is owned by root, not by the herokuish user, so the container can't write to it.

dokku permissions

Fix: copy the file into the container, then delete the original outside. You will have to rename the copied file inside the container.