Docker - Volume Mount

> Virtualization - (Virtual Machine|Container) > Docker

1 - About

File - (File Store|Volume|Partition) in Docker.

A volume is one type of mount in docker.

Volumes are one of the way of persisting data between container execution.

They are file store on the host where you can persist data generated by and used by Docker containers.

The -v and --volume options of docker run are used to create volume but also to defined a bind mount (What you want when you develop locally).

Advertising

3 - Concept

3.1 - Volume maps to a directory on the host

When you use a volume, a new directory is created within Docker’s storage directory on the host machine at Docker Root Dir/volumes (Default to /mnt/sda1/var/lib/docker/volumes).

Docker manages that directory’s contents. See inspect

A given volume can be mounted into multiple containers simultaneously.

When no running container is using a volume, the volume is still available to Docker and is not removed automatically.

If the target path for the volume exists already on the container, the content is obscured. The obscured files are not removed or altered, but are not accessible while the bind mount or volume is mounted.

3.2 - Scope

Unlike a bind mount, you can create and manage volumes outside the scope of any container.

3.3 - Volume driver

Volumes also support the use of volume drivers, which allow you to store your data on remote hosts or cloud providers, among other possibilities

4 - Syntax

4.1 - Name

The name of the volume may be:

  • given by using the create command
  • random generated (ex: 0da3cd28ba748613f4e52b885a5dc2c82d83844c0adc1543a48ac5f30fa644d9) when you don't give a volume name when starting a container.
Advertising

4.2 - Syntax of the -v or -volume options of the run command

The value of the -v or -volume options of the run command consists of three fields, separated by colon characters (:).

  • the first field is the name of the volume. For anonymous volumes, the first field is omitted. For a bind mount, you may specify a path on the host.
  • the second field is the path where the file or directory are mounted in the container.
  • the third field is optional, and is a comma-separated list of options, such as ro or rw,z
docker run -d \
  -it \
  --name devtest \
  -v volumeName:/app \
  nginx:latest

5 - Example

  • Create a volume named testVolumeName
docker volume create testVolumeName
testVolumeName
  • Mount this volume into the container at the path /pathInContainer/ and start bash.
docker run -it --name ubuntuTestVolume -v testVolumeName:/pathInContainer/  ubuntu bash
  • Go to the volume path
cd /pathInContainer/
  • Create a file named myFileInVolume and exit the container
touch myFileInVolume
exit
docker volume inspect testVolumeName
  • In the output we see that the data are stored at /mnt/sda1/var/lib/docker/volumes/testVolumeName/_data where /mnt/sda1/var/lib/docker/ is the Docker Root Dir
[
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/mnt/sda1/var/lib/docker/volumes/testVolumeName/_data",
        "Name": "testVolumeName",
        "Options": {},
        "Scope": "local"
    }
]
  • Enter the host to verify
docker-machine ssh
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   <  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 17.09.1-ce, build HEAD : e7de9ae - Fri Dec  8 19:41:36 UTC 2017
Docker version 17.09.1-ce, build 19e2cf6
[email protected]:~$ 
  • list the files in the path (with sudo because they are owned by root)
sudo ls /mnt/sda1/var/lib/docker/volumes/testVolumeName/_data
  • The file that we have created is present.
myFileInVolume
Advertising

6 - Management

6.1 - Installation

Locally, don't forget to mount your local drive. See shared_drive

The Volume service seems to given through a plugin (info)

docker info | grep -i volume
Plugins:
 Volume: local

6.2 - Create

docker volume create [OPTIONS] [VOLUME]
-v ~/nginxlogs:/var/log/nginx
services:

  elasticsearch:
    volumes:
      - ./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro
      - ~/nginxlogs:/var/log/nginx

where:

  • ~ will be translated to the host of your machine (not from the docker host docker user). Ie on windows C:\Users\Name\ and you need to have /c/Users/Name available on the host.
  • . will be translated to your current directory.
Advertising

6.3 - Info

6.3.1 - List

volume ls - List volumes

docker volume ls

6.3.2 - Inspect

volume inspect - Display detailed information on one or more volumes

docker volume inspect

Example:

docker volume inspect nico
[
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/mnt/sda1/var/lib/docker/volumes/nico/_data",
        "Name": "nico",
        "Options": {},
        "Scope": "local"
    }
]

6.4 - Suppress

6.4.1 - Prune

volume prune Remove all unused volumes

docker volume prune
Advertising

6.4.2 - Remove

volume rm Remove one or more volumes

docker volume rm	

7 - Support

7.1 - not a directory

When starting a machine with a volume configuration, you may get the following errors:

ERROR: for dockerelk_elasticsearch_1  Cannot start service elasticsearch: invalid header field value "oci runtime error: container_linux.go:247: starting container process caused \"process_linux.go:359: container init caused 
\\\"rootfs_linux.go:53: mounting \\\\\\\"/d/tmp/docker-elk/elasticsearch/config/elasticsearch.yml\\\\\\\" 
to rootfs \\\\\\\"/mnt/sda1/var/lib/docker/aufs/mnt/d4788609346584bc1936c65df13db2c7c52b0a0e25acc3658fb63c97e0dfb5a7\\\\\\\" 
at \\\\\\\"/mnt/sda1/var/lib/docker/aufs/mnt/d4788609346584bc1936c65df13db2c7c52b0a0e25acc3658fb63c97e0dfb5a7/usr/share/elasticsearch/config/elasticsearch.yml\\\\\\\" caused \\\\\\\"not a directory\\\\\\\"\\\"\"\n"

The configuration was:

docker-compose.yml
services:

  elasticsearch:
    volumes:
      - ./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro

The documentation on the volume syntax stipule that the first part is the path on the host, relative to the compose file.

The error said that the path is /d/tmp/docker-elk/elasticsearch/config/elasticsearch.yml. This path does not exist on your host.

docker-machine ssh
sudo su - 
ls /c
ls: /c: No such file or directory

Solution: You need to mount your disk as shared drive to made them available. See shared_drive

Advertising

8 - Documentation / Reference