6

When I create a volume manually and include it in docker-compose, if I don't prefix the volume tag with docker_, docker compose creates a new volume prefixed with docker_ For example: I create a volume with:

docker volume create myvolume

It's visible at /var/lib/docker/volumes/myvolume. I include it in my docker-compose yaml file, but when I run docker-compose, a new volume is created at /var/lib/docker/volumes/docker_myvolume If I call my volume docker_myvolume and include that in my docker-compose yaml, it uses it and doesn't create it's own.

Is this normal behavior?

wedwo
  • 338
  • 3
  • 11
  • 1
    can you provide your docker sample docker-compose.yaml file or you can read the official docs from docs.docker.com, https://docs.docker.com/compose/compose-file/#volumes. – Shudipta Sharma Apr 12 '20 at 16:54

2 Answers2

12

Yes, this is normal behavior. When you specify a volume in your docker-compose.yml file without a leading driver_ prefix, Docker Compose will create a new volume with a name that is prefixed with driver_. This is because Docker Compose uses a default driver for creating and managing volumes, which is the local driver.

You can specify a volume in your docker-compose.yml file with the external option to tell Docker Compose to use an existing volume instead of creating a new one. For example:

version: '3'
services:
  myservice:
    volumes:
      - type: volume
        source: myvolume
        target: /app/data
        volume:
          external: true

This will tell Docker Compose to use the existing volume myvolume instead of creating a new one.

mhadidg
  • 1,503
  • 17
  • 29
2

Yes, Compose generally prefixes things with its project name. This includes containers, networks, and named volumes. In general, if you actually need to interact with these things, there is an equivalent docker-compose command that chooses the correct name (e.g., docker-compose exec).

In general you shouldn't be directly modifying things inside /var/lib/docker. That directory tree is Docker's private state and there are no particular guarantees about the format of files there. If your use case involves directly interacting with the volume files from the host, either use a /host/path:/container/path bind mount or explicitly specify the storage location using volume options.

David Maze
  • 130,717
  • 29
  • 175
  • 215