Persistent Volumes with Docker Compose using Dokploy for Directus
If you're like me, you're relatively new to Docker and trying to learn self-hosting for small projects. You might be using a reasonably new PaaS tool called Dokploy to manage your containers. In this article, we'll explore how to run Directus with persistent volumes using Docker Compose and Dokploy.
What are Persistent Volumes in Docker Compose?
Persistent volumes in Docker Compose are a way to store data outside of a container, allowing the data to persist even if the container is stopped or removed. This is especially useful for applications like Directus, where you want to keep your data even if you need to rebuild or update your containers.
Prerequisites
Before we begin, make sure you have the following:
- Docker installed
- Docker Compose installed
- A Directus project set up
- Dokploy installed
Setting up Docker Compose
First, let's create a docker-compose.yml file in the root of your Directus project:
version: '3.8'
services:
directus:
image: directus/directus:v9.0.0
restart: always
ports:
- "8055:8055"
environment:
- DATABASE_CLIENT=postgres
- DATABASE_HOST=db
- DATABASE_PORT=5432
- DATABASE_NAME=directus
- DATABASE_USER=directus
- DATABASE_PASSWORD=directus
- DIRECTUS_SECRET=directus
- DIRECTUS_HOSTNAME=localhost
- DIRECTUS_PORT=8055
depends_on:
- db
db:
image: postgres:13.3
restart: always
environment:
- POSTGRES_USER=directus
- POSTGRES_PASSWORD=directus
- POSTGRES_DB=directus
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
This sets up two services: directus and db. The directus service uses the Directus image and maps port 8055 to the host. The db service uses the PostgreSQL image and maps the data directory to a persistent volume named db_data.
Setting up Dokploy
Next, let's create a dokploy.yml file in the root of your Directus project:
services:
directus:
image: directus/directus:v9.0.0
ports:
- "8055:8055"
environment:
- DATABASE_CLIENT=postgres
- DATABASE_HOST=db
- DATABASE_PORT=5432
- DATABASE_NAME=directus
- DATABASE_USER=directus
- DATABASE_PASSWORD=directus
- DIRECTUS_SECRET=directus
- DIRECTUS_HOSTNAME=localhost
- DIRECTUS_PORT=8055
depends_on:
- db
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
This sets up the same services and volumes as the docker-compose.yml file, but in a format that Dokploy can understand.
Deploying with Dokploy
Finally, let's deploy our project with Dokploy:
dokploy up
This will start your Directus project with persistent volumes, allowing your data to persist even if the containers are stopped or removed.
In this article, we explored how to use persistent volumes with Docker Compose and Dokploy for Directus. We covered what persistent volumes are, how to set up Docker Compose and Dokploy, and how to deploy your project with persistent volumes.
References
- Directus Documentation: https://docs.directus.io/
- Docker Compose Documentation: https://docs.docker.com/compose/
- Dokploy Documentation: https://dokploy.com/docs/