Why in docker-compose I should write full path to volume?

Viewed 3073

I have some confusing moments with docker-compose volumes (In docker windows server (container)). I wrote the next configuration

volumes:
  - "C:/ProgramData/Docker/volumes/admin-stat-logs:C:/app/Logs"

and why it so necessary to write the full path to the volume?

if I write:

volumes:
  - "admin-logs:C:/app/Logs"

it generates the error ERROR: Named volume "admin-logs:C:/app/Logs:rw" is used in service "admin-stat-table" but no declaration was found in the volumes section.

why does it happen?

UPDATE

let me show what I mean(I made wrong explonation). In an image, we have a short announcement from the official documentation.

enter image description here

I write the same in my case:

volumes:
  - admin-logs:C:/app/Logs

I create volume before by the terminal. When I start docker-compose up I get the next error

ERROR: Named volume "admin-stat-logs:C:/app/Logs:rw" is used in service "admin-stat-table" but no declaration was found in the volumes section.

I made a mistake in the first case, it's a simple mount to a folder, sorry for my misunderstanding.

volumes:
  - "C:/ProgramData/Docker/volumes/admin-stat-logs:C:/app/Logs"
2 Answers

You don't need to use a full path. You can use a relative path to the location of your docker-compose.yml file.

When you write admin-logs:C:/app/Logs, you're telling docker-compose to use a volume named admin-logs. If you want to use a folder where you docker-compose is located, you can write ./admin-logs:C:/app/Logs.

If you plan to use a separated docker volume, you need to first define it in the docker-compose.yml. Here's an example covering both cases:

version: '3'
services:
  mytest:
    image: ubuntu:18.04
    volumes:
      - "mymnt:/mnt/volume"
      - "./mymnt_on_host:/mnt/mounted_folder"
volumes:
  mymnt:

You'll need a mymnt_on_host directory in the folder where the docker-compose.yml is located.

For more info about this topic, you can check the reference guide for the docker-compose.yml

UPDATE:

If you plan to use an already created volume in your docker-compose, you can use external: true in the volumes definition.

e.g.

version: '3'
services:
  mytest:
    image: ubuntu:18.04
    volumes:
      - "mymnt:/mnt/volume"
      - "./mymnt_on_host:/mnt/mounted_folder"
volumes:
  mymnt:
  my_existing_volume:
    external: true

Now if you start this, docker-compose won't try to generate my_existing_volume but it'll expect to find it already on the machine.

For more info check external reference.

Can you show your version value in the docker-compose.yml file. It should be version: '3' or version: '3.5'. Probably you have version: '2'

Related