Docker volumes & Bind Mount

  By default all files created inside a container are stored on 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.
  • Choose the right type of mount

    No matter which type of mount you choose to use, the data looks the same from within the container. It is exposed as either a directory or an individual file in the container’s filesystem.

    An easy way to visualize the difference among volumes, bind mounts, and tmpfs mounts is to think about where the data lives on the Docker host.

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

    Docker Volume Example

    docker volume create demo-vol
    docker volume ls     //to list the volumes
    docker run -it --mount source=demo-vol,destination=/app -d ubuntu
    docker run -it --mount source=demo-vol,destination=/test --mount source=demo-vol1,destination=/test1 -d ubuntu

    Docker Bind Mount Example

    docker run -it -v /home/ubuntu/mount:/demo -d ubuntu

Comments

Popular posts from this blog

Terraform

Scrum Master Interview help - Bootcamp

Kubernetes