Docker Volumes


Docker offers three different ways to mount data into a container from the Docker host: volumesbind mounts, or tmpfs volumes. When in doubt, volumes are almost always the right choice



·         Volumes are stored in a part of the host filesystem which is managed by Docker(/var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this part of the filesystem. 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 filesystem

There are three main use cases for Docker data volumes:
1.      To keep data around when a container is removed
2.      To share data between the host filesystem and the Docker container
3.      To share data with other Docker containers

Volume Commands

Create a volume:
$ docker volume create my-vol

List volumes:
$ docker volume ls

Inspect a volume:
$ docker volume inspect my-vol

Remove a volume:
$ docker volume rm my-vol

start the container using volume
$ docker run -d -it --name=nginxtest -v nginx-vol:/usr/share/nginx/html nginx:latest

Clean container with volume
$ docker container stop nginxtest
$ docker container rm nginxtest
$ docker volume rm nginx-vol

For creating readonly containers
$ docker run -d -it --name=nginxtest -v nginx-vol:/usr/share/nginx/html:ro nginx:latest

Binding Mounts  (Sharing Data Between the Host and the Docker Container)


$ docker run -dit --name devtest -v "$(pwd)"/target:/app nginx:latest

$ docker run -d -v ~/nginxlogs:/var/log/nginx -p 5000:80 -i nginx:latest

-v ~/nginxlogs:/var/log/nginx — This will set up a volume that links the /var/log/nginx directory from inside the Nginx container to the ~/nginxlogs directory on the host machine. Docker uses a : to split the host's path from the container path, and the host path always comes first.


No comments:

Post a Comment