define volumes in docker-compose.yaml
Asked Answered
C

2

23

I am writing a docker-compose.yaml file for my project. I have checked the volumes documentation here .

I also understand the concept of volume in docker that I can mount a volume e.g. -v my-data/:/var/lib/db where my-data/ is a directory on my host machine while /var/lib/db is the path inside database container.

My confuse is with the link I put above. There it has the following sample:

version: "3.9"

services:
  db:
    image: db
    volumes:
      - data-volume:/var/lib/db
  backup:
    image: backup-service
    volumes:
      - data-volume:/var/lib/backup/data

volumes:
  data-volume:

I wonder does it mean that I have to create a directory named data-volume on my host machine? What if I have a directory on my machine with path temp/my-data/ and I want to mount that path to the database container /var/lib/db ? Should I do something like below?

version: "3.9"

services:
  db:
    image: db
    volumes:
      - temp/my-data/:/var/lib/db

volumes:
  temp/my-data/:

My main confusion is the volumes: section at the bottom, I am not sure whether the volume name should be the path of my directory or should be just literally a name I give & if it is the latter case then how could the given name be mapped with temp/my-data/ on my machine? The sample doesn't indicate that & is ambiguous to clarify that.

Could someone please clarify it for me?

P.S. I tried with above docker-compose I guessed, ended up with the error:

ERROR: The Compose file './docker-compose.yaml' is invalid because:
volumes value 'temp/my-data/' does not match any of the regexes: '^[a-zA-Z0-9._-]+$'
Chondriosome answered 4/8, 2021 at 7:39 Comment(0)
F
15

Mapped volumes can either be files/directories on the host machine (sometimes called bind mounts in the documentation) or they can be docker volumes that can be managed using docker volume commands.

The volumes: section in a docker-compose file specify docker volumes, i.e. not files/directories. The first docker-compose in your post uses such a volume.

If you want to map a file or directory (like in your last docker-compose file), you don't need to specify anything in the volumes: section.

Docker volumes (the ones specified in the volumes: section or created using docker volume create) are of course also stored somewhere on your host computer, but docker manages that and you shouldn't normally need to know where or what the format is.

This part of the documentation is pretty good about explaining it, I think https://docs.docker.com/storage/volumes/

Footboy answered 4/8, 2021 at 8:9 Comment(0)
H
11

As @HansKilian mentions, you don't need both volumes and services.volumes. To use services.volumes, map the host directory to the container directory like this:

services:
  db:
    image: db
    volumes:
      - /host/path/lib/db:/container/path/lib/db

With that, the directory /host/path/lib/db on the host machine will be used by the container and available at /container/path/lib/db.

Now, if you're like me, I get really confused with fake examples, so let's say the real directory on your host machine is /var/lib/db and you just want to see it at /db when you run a shell in Docker (i.e., docker exec -it /bin/bash container-id).

docker-compose.yaml would look like this:

services:
  db:
    image: db
    volumes:
      - /var/lib/db:/db

Now when you run the shell, cd /db and ls, you'll see the same results as if you'd cd /var/lib/db on the host.

If you want to use the volumes section to indicate a global volume to use, you first have to create that volume using docker volume create. The documentation Hans linked includes steps to do this. The syntax of /host/path:/container/path is replaced by volume-name:/container/path. Then, once defined, you'd alter your docker-compose.yaml to be more like this:

services:
  db:
    image: db
    volumes:
      - your-global-volume-name:/db

volumes:
  your-global-volume-name:
    external: true

Note that I have not tested or used the this configuration. I'm assuming it's correct based on the other method working and the few changes I can identify in the docs.

Hetti answered 10/2, 2023 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.