Data Volumes
- Can be specified in
Dockerfile
with VOLUME - They outlive container/image -- they need to be removed manually
VOLUME /var/lib/mysql # example from official mysql image
Can be also seen in image metadata
~> docker image inspect mysql
...
"Volumes": {
"/var/lib/mysql": {}
},
...
Or in container metadata
~> docker container inspect mysql
...
"Mounts": [
{
"Type": "volume",
"Name": "60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626",
"Source": "/var/lib/docker/volumes/60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
...
"Volumes": {
"/var/lib/mysql": {}
},
...
List volumes and details
~> docker volume ls
DRIVER VOLUME NAME
local 60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626
~> docker volume inspect 60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626
[
{
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626/_data",
"Name": "60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626",
"Options": {},
"Scope": "local"
}
]
Prove that data are safe
# check that we have active container and volume
~> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2b4303ca2b09 mysql "docker-entrypoint..." 15 minutes ago Up 15 minutes 3306/tcp mysql
# check that we have a volume
~> docker volume ls
DRIVER VOLUME NAME
local 60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626
# remove a container
~> docker container rm -f mysql
# check that no container left
~> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# volume is still present
~> docker volume ls
DRIVER VOLUME NAME
local 60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626
Named volumes
- Friendly way to assign volumes to containers
-v name:path
~> docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true -v mysql-db:/var/lib/mysql mysql
8b27c98f1365c834e64dba06d948126fd87e7a49a4902937e732b7ab6cdcbc59
~> docker volume ls
DRIVER VOLUME NAME
local 60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626
local mysql-db
~> docker volume inspect mysql-db
[
{
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/mysql-db/_data",
"Name": "mysql-db",
"Options": {},
"Scope": "local"
}
]
- Reusing
-v name:path
doesn't create new volume, but attach to existing one
~> docker container run -d --name mysql2 -e MYSQL_ALLOW_EMPTY_PASSWORD=true -v mysql-db:/var/lib/mysql mysql
0896580e6313ac6fa9b94956e3ccdf301fd6e98dc0f73302632738c7e45a582c
~> docker volume ls
DRIVER VOLUME NAME
local 60afa8e3350f96256e33a8459b4af4742a45d102ee09d527dd5e44a1fc46d626
local mysql-db
docker volume create
- required to do this before "docker run" to use custom drivers and labels