Overview of Storage in Docker Containers


In a Docker container, the storage is provided to each process as a virtualized block device. All containers share this same storage resource from their host machine.

The benefit of using docker volumes for data persistence between restarts of your application can be seen when you have multiple applications on one machine that all use different directories on the shared filesystems.

When you stop and remove the container, all data that was stored in those directories will be lost. However, with docker volumes, your data is persistent even after removing a container.

Docker containers are ephemeral by nature, the life cycle of an application is tied to the running container.

Volumes allow you to create persistent storage for Docker containers on top of your docker host file system. | This allows you to have a greater degree of freedom in how applications can be used and deployed across different environments without sacrificing portability.

A volume is a specially-designated directory within one or more containers that bypasses the Union File System to provide persistent or shared storage for Docker applications. Volumes are designed to persist data, independent of the container’s life cycle.

You can create three types of volumes: Bind, Volume and TMPFS however the most recommended method to use is Volume.

Bind

Bind mount volumes are created by making a portion of another filesystem on the host machine available within your container. This is done using the -v flag when starting out the docker run command and specifying where to bind in your file system, what path inside that volume to map it to in the container’s environment and optionally which commands to run after the bind mount is mounted.

The bind mount is mapped to the host machine file system as a path which can be an issue because the folder path needs to exist on the Docker host before mounting the volume. Another issue is that Docker CLI cannot manage Bind volumes.

Volume

Named volumes are created by specifying a path on your docker host that will be made available inside of one or more containers. This can either be done using the -v flag when starting out the docker run command and specifying where to make it available in your file system, what path within that volume to map it to in the container’s environment and optionally which commands to run after it is mounted.

In an anonymous volume, a randomly named data volume will be generated for you that gets destroyed when your container goes away. This can either be done using the -v flag when starting out the docker run command and specifying where to make it available in your file system, what path within that volume to map it to in the container’s environment and optionally which commands to run after it is mounted.

Names volumes are the recommended method to use storage with Docker. The volumes don’t need to exist on the host before using them. Volumes are managed by Docker inside the Docker storage directory.

TMPFS

Docker tmpfs is a volume type that provides temporary storage for Docker applications. Docker tmpfs volumes are created automatically when you use the -v flag with your docker run command and specify a directory as part of the file system that already exists on your docker host.

TMPFS volumes get deleted when the container is removed. The only use case for using tmpfs is when you need the container to store sensitive information for a short time and delete it when the container is removed. tempfs volumes are only supported on Linux hosts.

Conclusion

when planning to use volumes inside your Docker deployment you have 3 options to choose from however if you look closely at each option the recommended best practices you really have one good option and it is the use the volume type option.

When creating a volume it is always a good idea to create the volume first using the following command before attaching it to a container. If you don’t create the volume first Docker will create one using a random name which can be hard to troubleshooting later on.


Posted

in

,

by