Docker - Volume Mount

Card Puncher Data Processing

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

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:

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

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

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

  • 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

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:

  • ~ 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.

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





Discover More
Docker Host Virtualbox
Docker - (Virtual) Host (or Machine or Server) - Docker Type

a machine where docker server run or a network An host (or machine) is: a virtual host that you can see running in your virtual machine provider (such as virtual box). is managed through the...
Docker Shared Drive New Credentials
Docker - Bind mount

bind mount is type of mount. It's one of the three way on how you can persist data from a container. See The file or directory is referenced by its full or relative path on the host machine. The...
Docker Daemon
Docker - Daemon - dockerd

The daemon is: a self-sufficient runtime for containers. a background service running on the host that manages building, running and distributing Docker containers. The daemon creates and manages...
Card Puncher Data Processing
Docker - Data Persistence

mount a file or directory of the host into a container. volume: mount a file or directory of the host machine into a container located at /var/lib/docker/volumes/ : mount a file or directory of the...
Card Puncher Data Processing
Docker - Docker Root Dir (Docker Data Storage Path)

The docker root dir is the root path where all data docker is stored. Log into the host And select it where:
Card Puncher Data Processing
Docker - Mount

in docker The type can be: bind, volume, or tmpfs tmpfs and looks at the section Mounts where: type can be : or
Card Puncher Data Processing
Docker - docker client

The CLI uses the Docker REST API to control or interact with the Docker daemon where subcommand is: attach - Attach to a running container build - Build an image from a Dockerfile commit -...
Jenkins Unlock
Jenkins - Docker Installation

Jenkins installation on Docker Create an container and run it from the Community edition image /var/run/docker.sock on Windows mounting JENKINS_HOME to a local directory (in our example to...
Undraw File Manager Re Ms29
What is an Object Storage File System? (known also as edge storage)

Object storage (or object edge storage) is: a file system for blob file (binary object) (ie object) that are distributed/replicated at several CDN location (at the edge) In addition to the path,...



Share this page:
Follow us:
Task Runner