Kumar Swapnil
Kumar Swapnil

Reputation: 73

Unable to understand a Docker-compose service property

I am new to docker, and stumbled upon a docker-compose file. I get the gist of all other properties but I have no idea what below line is doing:

volumes: - ./data:/data/db

Can anyone please help me with this.

Upvotes: 2

Views: 1940

Answers (2)

Scott Stensland
Scott Stensland

Reputation: 28285

multiple volumes can be attached to your container ... each are defined as a pair

volumes:
- /parent/host/path01:/inside/container/path_one    
- /parent/host/path02:/inside/container/path_another

of each pair the left side is a pre-existing volume reachable on host before container is created ... right side is what the freshly launched container views that left side as from inside the container

in your example, in same dir where you launch docker-compose from, there evidently exists a dir called data ... using ./data will reach it using a relative path ... the right side /data/db is what the code in your container calls that same dir

/full/path/to/reach/data:/data/db

is using the absolute path to reach that same ./data dir which lives on the parent host which docker-compose is executed on

This volume mapping allows permanent storage on parent host to become visible (read/writable) to the container ... since the container filesystem is ephemeral and so goes away when container exits this volume mapping gives the container access to permanent storage for specified paths which must appear in your yaml file ... especially important for database containers like mongo ... all files used in your container not mapped in the volumes yaml disappear once the container exists

Here is a typical yaml snippet for mongo where it gains access to permanent storage on parent host

  loudmongo:
    image: mongo
    container_name: loud_mongo
    restart: always
    ports:
     - 127.0.0.1:27017:27017
    volumes:
     - /cryptdata7/var/data/db:/data/db

Upvotes: 5

ely
ely

Reputation: 77424

The dash symbol is probably what is throwing you off, because it is poorly formatted YAML syntax for a YAML list element.

The volume syntax after the dash is just following the so-called "short" syntax for a host-to-container bind-mounted volume mapping.

Upvotes: 1

Related Questions