How to deal with persistent storage (e.g. databases) in Docker?

DockerOperating SystemOpen Source

You can use Docker volumes to achieve a solution for persistent storage in Docker. There are bind mounts as well but the problem with them is that they are highly dependent on the underlying host as well as the directory structure. The volumes are completely managed and controlled by Docker. With a bond mount, we mount a file or a directory of the host system to a container. We can reference the mounted directory by it’s absolute path.

However, when we use a volume, we create a new directory inside the storage directory of Docker on the host machine which is managed by Docker. Volumes are always a better choice as compared to persisting file or data on the writable layer of the container. This is so because using a volume doeHow to deal with persistent storage (e.g. databases) in Docker?s not increase the size of the container. Moreover, the contents of the volumes also exist outside the container’s lifecycle.

Before we begin with volumes, it’s necessary that you understand the benefits of using volume over other methods of persistent storage such as bind mounts.

  • Easier to migrate or back up.
  • Easier to manage using simple APIs or CLI.
  • They work on both Windows and Linux.
  • Safe and easy to share.
  • Drivers let you store volumes on cloud or remote server, encryption of content, etc.
  • You can pre-populate volumes with content from other containers.

Now, let’s see how to mount volumes. You can use two different flags called -v and --mount to mount volumes on a container.

The --mount option consists of key-value tuples separated by commas. It’s syntax is more verbose and the order of the keys is insignificant. If you want to specify the driver options, it’s mandatory that you use the --mount option.

The -v flag has three fields separated by : and they have to be in correct order. Let’s see how to work with volumes using different commands.

To create a volume, you can use -

$ docker volume create my-vol

This command will create a volume called my-vol in the Docker’s storage directory in your host machine.

If you want to list all the volumes, you can use -

$ docker volumes ls

To inspect a Docker volume, use -

$ docker volume inspect my-vol

This will list all the details such as date of creation, path, associated containers, etc.

Finally, to completely remove Docker volumes, you can use -

$ docker volume rm my-vol

Now that you have created a volume, the next step is to mount the volume to a container. Please note that if you mount a volume that does not exist, Docker will automatically create it for you.

$ docker run -it --p 8080:80 --mount source=my-vol,target=/myapp nginx:latest

The above command will create a container associated with the Nginx image, publish the port 8080 to the port 80 in the host. We have used the --mount option to specify key-value pairs to define the source name of the volume and the target destination where the volume needs to be mounted in the container.

Another way to mount a volume is by using the -v or --volume option. Consider the command below.

$ docker run -it -v /source/path/location:/destinal/container-path debian

You can also easily share the volumes with multiple other containers as well. This will allow you to share data and files with multiple other containers. You can do so using the --volumes-from option. The files that you have stored inside the shared volume will be accessible by all the containers and they can manipulate it. The changes will reflect on other containers as well.

To sum up, in this article, we discussed how to deal with the problem of persistent data storage in Docker. When you exit a Docker container, all the data stored in it is lost. Hence, you can use volumes or bind mounts to solve this problem.

raja
Published on 06-Aug-2021 11:55:05
Advertisements