Docker named volumes Vs DOC (data-only-containers)


Docker offers several options for storing data in containers. Two popular choices are named volumes and DOCs (data-only containers). In this article, we'll compare these two options and discuss when it might be more appropriate to use one over the other.

What is Docker named volumes?

Docker-named volumes are a way to persist data in Docker containers. They allow you to create a named volume and mount it to a container so that the data stored in the volume is preserved even if the container is stopped or removed.

To create a named volume in Docker, you can use the docker volume create command. For example −

$ docker volume create my-volume
my-volume

To mount a named volume to a container, you can use the -v flag with the docker run command. For example −

$ docker run -d -v my-volume:/app/data busybox

In this example, the named volume my-volume is mounted to the /app/data directory in the busybox container. Any data stored in the /app/data directory will be persisted in the named volume, even if the container is stopped or removed.

Named volumes have a few advantages −

  • They are easy to use and don't require any extra configuration

  • They are portable, meaning they can be used with any container on the same host

  • They can be shared between multiple containers

However, there are also some drawbacks to using named volumes −

  • They can only be used on the host where they are created

  • They can be difficult to manage and maintain, especially if you have a large number of volumes

What is Docker DOCs (data-only containers)?

DOCs (data-only containers) are a way to store data in Docker containers using a different approach. Instead of using a named volume, you create a container specifically for storing data and then mount the data from that container to other containers as needed.

To create a DOC in Docker, you can use the docker create command. For example −

$ docker create -v /app/data --name data-container busybox /bin/true

In this example, a new container named data-container is created using the busybox image. The -v flag is used to create a volume at /app/data in the container, and the /bin/true command is used to keep the container running.

To mount the data from the DOC to another container, you can use the --volumes-from flag with the docker run command. For example −

$ docker run -itd --volumes-from data-container busybox

In this example, the data from the data-container DOC is mounted to the busybox container. Any data stored in the /app/data directory in the data-container will be available to the busybox container.

DOCs have a few advantages −

  • They allow you to separate the data from the container, making it easier to manage and maintain

  • They can be used on any host as long as the DOC is present

  • They can be used to share data between multiple containers

However, there are also some drawbacks to using DOCs −

  • They require an extra step to create and configure the DOC

  • They are not as portable as named volumes, as they need to be present on each host where they are used

  • They are not as easy to use as named volumes, as they require an extra step to mount the data to other containers

Implementation

Here is the implementation of creating data-only containers.

Step 1 − Create a container to use as volume.

$ docker create -v /DOC --name data-container3 busybox /bin/true

Step 2 − Run a container attaching data-container3

$ docker run -itd --volumes-from data-container3 --name test1 busybox

Go inside and check if the volume /DOC is present −

$ $ docker exec -it test1 sh
/ # ls
DOC    bin    dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var

Hence DOC volume is present.

Comparison of named volumes and DOCs

So, which data storage option is better - named volumes or DOCs? The answer ultimately depends on your specific needs and use case. Here are some considerations to help you decide −

Feature

Named Volumes

DOCs (Data-Only-Containers)

Configuration

No extra configuration required

Requires creation and configuration of the separate data-only container

Portability

It can be used with any container on the same host

It can be used on any host as long as the DOC is present

Data management

Data is stored within the container and can be difficult to manage with a large number of volumes

Data is separated from the container, making it easier to manage

Sharing

Can be shared between multiple containers on the same host

It can be shared between multiple containers on any host as long as the DOC is present

Ease of use

Simple and easy to use

Requires an extra step to mount data to other containers

Preference

Whether Docker-named volumes or DOCs are preferred for a particular task will depend on the specific requirements and use case. Here are some things to consider when deciding which data storage option to use −

  • If you want a simple and easy-to-use data storage option, named volumes might be the better choice. They don't require any extra configuration and can be used with any container on the same host.

  • If you need a more flexible and portable data storage option, DOCs might be the way to go. They can be used on any host as long as the DOC is present, and they allow you to separate the data from the container for easier management.

  • If you have a large number of data volumes, named volumes might be more difficult to manage. In this case, DOCs might be a better option as they allow you to separate the data from the container and make it easier to manage.

  • If you need to share data between multiple containers, either named volumes or DOCs could work. Named volumes can be shared between any containers on the same host, while DOCs can be shared between any containers as long as the DOC is present.

Ultimately, the best data storage option for you will depend on your specific needs and use case. Consider your requirements and choose the option that works best for you.

Conclusion

In conclusion, both named volumes and DOCs are useful options for storing data in Docker containers. Which one is best for you will depend on your specific needs and use case. Named volumes are simple and easy to use, while DOCs are more flexible and allow for better data management. Consider your requirements and choose the option that works best for you.

Updated on: 16-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements