How To Share a Workspace with Compose#
When using Docker Compose, it’s very common to share your project with the service containers through a bind mount, such as:
1services:
2 web:
3 build: .
4 ports:
5 - "8000:5000"
6 volumes:
7 - .:/code
8 redis:
9 image: "redis:alpine"
However, this tries to bind a directory from the docker daemon host, not inside the development environment. So how does one share code with services?
Explicitly Name Your Workspace#
While this isn’t strictly necessary, since you’re going to refer to your workspace volume by name, I suggest explicitly naming it.
1---
2[dev]
3# ...
4volume = "workspace"
5# ...
6---
7# ...
Warning
If anyone has an Unholy workspace, and you change this value from
"workspace"
, everyone must recreate their entire Unholy project.
unholy remake
will not save you.
Tell Compose#
Ok, the actually important part:
Tell Compose about your workspace volume
Mount it into your services
Like so:
1services:
2 web:
3 build: .
4 ports:
5 - "8000:5000"
6 volumes:
7 - workspace:/code
8 redis:
9 image: "redis:alpine"
10
11volumes:
12 workspace:
Warning
docker compose down --volumes
will now attempt to delete your workspace.
It’ll fail (probably), but it’s going to try.
Recreate Your Services#
Ask Compose to recreate your services:
$ docker compose up -d
All Done!#
Now things like code auto-reload should work as expected.