All Collections › Docker Clusters › Volumes, SQLite and assets

Volumes, SQLite and assets

Keep files across deploys, back up SQLite, and serve the last release's assets during a deploy

Updated

Files written inside a container disappear when it's replaced on the next deploy. Anything that has to outlive a deploy goes on a volume.

Volumes

On the first deploy of an app that uses Active Storage or SQLite, Hatchbox mounts a Docker volume named <app>_storage at storage under the Dockerfile's WORKDIR (/rails/storage with the Rails Dockerfile). That's where Rails keeps Active Storage files and SQLite databases. Other apps start with no volumes.

Change them under Volumes in the app's settings, one source:target per line. A name makes a Docker volume Hatchbox manages; a path starting with / mounts a directory on the server.

Docker volumes live under /var/lib/docker/volumes/<name>/_data on each server. A volume belongs to one server, so files on it aren't shared between servers. Use S3 or another object store for Active Storage on a multi-server cluster.

Renaming an app moves its <app>_* volumes to the new name instantly, without copying, and the app keeps running.

SQLite

Because a volume belongs to one server, an app on SQLite only works on a single-server cluster.

Hatchbox doesn't find an app's SQLite databases on its own, and until you add them they aren't backed up. Each deploy of an app with the sqlite3 gem reminds you. On the app's Databases page, create a database under SQLite for each file, with its path inside the container (/rails/storage/production.sqlite3 in a rails new app), and turn on its backups.

A backup copies the file with sqlite3 in the app's current image while the app keeps running, so the image needs the sqlite3 command. The generated Dockerfile installs it when Gemfile.lock has the gem. The database's page shows how to restore a backup into the live database. Active Storage files and anything else on the volume aren't backed up.

Assets across deploys

A page served just before a deploy asks for its stylesheet and JavaScript just after it, from the new container, and each release has a different set of fingerprinted files. So on each deploy Hatchbox copies the release's assets out of the image into a directory on each server, keeps the previous release's beside them, and mounts that directory over the image's own in both the old and the new containers. The previous release's assets are removed once its containers are gone.

Hatchbox turns this on for apps whose Gemfile.lock has Propshaft or Sprockets, at public/assets under the Dockerfile's WORKDIR (/rails/public/assets with the Rails Dockerfile). Change the path under Assets kept across deploys if your image keeps them elsewhere, or blank it to serve each release's own assets only.