Tags

Summary

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.

Writable layer

  1. 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.
  2. Writing into a container’s writable layer requires a storage driver to manage the filesystem. The storage driver provides a union filesystem, using the Linux kernel. This extra abstraction reduces performance as compared to using data volumes, which write directly to the host filesystem.

Docker has two options for containers to store files in the host machine, so that the files are persisted even after the container stops:

  • 1 . volumes

    Volumes: 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.

    `docker volume prune` to remove volume

    1. bind mounts.

    Available since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its full path on the host machine. The file or directory does not need to exist on the Docker host already.

Other options

If you’re running Docker on Linux you can also use a tmpfs mount. If you’re running Docker on Windows you can also use a named pipe.