-
Notifications
You must be signed in to change notification settings - Fork 2
Volumes
There are two kinds of data:
- Persistent: It's that information we want to keep, for instance, if we have a MySQL database, we want to save all our records.
- Non-persistent: Non-relevant information. cache, logs, and so on.
By default, every container has its own non-persistent storage (most OS use overlay2 and it stores container information under /var/lib/docker/storage-driver/)
However, we can create volumes and persist our data. How ?
The following command to create a new volume called my-first-volume:
vagrant@ubuntu-xenial:~$ docker volume create my-first-volume
my-first-volume
vagrant@ubuntu-xenial:~$ docker volume ls
DRIVER VOLUME NAME
local my-first-volume
vagrant@ubuntu-xenial:~$ docker volume inspect my-first-volume
[
{
"CreatedAt": "2019-09-28T09:47:37Z",
"Driver": "local", ---> By default, Docker creates new volumes with local driver, they are only available on the node they were created
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my-first-volume/_data",
"Name": "my-first-volume",
"Options": {},
"Scope": "local"
}
]
Now, we are going to attach this volume to a container and create a file (test-volume.txt) under path /data:
vagrant@ubuntu-xenial:~$ docker container run -d -it --name test-volume-alpine --mount source=my-first-volume,target=/data alpine
b7bf95e4a4c2c44a77b1cb6cf083a9958b2b91aff4c826dc71d2d48efe98189d
vagrant@ubuntu-xenial:~$ docker container exec -ti test-volume-alpine /bin/sh
/ # ls
bin data dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # cd data/
/data # ls
/data # echo "Hello World" > test-volume.txt
We leave of our Alpine container (Ctrl-PQ) and verify if test-volume.txt was created under /var/lib/docker/volumes/my-first-volume/_data:
vagrant@ubuntu-xenial:~$ sudo -i
root@ubuntu-xenial:~# cd /var/lib/docker/volumes/my-first-volume/_data
root@ubuntu-xenial:/var/lib/docker/volumes/my-first-volume/_data# ls
test-volume.txt
root@ubuntu-xenial:/var/lib/docker/volumes/my-first-volume/_data# cat test-volume.txt
Hello World
root@ubuntu-xenial:/var/lib/docker/volumes/my-first-volume/_data# exit
logout
On the other hand, a volume can be shared, for instance, we are going to run another container in order to access to same volume:
vagrant@ubuntu-xenial:~$ docker container run -d -it --name test-volume-alpine2 --mount source=my-first-volume,target=/data alpine
5d31ea3a636e2858c39cd40fa52cb27b859c60c425ac6b6abfa968f1a90a9962
vagrant@ubuntu-xenial:~$ docker container exec -ti test-volume-alpine /bin/sh
/ # cd data/
/data # ls
test-volume.txt
/data # cat test-volume.txt
Hello World
/data # read escape sequence
Do you want remove these volumes?, there are two commands to remove volumes:
- docker volume prune: Delete all volumes that are not mounted into a container (be careful).
- docker volume rm: Specify which volume you want to delete.
vagrant@ubuntu-xenial:~$ docker container stop test-volume-alpine
test-volume-alpine
vagrant@ubuntu-xenial:~$ docker container stop test-volume-alpine2
test-volume-alpine2
vagrant@ubuntu-xenial:~$ docker container rm test-volume-alpine
test-volume-alpine
vagrant@ubuntu-xenial:~$ docker container rm test-volume-alpine2
test-volume-alpine2
vagrant@ubuntu-xenial:~$ docker volume rm my-first-volume
my-first-volume
Finally, I am going to show a case that is often used with Docker when it comes to test.
First of all, we create a Nginx container and expose its port:
vagrant@ubuntu-xenial:~$ docker container run -d -p 8080:80 --name nginx nginx
vagrant@ubuntu-xenial:~$ curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Secondly, we are going to change Nginx home page:
docker container stop nginx
docker container rm nginx
We create a new index.html:
cat <<EOF >index.html
<!DOCTYPE html>
<html>
<head>
<title>Nginx home</title>
<style>
body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
EOF
We run Nginx container by using -v parameter where we specify to mount a file (index.html) under a particular path (/usr/share/nginx/html/index.html):
vagrant@ubuntu-xenial:~$ echo $PWD
/home/vagrant
vagrant@ubuntu-xenial:~$ ls
index.html
vagrant@ubuntu-xenial:~$ docker container run --name nginx -d -p 8080:80 -v $PWD/index.html:/usr/share/nginx/html/index.html nginx
6188219f7a74ca197daf775ca87be0c489f365eea54b96b04c7ca65275a40e38
vagrant@ubuntu-xenial:~$ curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Nginx home</title>
<style>
body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
Tidying up:
vagrant@ubuntu-xenial:~$ docker container stop nginx
nginx
vagrant@ubuntu-xenial:~$ docker container rm nginx
nginx
vagrant@ubuntu-xenial:~$ docker image rm $(docker image ls -qa)
Untagged: nginx:latest
Untagged: nginx@sha256:aeded0f2a861747f43a01cf1018cf9efe2bdd02afd57d2b11fcc7fcadc16ccd1
Deleted: sha256:f949e7d76d63befffc8eec2cbf8a6f509780f96fb3bacbdc24068d594a77f043
Deleted: sha256:301c5d89cad94a6a99703841b021cf7df4326d2f14715c52b4b27893b13e02c0
Deleted: sha256:c3172409dcf95530cce7aad6a4c16a476fec9c43ac38426e9487b43efd246357
Deleted: sha256:2db44bce66cde56fca25aeeb7d09dc924b748e3adfe58c9cc3eb2bd2f68a1b68
Untagged: alpine:latest
Untagged: alpine@sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Deleted: sha256:961769676411f082461f9ef46626dd7a2d1e2b2a38e6a44364bcbecf51e66dd4
Deleted: sha256:03901b4a2ea88eeaad62dbe59b072b28b6efa00491962b8741081c5df50c65e0
vagrant@ubuntu-xenial:~$ rm index.html