Welcome to Day 13 of 21 days of Docker. In the first part of the series, we see all the issues that are been faced if you store the data inside the container, so you need a reliable place to store the data.
Docker has two options for containers to store files in the host machine so that the files are persisted even after the container stops:
- volumes
- bind mounts
- tmpfs
- Volumes are stored in a part of the host filesystem which is managed by Docker (
/var/lib/docker/volumes/
on Linux). Non-Docker processes should not modify this part of the filesystem. Volumes are the best way to persist data in Docker. - Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.
tmpfs
mounts are stored in the host system’s memory only, and are never written to the host system’s filesystem.
One more issue I want to highlight, This extra abstraction reduces performance as compared to using data volumes, which write directly to the host filesystem.
Before digging deeper into volumes, let start with a simple example, up to this point we know that overlay2 is our default driver and docker storage directory location is /var/lib/docker
- Let’s go one level down
cd /var/lib/docker/overlay2
brw------- 1 root root 202, 1 Oct 18 00:00 backingFsBlockDev
drwx------ 3 root root 47 Oct 18 00:15 7d3b75987d3723a085cb7c7482386f9a75bf2f4b40f7c8f0ba6f3450e9da14a2
drwx------ 2 root root 40 Oct 18 00:17 l
- Let spin one container
# docker container run -dt alpine sh
6a315533797a336f1b5ee51140eb5dbc2b5d7bc28146ca0ad212ff23146cf5fa
- Again run ls -l /var/lib/docker/overlay2
# ls -ltr
total 0
brw------- 1 root root 202, 1 Oct 18 00:00 backingFsBlockDev
drwx------ 3 root root 47 Oct 18 00:15 7d3b75987d3723a085cb7c7482386f9a75bf2f4b40f7c8f0ba6f3450e9da14a2
drwx------ 2 root root 108 Oct 18 00:20 l
drwx------ 4 root root 72 Oct 18 00:20 cbcaa1e4c60c97533105b5858ff220d72cc31258dea7456d9f789e369e721af2-init <---
drwx------ 5 root root 69 Oct 18 00:20 cbcaa1e4c60c97533105b5858ff220d72cc31258dea7456d9f789e369e721af2 <---
- You will see to additional layer, which is container writable layer
- Now let’s stop and delete the container
# docker stop 6a315533797a
6a315533797a
# docker rm 6a315533797a
6a315533797a
- Again run ls -l /var/lib/docker/overlay2
# ls -ltr
total 0
brw------- 1 root root 202, 1 Oct 18 00:00 backingFsBlockDev
drwx------ 3 root root 47 Oct 18 00:15 7d3b75987d3723a085cb7c7482386f9a75bf2f4b40f7c8f0ba6f3450e9da14a2
drwx------ 2 root root 40 Oct 18 00:22 l
- So those two layers are gone now, that means all our data that is stored in that layer, we need some way to store our data persistently
- To verify any volume present in your system
# docker volume ls
DRIVER VOLUME NAME
- To create a volume
# docker volume create mytestvol
mytestvol
- Verify it again
# docker volume ls
DRIVER VOLUME NAME
local mytestvol
- Now run a container using this Volume
# docker container run -dt --name voltest -v mytestvol:/etc alpine sh
0dbffcc4beb99787ee45d6dbfd2f87ab0ba819614ce9bd6403d6b2e9c5edb8
- To get more information about the Volume
# docker volume inspect mytestvol
[
{
"CreatedAt": "2019-10-18T00:27:51Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/mytestvol/_data", <------
"Name": "mytestvol",
"Options": {},
"Scope": "local"
}
]
Pages: 1 2