Docker Volume
本文最后更新于 703 天前,其中的信息可能已经有所发展或是发生改变。

what is docker volume

Docker Image可以理解成多个只读文件叠加而成,因此Docker Image是只读的。

当我们将其运行起来,就相当于在只读的Image外包裹了一层读写层变成了容器。

当你删除容器之后,使用这个镜像重新创建一个容器,此时的镜像的只读层还和原来的一样,但是你在读写层的修改全部都会丢失。

那么问题就来了,如果想要 持久化 在读写层的数据,该怎么利用docker做到呢?

docker使用volume实现数据的持久化,不仅如此volume还能帮助容器和容器之间,容器和host之间共享数据。

command

docker run .... -v /data
docker run -td --name testCon --volume testA:/data alpine
docker run -td --name testCon -v testA:/data alpine
or
docker volume create --name **
docker volume create testA
[root@cos7-1 ~]# docker run -td --name testDcon -v /data1 alpine

60afd20098ed4cb7631ee6eaff5bf4cc9fb5fe8e623c044d95075b637f5d6c9d

[root@cos7-1 ~]#

[root@cos7-1 ~]# docker volume ls

DRIVER VOLUME NAME

local 86bb1cfa654bbeeefc766131de443ebafbadbccb1e789c4e4cd160698c3bead3

local testA

local testB

list the volumes

docker volume ls

find volume's path

docker volume inspect [volume name]

if a volume already exist

docker run -td --name testAcon --volume testA:/data alpine
docker exec -it testAcon sh

delete volume

*删除container时并不会一并删除volume

#查看当前卷,有三个,两个命名卷,一个匿名卷,此处以删除匿名卷为例,命名卷操作也是相同的
[root@cos7-1 ~]# docker volume ls
DRIVER    VOLUME NAME
local     86bb1cfa654bbeeefc766131de443ebafbadbccb1e789c4e4cd160698c3bead3
local     testA
local     testB

#尝试删除匿名卷,出现提示,卷正在被容器60afd2...使用,这个ID就是testDcon容器的ID
[root@cos7-1 ~]# docker volume rm 86bb1cfa654bbeeefc766131de443ebafbadbccb1e789c4e4cd160698c3bead3
Error response from daemon: remove 86bb1cfa654bbeeefc766131de443ebafbadbccb1e789c4e4cd160698c3bead3: volume is in use - [60afd20098ed4cb7631ee6eaff5bf4cc9fb5fe8e623c044d95075b637f5d6c9d]

#先停止容器并删除容器
[root@cos7-1 ~]# docker stop testDcon
testDcon
[root@cos7-1 ~]# docker rm testDcon
testDcon

#再次删除卷,已经正常删除了
[root@cos7-1 ~]# docker volume rm 86bb1cfa654bbeeefc766131de443ebafbadbccb1e789c4e4cd160698c3bead3
86bb1cfa654bbeeefc766131de443ebafbadbccb1e789c4e4cd160698c3bead3
[root@cos7-1 ~]# 
[root@cos7-1 ~]# docker volume ls
DRIVER    VOLUME NAME
local     testA
local     testB

which container bind the volume?

#通过下例查看testCcon容器都使用了哪些卷
#如下命令其实就是过滤出容器的详细信息中的Mounts段
#如下示例的testCcon容器使用了两个卷,testA和testB
[root@cos7-1 ~]# docker inspect testCcon -f '{{.Mounts}}'
[{volume testB /var/lib/docker/volumes/testB/_data /var/log local z true } {volume testA /var/lib/docker/volumes/testA/_data /data local z true }]

#通过下例查看testA卷被哪些容器使用了
#如下示例中testA卷被testBcon和testCcon两个容器使用了
[root@cos7-1 ~]# docker ps -a -f "volume=testA" 
CONTAINER ID   IMAGE     COMMAND     CREATED        STATUS                      PORTS     NAMES
9b6d44174554   alpine    "/bin/sh"   21 hours ago   Exited (137) 13 hours ago             testBcon
d8d7470c5d86   alpine    "/bin/sh"   24 hours ago   Exited (137) 13 hours ago             testCcon

free up disk space

批量删除所有的没有被任何容器使用的卷

docker volume prune
docker system df
docker system events
docker system info
docker system prune
[root@localhost ~]# docker system prune
WARNING! This will remove:
        - all stopped containers # 清理停止的容器
        - all networks not used by at least one container #清理没有使用的网络
        - all dangling images #清理废弃的镜像
        - all build cache #清理构建缓存
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

更进一步,使用-a选项可以做深度清理。这时我们会看到更加严重的WARNING信息:

$ docker system prune -a
WARNING! This will remove:
        - all stopped containers
        - all volumes not used by at least one container
        - all networks not used by at least one container
        - all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: test:latest
deleted: sha256:c515ebfa2...
deleted: sha256:07302c011...
deleted: sha256:37c0c6474...
deleted: sha256:5cc2b6bc4...
deleted: sha256:b283b9c35...
deleted: sha256:8a8b9bd8b...
untagged: alpine:latest
untagged: alpine@sha256:58e1a1bb75db1...
deleted: sha256:4a415e366...
deleted: sha256:23b9c7b43...
Total reclaimed space: 2.151GB

references

docker prune命令 可定时清理不常用数据
docker数据卷
清理Docker的container,image与volume
docker volume

https://blog.0001024.xyz/docker-volume/
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇