When working with Docker containers, it is important to consider how data is stored and accessed. Data inside a container is ephemeral, which means that it is not persisted after the container is stopped or removed. This can be a problem if you need to store data outside of the container, for example in a database.
There are two main ways to store data outside of a container:
In this article, we will focus on bind mounts. We will cover the following topics:
A bind mount is a type of mount where a file or directory from the host file system is mounted into a container. The file or directory is then accessible from inside the container.
Bind mounts are useful for a number of reasons:
Bind mounts can be used in two ways:
docker run
command to mount a file or directory from the host file system into the container. For example, the following command will mount the /data
directory from the host file system into the /data
directory in the container:docker run -v /data:/data my-image
docker volume
command to create a named volume and then mount it into the container. Named volumes are useful if you want to share data between containers or if you want to persist data outside of the container. For example, the following command will create a named volume called my-volume
and mount it into the /data
directory in the container:docker volume create my-volume
docker run -v my-volume:/data my-image
Bind mounts have a number of benefits:
docker run
command to mount a file or directory from the host file system into the container.Bind mounts have a few drawbacks: