Storing and Sharing with Docker Volumes

28/12/2020
In Docker, you can write data on the writable layer. But the data doesn’t persist after a container shuts down. Also, it’s not easy to move the data from one container to another. Naturally, data storing and sharing is sometimes necessary between services.

Docker has three types of data mounts that can help – volumes, bind mounts, and tmpfs. Volumes store data in the host’s filesystem but it is managed by Docker. Bind mounts help store data anywhere on the host system and users can directly modify the files from the host’s own processes. The tmpfs mounts are stored in the host’s memory only. Docker volumes are the best option because they are the safest to use.

How to Use Docker Volumes

Let’s try out a hands-on example. We are going to create a few Ubuntu containers that share the same volume.

First, we want to create the volume with the following command:

$ docker volume create my-common-vol

Now we can check if the volume exists:

$ docker volume ls
 
DRIVER              VOLUME NAME
local               my-common-vol

We can further inspect the volume to check its properties:

$ docker volume inspect my-common-vol
 
[
{
  "CreatedAt": "2018-04-06T07:43:02Z",
  "Driver": "local",
  "Labels": {},
  "Mountpoint": "/var/lib/docker/volumes/my-common-vol/_data",
  "Name": "my-common-vol",
  "Options": {},
  "Scope": "local"
}
]

It’s important to remember that Mountpoint is actually inside the VM that docker is running on. So, it’s not directly accessible.

Now let’s start our first server with my-common-vol.

(Note for the docker run command, you can use the –mount and –v options to mount a volume. The syntax of the two is different. We will use the latest –mount option as it is the latest.)

$ docker run –name server1 –mount source=my-common-vol,target=/app -it ubuntu

We are mounting my-common-vol to /app folder on the server1 docker container. The above command should log you into the ubuntu server1. From the command line go to the /app folder and create a file:

root@1312ea074055:/# cd /app
root@1312ea074055:/app# ls
root@1312ea074055:/app# touch created-on-server1.txt
root@1312ea074055:/app# ls
created-on-server1.txt

So we have the file created-on-server1.txt in the /app folder.

Let’s go create a second server and mount the same my-common-vol volume to it:

$ docker run –name server2 –mount source=my-common-vol,target=/src -it ubuntu

Now we can go to the /src folder in server2, check for server1 files and create a new file:

root@77cd51945461:/# cd /src
root@77cd51945461:/src# ls
created-on-server1.txt
root@77cd51945461:/src# touch created-on-server2.txt
root@77cd51945461:/src# ls -1
created-on-server1.txt
created-on-server2.txt

In the /src folder, we see that created-on-server1.txt already exists. We add created-on-server2.txt. We can check back on server1 and see that created-on-server2.txt shows up.

Let’s start a new container server3 that will only have read-only access to the volume my-common-vol:

$ docker run –name server3 –mount source=my-common-vol,target=/test,readonly -it ubuntu

So we have created server3 with my-common-vol mounted to /test.

Let’s try to write something in /test:

root@a6620da1eea1:/# cd test
root@a6620da1eea1:/test# ls -1
created-on-server1.txt
created-on-server2.txt
root@a6620da1eea1:/test# touch created-on-server3.txt
touch: cannot touch ‘created-on-server3.txt’: Read-only file system

You can see that we can’t write to my-common-vol from server3.

You can delete volumes. But you have to remove all the associated containers before you can attempt. Otherwise, you’ll get an error like this:

$ docker volume rm my-common-vol
 
Error response from daemon: unable to remove volume: remove my-common-vol:
volume is in use – [1312ea07405528bc65736f56692c06f04280779fd283a81f59f8477f28ae35ba,
77cd51945461fa03f572ea6830a98a16ece47b4f840c2edfc2955c7c9a6d69d2,
a6620da1eea1a39d64f3acdf82b6d70309ee2f8d1f2c6b5d9c98252d5792ea59]

In our case, we can remove the containers and the volume like this:

$ docker container rm server1
 
$ docker container rm server2
 
$ docker container rm server3
 
$ docker volume rm my-common-vol

Also, if you want to mount multiple volumes, the “docker run” command’s –mount option allows that too.

Further Study:

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Networking and Storage for Docker Containers

Networking and Storage for Docker Containers The first thing people look for after running Apache in a container is how...
28/12/2020

How to Remove Docker Containers

In this article, I am going to show you how to remove Docker containers. So, let’s get started. Requirements: I assume...
29/12/2020

Install Docker CE on CentOS 8

Docker CE is officially not supported on Red Hat Enterprise Linux (RHEL) 8 or CentOS 8. The Red Hat’s officially recommended...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024