Persistent or Not
When you want to create an app you have the option of creating the app with "Persistent Data" or not. By default, you should always prefer no persistence. However, they are cases where you need to create an app with persistence, see below.
These are the apps that have some data that need to survive restart, crash, container update and etc. Because these apps store data on disk, once they get created they get locked down on a specific server (if you have multiple servers). You can still change the constraint so that they will be moved to another machine, but if they will lose anything that might have been stored on the current host. Examples that use persistent apps include:
- Any database that stores data on disk has to have persistent data enabled. Otherwise all data will be lost when the container restarts (due to crash, host restart and etc...)
- A photo upload app which does not use third party storages like Amazon S3 to store images. Instead, it locally stores uploaded images.
- A webapp that needs to store some user uploaded files and plugins locally on disk (like WordPress)
The main limitation of apps with Persistent Data is that they cannot be run as multiple instances. That's because they would access the same storage area and the data can be corrupted if multiple apps try to write on the same path.
Note that even for Persistent Apps, NOT ALL DIRECTORIES will be treated as persistent directories. After you created the app as an app with persistent data, you'll have to define directories that you want to be persistent in the app details page on web dashboard. You can let CapRover manage the stored directories for you, in that case, they will be placed in
/var/lib/docker/volumes/YOUR_VOLUME_NAME/_data. Alternatively, you can set specific path on your host machine to be mapped to a directory in your container. For example, you can map
/var/usr on your server to
/my-host-usr-something in your container. This way you can save a file in your container at
/my-host-usr-something/myfile.txt and the file will be available on your server at
/var/usr/myfile.txt. If you choose to use this option (specifying a specific host path), you'll have to make sure that the path already exists in your host before assigning it.
Removing Persistent Apps:
Persistent directories need to be manually removed after you remove an app from Captain dashboard. This is to avoid accidential delete of important data. To delete persistent directories, depending on the type of persistent directories, steps are different:
- Volumes (persistent directories mapped to a label):
For this type, you need to run
docker volume lsto see the names of the volumes, and then run
docker volume rm NAME_OF_VOLUMEto remove the volume
- Mapped directories on host: these are directories from your server that are mapped to a directory in your container (app). To remove them, simply remove the directory from your server via
rm -rf /path/to/directory
Generally speaking, anything that does not directly stores data on disk can be made non-persistent. You should always prefer to have non-persistent apps as they are much more flexible. Let's say you have multiple servers, if a server becomes unhealthly, all "non-persistent" apps on that server will automatically get moved to other servers whereas persistent apps are locked down to that server due to some data that they saved on that server.
Also, multiple instances of non-persistent apps can be running at the same time without causing any issues as they live in isolated environment and each of them has their very own disk space. Note that non persistent apps can still write data on disk, things on temporary cache and etc, but that data will get deleted once the container restarts due to a crash, deploy, configuration update or host restart. Examples include:
- A image processor which takes a photo and runs some logic to figure out what is in the picture. This is a good example of an app that can benefit from getting spawned as multiple instances as it's CPU heavy.
- A TODO web app. Note that this app will definitely uses some sort of database which is persistent. But the webapp itself does not store anything directly on disk.
- An image upload app that uses Amazon S3 as the storage engine rather than storing images locally on disk