Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR: volumes.dbdata must be a mapping or null

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?

like image 439
rupali317 Avatar asked Apr 21 '26 16:04

rupali317


2 Answers

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.

like image 195
BMitch Avatar answered Apr 23 '26 16:04

BMitch


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

like image 22
Fendi jatmiko Avatar answered Apr 23 '26 16:04

Fendi jatmiko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!