简体   繁体   中英

docker-compose volumes not mounting directories, only files inside directory

Might sound weird, but I am trying to get docker-compose to mount directories to a volume I created

volumes:
  - backend-data:/app/migrations/autogen-migrations
  - backend-data:/app/seeds/autogen-seeds
  - backend-data:/app/server/public
  - backend-data:/app/server/src/services/location

Only problem is, that instead of simply mapping the folders to the volume, it's mounting the content inside the volume. Is there any way to tell docker-compose to copy/map the folder itself?

Edit: Already tried doing

backend-data/autogen-migrations:/app/migrations/autogen-migrations

And I get the following error:

Named volume "backend-data/autogen-migrations:/app/migrations/autogen-migrations:rw" is used in service "backend" but no declaration was found in the volumes section.

Btw this is how my volumes are declared

volumes:
  backend-data:
    driver: local

When you need mount a local directory as Docker volume, you have use this syntax in your service:

volumes:
  - ./your/local/path:/app/migrations/autogen-migrations

In this way, Docker create a local path "./your/local/path" in the same folder where docker-compose.yml file is, to store the data of mounted volume. In this case you don't need specify the volume section in the docker-compose.yml because you manage the volumes by yourself. If you need mount more than one folder, remember also to mount more than one local folder:

volumes:
  - ./your/local/migrations/or/whatever/you/want:/app/migrations/autogen-migrations
  - ./your/local/seeds:/app/seeds/autogen-seeds
  - ./your/local/server/public:/app/server/public
  - ./your/local/server/src:/app/server/src/services/location

You can also aggregate mounts folder under the same folder:

volumes:
  - ./your/local/migrations:/app/migrations/autogen-migrations
  - ./your/local/seeds:/app/seeds/autogen-seeds
  - ./your/local/server:/app/server

In this case into './your/local/server' you found all '/app/server/' content.

If you use the syntax:

volumes:
  backend-data:
    driver: local

you tell Docker: "Docker, please, mount the folder which correspond to backend-data: (in your case backend-data:/app/migrations/autogen-migrations ) when you want and store my data.", In this case. Docker manage the "local" folder by itself without use the the same folder where docker-compose.yml file is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM