Mind~G
Docker

Volumes

Docker volumes are like external hard drives for your containers, allowing them to store data that persists even after the container is stopped or removed.

They are used to store data that you want to keep even if the container is deleted or recreated.

What are Docker Volumes?

When you run a container, it has its own filesystem that is temporary. If you delete the container, all the data inside it is lost. Docker volumes solve this problem by allowing you to store data outside of the container's filesystem.

Types of Volumes

Docker supports different types of volumes:

  • Named Volumes: These are managed by Docker and can be shared between containers. They are stored in a specific location on your host machine.
  • Anonymous Volumes: These are created without a name and are not easily accessible.
  • Bind Mounts: This allows you to share files between the your machine and the container.

Creating and Using Volumes

Using Existing Volume

Like you want to share your file in ubuntu conatiner with your host machine. You can use a bind mount to share a directory from your host machine with a container.

docker run -v -it /path/on/host:/path/in/container ubuntu

Now using conatiner you can perform any operation on the file and it will be reflected in the host machine.

Custom Volumes

You can create your own volumes to store data that you want to keep even if the container is deleted.

Benifits of using custom volume if the container is deleted, the data in the volume remains intact. You can also share this volume between multiple containers. To create a named volume, you can use the following command:

docker volume create my_volume

Then, you can use this volume when running a container:

docker run -v my_volume:/data my_image

Example Imagine you're building a web application that needs to store user data. You can create a named volume to keep this data safe even if you update or delete the container.

docker volume create user_data
docker run -v user_data:/app/data my_web_app

This way, the user data will be stored in the user_data volume, and it will persist even if you remove or recreate the container.

On this page