Reputation: 512
Description of compose file: I have a docker-compose.yml file, which has a service called 'db'. It has a volume 'dbdata' on my local machine which points to '/new/folder'. This is mapped to the container's '/some/path/to/folder' path.
The following is the content of my compose file:
version: "3"
services:
db:
image: new:1
volumes:
- "dbdata:/some/path/to/folder"
ports:
- "1234:1234"
networks:
- webnet
volumes:
dbdata: /new/folder
networks:
webnet:
Issue: When I try to deploy the stack by issuing the command : docker stack deploy --compose-file docker-compose.yml service_name, I get the error as "volumes.dbdata must be a mapping or null".
I have searched for similar queries as well and the answers suggested that in yaml files, the indentation has to be correct. I ensured that the indentation is correct in the compose file. I tried enclosing /new/folder within double quotes, removing the extra space before /new/folder. Unfortunately, none worked.
Questions: Why is the error "volumes.dbdata must be a mapping or null" existing? How do i fix it?
Upvotes: 10
Views: 18926
Reputation: 264841
To map a directory in as a host volume mount, you declare the source path directly without a volume name (this is the typical solution):
version: "3"
services:
db:
image: new:1
volumes:
- "/new/folder:/some/path/to/folder"
ports:
- "1234:1234"
networks:
- webnet
networks:
webnet:
Or you can declare a named volume that is a bind mount:
version: "3"
services:
db:
image: new:1
volumes:
- "dbdata:/some/path/to/folder"
ports:
- "1234:1234"
networks:
- webnet
volumes:
dbdata:
driver: local
driver_opts:
type: none
o: bind
device: /new/folder
networks:
webnet:
There are two behavior differences to be aware of. With a named volume, docker will run the initialization step (copying files and permissions from the image into an empty named volume). However, for the bind mount to work, that directory must already exist.
Upvotes: 14
Reputation: 51876
In the volumes section below, you only declare the volume name. You can add volume and driver options to this section, but you don't declare the mapping there:
version: "3"
services:
db:
image: new:1
volumes:
- "dbdata:/some/path/to/folder"
ports:
- "1234:1234"
networks:
- webnet
volumes:
dbdata:
networks:
webnet:
Upvotes: 1
Reputation: 2895
according to docker volume docs i dont think You can map a top-level volume in docker-compose like that.
volumes: dbdata: /new/folder
or maybe i just havent heard such a feature exists,,
so in my opinion, either you map your volume per container like :
version: "3"
services:
db:
image: new:1
volumes:
- db_data: /some/path/to/folder
or
Create a mapping volume in your compose such as :
volumes:
db_data:
external:
name: volume-names
Upvotes: 1