Table of Contents

Docker - Volume Mount

About

This page is about a volume (file store) 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).

Concept

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.

Scope

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

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

Syntax

Name

The name of the volume may be:

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 (:).

docker run -d \
  -it \
  --name devtest \
  -v volumeName:/app \
  nginx:latest

Example

docker volume create testVolumeName
testVolumeName

docker run -it --name ubuntuTestVolume -v testVolumeName:/pathInContainer/  ubuntu bash
cd /pathInContainer/
touch myFileInVolume
exit
docker volume inspect testVolumeName
[
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/mnt/sda1/var/lib/docker/volumes/testVolumeName/_data",
        "Name": "testVolumeName",
        "Options": {},
        "Scope": "local"
    }
]

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
docker@default:~$ 

sudo ls /mnt/sda1/var/lib/docker/volumes/testVolumeName/_data
myFileInVolume

Management

Installation

Locally, don't forget to mount your local drive. See Docker - Installation

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

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

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:

Info

List

volume ls - List volumes

docker volume ls

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"
    }
]

Suppress

Prune

volume prune Remove all unused volumes

docker volume prune

Remove

volume rm Remove one or more volumes

docker volume rm	

Support

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:

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 Docker - Installation

Documentation / Reference