Sharing Docker Volumes in DDEV
In a recent project, we dealt with 2 DDEV instances — 2 applications that need to communicate with each other or, more specifically, exchange files.
Since DDEV is mainly a wrapper around Docker and Docker Compose, we can use shared Docker volumes to achieve our needs.
First, let's create the shared Docker volume with the following command:
docker volume create customer-shared-volume
Next, in both DDEV projects, we need to add another Docker Compose file to configure the shared volume for the web container, which is where our apps are running in.
Create a new file .ddev/docker-compose.mounts.yaml
with the following content:
services:
web:
volumes:
- "shared-vol:/var/www/html/shared-volume:rw"
volumes:
shared-vol:
name: "customer-shared-volume"
external: true
2 important things to mention:
- The shared volume
shared-vol
needs to have a name configured. Otherwise, Docker and Docker Compose, specifically, will prefix the shared volume with the project name, and thus, you will end up with two distinct volumes that do not share the files. - The
external: true
flag is important because last year, Docker Compose version 2.19.1 implemented some tighter security checks. I blogged about a similar problem I had with shared networks last year.
Once you have added the .ddev/docker-compose.mounts.yaml
in each project, you can ddev start
your projects, and the shared volume should be available in both applications.