The easiest way to play with volumes is to use the Docker Toolbox as when directly using Docker for Mac or Docker for Windows, then the volumes are stored inside a (somewhat hidden) Linux VM that Docker for Mac/Win uses transparently.
Thus, we suggest the following:
$ docker-machine create --driver virtualbox volume-test
$ docker-machine ssh volume-test
And now that you're inside a Linux VM called volume-test, you can execute the following exercises:
- To create a named volume run the following command:
$ docker volume create my-products
- Execute the following command:
$ docker container run -it --rm \
-v my-products:/data:ro \
alpine /bin/sh
- To get the path on the host for the volume use, for example, this command:
$ docker volume inspect my-products | grep Mountpoint
Which (if using docker-machine and VirtualBox) should result in:
"Mountpoint": "/mnt/sda1/var/lib/docker/volumes/my-products/_data"
Now execute the following command:
$ sudo su
$ cd /mnt/sda1/var/lib/docker/volumes/my-products/_data
$ echo "Hello world" > sample.txt
$ exit
- Execute the following command:
$ docker run -it --rm -v my-products:/data:ro alpine /bin/sh
# / cd /data
# / cat sample.txt
In another terminal execute:
$ docker run -it --rm -v my-products:/app-data alpine /bin/sh
# / cd /app-data
# / echo "Hello other container" > hello.txt
# / exit
- Execute a command such as this:
$ docker container run -it --rm \
-v $HOME/my-project:/app/data \
alpine /bin/sh
- Exit both containers and then back on the host, execute this command:
$ docker volume prune
- Run the following command:
$ docker system info | grep Version
Which should output something similar to this:
Server Version: 17.09.1-ce
Kernel Version: 4.4.104-boot2docker
If you have been using docker-machine to create and use a Linux VM in VirtualBox, don't forget to clean up after you're done:
$ docker-machine rm volume-test