brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: external: true makes Compose use a Docker volume it did not create

Docker
~/tips/tip-docker-compose-external-volume

external: true

Share a volume between projects and keep Compose from ever removing it.

Compose prefixes every volume it creates with the project name and happily removes it with down -v. For data that should outlive the project, mark the volume as external.

services:
  app:
    image: example/app:1.0
    volumes:
      - media:/media

volumes:
  media:
    external: true
    name: shared-media

You create the volume yourself, once:

docker volume create shared-media

Compose now looks for shared-media as is, without a project prefix, and never creates or deletes it. Two projects can point at the same volume this way, and a careless docker compose down -v leaves it alone. The same external: true works for networks, which is how I would attach several stacks to one shared proxy network.

If the volume does not exist, Compose refuses to start the project with an error like external volume "shared-media" not found. That is a feature: you notice the missing data right away instead of getting a fresh empty volume. On a new machine, the create step has to be part of your setup notes or script.

Further reading: Compose file reference: volumes, Compose file reference: networks

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link