How to pass docker volume name to docker compose.yml

I know needed volume name

sudo docker volume ls
...
cfc151dc475be39535b438d54d245107c5f1ce022622a0eae53bc03016aa987d

And I want to pass this volume (but with opportunity to changing it in command line) to docker compose file.

nginx:
  image: "nginx:alpine"
  ports:
    - 6000:80
  links:
    - registry:registry
  volumes:
    - ./auth:/etc/nginx/conf.d
    - ./auth/nginx.conf:/etc/nginx/nginx.conf:ro
    - ./auth/domain.crt:/etc/ssl/certs/nginx-selfsigned.crt:ro
    - ./auth/domain.key:/etc/ssl/private/nginx-selfsigned.key:ro

registry:
  image: 5money:latest
    volumes:
    - registry-external-data:/var/lib/registry

volumes:
  registry-external-data:
    external: true
    ??name: $data??

I donā€™t understand last line in composer file, what is correct? I want start compose something like this:

sudo docker-compose -f 5money-compose.yml up -d -e data=cfc151dc475be39535b438d54d245107c5f1ce022622a0eae53bc03016aa987d
  • docker-compose up does not have -e to set environment variables
  • docker-compose run has but those variables will be available by a process in the running container not by the yaml parser
  • You can use variables in the docker-compoye.yml if you create a file named .env next to the docker-compose.yml to set the variable like:
    data=cfc151dc475be39535b438d54d245107c5f1ce022622a0eae53bc03016aa987d

You could also ā€œrenameā€ the volume. I mean copy and delete the original. I created some scripts for Docker years ago. I tried docker-volume-copy now and still works. GitHub - itsziget/scripts-for-docker: Useful scripts you can use with Docker to save some time

docker-volume-copy cfc151dc475be39535b438d54d245107c5f1ce022622a0eae53bc03016aa987d registry-external-data

Then you could know what that volume is when you run docker volume ls. Obviously you would have to change the value of the variable.

Cool, this is great scripts. And thank you, Iā€™m understand about -e parameters to composer. But Iā€™m still confused - what correct last line in compose file? Iā€™m not sure about ā€œnameā€ - is this correct?

The last line is correct except those question marks, but I am sure those are not in the original compose file.

Donā€™t use links, they have been deprecated

1 Like