D
D
dipech2019-02-02 17:49:08
Docker
dipech, 2019-02-02 17:49:08

How to change storage path of named volumes on host machine in docker?

What is available:
There is a production server on which it is planned to keep a web application that will be spinning using docker. I use docker-compose and named volumes to help share data between containers.
The server has 2 disks: HDD (2 TB) and SSD (500 GB).
What you need:
You need to store part of the persistent data on the HDD (for example, large user data), and store the rest of the data on the SSD (source code and database).
What I have already tried:
After reading hundreds of manuals and articles in the documentation, I found the following approach, described below (docker-compose code is attached).
This approach, judging by the information found, works for most people (but not for me, for some reason).
It is also worth noting that all necessary paths on the host machine (for example: “/awesomeapp_source_files” ) have been pre-created and all permissions and owners have been configured.
docker-compose.prod.yml

volumes:
   volume_source:
       driver: local
       driver_opts:
           type: none
           o: bind
           # SSD
           device: /awesomeapp_source_files
   volume_database:
       driver: local
       driver_opts:
           type: none
           o: bind
           # SSD
           device: /awesomeapp_database_files
   volume_public_data:
       driver: local
       driver_opts:
           type: none
           o: bind
           # HDD
           device: /var/awesomeapp/public_files
   volume_private_data:
       driver: local
       driver_opts:
           type: none
           o: bind
           # HDD
           device: /var/awesomeapp/private_files

services:
   # Контейнер содержит исходный код приложения. Исходники расшариваются при помощи named volumes.
   app:
       volumes:
           - volume_source:/var/www
           - volume_public_data:/var/www/public/data:nocopy
           - volume_private_data:/var/www/data:nocopy

   nginx:
       volumes:
           - volume_source:/var/www:nocopy
           - volume_public_data:/var/www/public/data:nocopy
           - volume_private_data:/var/www/data:nocopy

   mysql:
       volumes:
           - volume_database:/var/lib/mysql

   php:
       volumes:
           - volume_source:/var/www:nocopy
           - volume_public_data:/var/www/public/data
           - volume_private_data:/var/www/data

The key settings are these:
driver_opts:
    type: none
    o: bind
    device: /path/to/folder/on/host

If I delete them, named volumes are stored on the host machine in the standard paths: “/var/lib/docker/volumes/{NAMED_VOLUME_NAME}/_data” . And my application works as expected.
However, once I use these settings, my directories on the host machine ( “/awesomeapp_source_files” , “/var/awesomeapp/private_files” , “/var/awesomeapp/public_files” ) and directories inside containers ( “/var/www ” , “/var/www/data” , “/var/www/public/data” ) are empty.
My assumption (I don't know if it's correct) is that the data is not synchronized from containers to the host, and then from host directories to containers.
But I have no idea how to check it. And what needs to be done to correct the behavior.
Please help with a solution. I hope for your help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question