skip to Main Content

docker compose up and docker compose down work like a charm: creating everything from scratch and wiping without a trace.

That puts data in the volumes at the mercy of “fat fingers” typing down instead of stop.

Is there a setting in the docker-compose.yml to tell: don’t nuke this volume?

2

Answers


  1. You have two options.

    Option A:

    Create the volume manually:

    docker volume create web_data
    

    And mark it as external in your docker-compose.yaml file:

    volumes:
      web_data:
        external: true
    

    Option B:

    Mount a host directory as the volume:

    volumes:
        - /path/to/host/directory:/path/to/container/directory
    
    Login or Signup to reply.
  2. As I known, docker compose down won’t delete volume, but if you didn’t specify the volume name, it will create a new volume as default to bind.

    And every volume docker create won’t delete until you type docker volume prune, you can check by docker volume ls to see if there is a lot of none-name volumes.

    You can create a volume by docker create volume example, and define to use it in docker-compose.yml, then it will use same volume on next docker compose up.

    For example what I did:

    1. Create volume by docker volume create mongodb.

    2. Edit docker-compose.yml.

    services:
        mongo:
            ...
            volumes:
                - mongodb:/data/db
    
    volumes:
        mongodb:
            external: true
            name: mongodb
    

    Docker document also said removing the service doesn’t remove any volumes created by the service. Volume removal is a separate step, more details see here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search