Reputation: 123
I am new to docker and volumes and is confused about how named volumes are working. I have two scenarios in which I want to know how the named volumes will work
First Scenario
I have to setup two projects with docker and both have separate databases. Now how the database volumes will be mapped with /var/lib/mysql
? Does it maintain separate data based on db name?
Second Scenario I have two services using same named volume. In both the services, the path of container mapped to the named volume is different. How this will work?
services:
s1:
volume:
- vol:/var/lib/s1
s2:
volume:
- vol:/var/lib/s2
volumes:
vol:
Upvotes: 0
Views: 449
Reputation: 17624
Since you are using docker-compose
, it does some things for you. If your composed "project name" is project_a
, the docker-compose vol
volume will be named project_a_vol
. Verify this by running docker volume ls
. By a "composed project name" I mean the name of the project which usually equals to the name of the directory in which the docker-compose
was run, or custom one if the --project-name
parameter was set (eg. docker-compose --project-name xxx up
)
I assume you're using the default docker volume filesystem storage driver. A named volume is nothing more than a directory inside the /var/lib/docker/volumes
folder (try it sudo ls -l /var/lib/docker/volumes
). By mounting a volume using vol:/var/lib/s1
you tell docker to synchronize directories:
Local /var/lib/docker/volumes/project_a_vol
with container directory /var/lib/s1
.
If you compose your services this way:
services:
s1:
volumes:
- vol:/var/lib/s1
s2:
volumes:
- vol:/var/lib/s2
The same directory will be mounted to 2 services: s1
and s2
and you most probably will have a problem because 2 services will try to read & write to the same directory at the same time. Unless those services can handle such case.
It's better to have separate volumes though. In such case a volume for one service can be purged leaving the other one intact.
Upvotes: 1
Reputation: 530
Some hints to your questions.
First Scenario : Two docker containers with a DB each.
- For this scenario since the databases are different, they run on their own container space.
- You can create a Docker Volume or use Docker bind mounts to attach disk to your Database `/var/lib/mysql'
- If you use volumes, you create one volume per database and the data are isolated.
- If you use bind mounts, make sure you mount different disk locations, if you use same location, the second database container data will overwrite the first database data.
Second Scenario : As per this scenario, since the Volume label is same, the second coming up service data would replace the already running Service data every time the services start.
Upvotes: 0