Docker Volume (Managed Data in Dockers)
By default all the files created inside a container are stored in a writable container layer. This means that:
- The data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it.
- A container’s writable layer is tightly coupled to the host machine where the container is running. You can’t easily move the data somewhere else.
- Writing into a container’s writable layer requires a storage driver to manage the file system. The storage driver provides a union file system, using the Linux kernel. This extra abstraction reduces performance as compared to using data volumes, which write directly to the host file system.
Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops: volumes, and bind mounts. If you’re running Docker on Linux you can also use a tmpfs mount.
- Volumes are stored in a part of the host file system which is managed by Docker (/var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this part of the file system. Volumes are the best way to persist data in Docker.
- Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.
- tmpfs mounts are stored in the host system’s memory only, and are never written to the host system’s file system.
· Created and managed by Docker. You can create a volume explicitly using the
docker volume create command, or Docker can create a volume during container or service creation.
· When you create a volume, it is stored within a directory on the Docker host. When you mount the volume into a container, this directory is what is mounted into the container. This is similar to the way that bind mounts work, except that volumes are managed by Docker and are isolated from the core functionality of the host machine.
· 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.
You can remove unused volumes using
docker volume prune.
Docker Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
In Docker when we Create Container, Container need some place where container store data, When we do not provide any specific location, it get store in the container and when we delete the container it delete that data.
When we work in Enterprise level, Data should not be deleted or required to create more container with old data and share data between containers.
Uses of Docker Volume
1. To keep data around when a container is removed
2. To share data between the hosts file system and the Docker container
3. To share data with other Docker containers
Docker Volume commands
To create a volume
docker volume create <vol_name>
Display detailed information on one or more volumes
docker volume inspect <vol_name>
docker volume ls
Remove all unused local volumes
docker volume prune
Remove one or more volumes
docker volume rm
Lab: Create a Volume
docker volume create myVOl1
Case-1: Attach the volume to the container while creation.
docker run — name=myJenkins1 -v myVol1:/var/jenkins_home -p 8181:8080 -p 50001:50000 jenkins
Explanation of the commands
Giving name to the container
myVol1 — volume of my base machine that is attaching to the /var/jenkins_home (Volume of container)
8181 is the port of docker host VM for port mapping
This port used for API configuration
Case -2: Attach the same volume to the container no. 2
docker run — name=myJenkins2 -v myVol1:/var/jenkins_home -p 8282:8080 -p 50002:50000 jenkins
Case -3: Attach any specific folder to the container no. 3
docker run — name=myJenkins3 -v /usr/local/:/var/jenkins_home -p 8383:8080 -p 50003:50000 jenkins
Case -4: We can use any specific Container and a storage volume container and can share that volume with any of Docker container
These type of containers are called “Data Only Containers”
· Create a data only container. As we don’t have to do any operations inside this container, we used “/bin/true”. It will stop the container
docker run -it — name=data-only -v /DATA centos:latest /bin/true
· We already stopped this container so it will show in
docker ps –a
· Create one more container and mount the volume from base
Container using “ — volumes-from” flag.
docker run -it — volumes-from data-only centos /bin/bash