学无先后,达者为师

网站首页 编程语言 正文

Docker可视化、数据持久化

作者:吉松松 更新时间: 2022-07-09 编程语言

可视化(portainer)

$ docker run -d -p 9000:9000 -p 8000:8000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
$ docker ps
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                                            NAMES
809ca9a10ef9        portainer/portainer   "/portainer"        14 hours ago        Up 4 seconds        0.0.0.0:8000->8000/tcp, 0.0.0.0:9000->9000/tcp   portainer

持久化

将容器内部数据挂载到宿主机上的目录上,实现数据的持久化。使用 docker run -v参数挂载

具名挂载、匿名挂载、指定路径挂载

#匿名挂载
-v 直接写容器路径
$ docker run --name nginx  -v /usr/share/nginx/html -d nginx
$ docker inspect nginx
..........
"Mounts": [
            {
                "Type": "volume",
                "Name": "adb2dfbbdf53cf16e80a0f9a8df0fed73ff7bbc8a37909e80462e4018c50bc9d",
                "Source": "/opt/docker/volumes/adb2dfbbdf53cf16e80a0f9a8df0fed73ff7bbc8a37909e80462e4018c50bc9d/_data",
                "Destination": "/usr/share/nginx/html",
......
容器中的/usr/share/nginx/html挂在了宿主机的
/opt/docker/volumes/adb2dfbbdf53cf16e80a0f9a8df0fed73ff7bbc8a37909e80462e4018c50bc9d/_data目录下。
默认是/var/lib/docker/volumes,为什么会变成/opt/docker?原因在于/etc/docker/daemon.json中指定了data-root参数
$ cat /etc/docker/daemon.json
{ "data-root":"/opt/docker" }


#具名挂载
容器中的/usr/share/nginx/html挂在了宿主机/opt/docker/volumes/html/_data目录下。
-v 卷名:容器内的路径
$ docker run --name nginx -v html:/usr/share/nginx/html -d nginx
#查看卷具体所在位置
$ docker inspect nginx 
....
 "Mounts": [
            {
                "Type": "volume",
                "Name": "html",
                "Source": "/opt/docker/volumes/html/_data",
                "Destination": "/usr/share/nginx/html",
                "Driver": "local",
....


#指定路径挂载
容器中的/usr/local/nginx/html挂在了宿主机指定的/opt/html目录下。
$ docker run --name nginx -v /opt/html:/usr/share/nginx/html  -d  nginx
$ docker inspect nginx
...
 "Mounts": [
             {
                "Type": "bind",
                "Source": "/opt/html",
                "Destination": "/usr/share/nginx/html",
                "Mode": "",
......

# --volumes-from
当有多个容器想要共享宿主机同一个目录时,可以使用参数--volumes-from
例如: 
宿主机的/opt/html目录挂载至容器的/usr/share/nginx/html,
$ docker run --name nginx -v /opt/html:/usr/share/nginx/html -d nginx
当有另外的容器同样需要挂载/opt/html目录时,有两种方法,1、-v参数 2、--volumes-from
$ docker run --name nginx2 --volumes-from nginx -d nginx

总结

相同点:
    1、匿名挂载、具名挂载、指定路径挂载都可以实现容器数据持久化
    2、匿名挂载、具名挂载、指定路径挂载都会在宿主机上产生某个相对应的文件夹
不同点:
    1、匿名挂载产生的文件夹是随机64位字符串
    2、具名挂载是将匿名挂载的随机64位字符串替换成指定文件夹名称
    3、匿名挂载、具名挂载是将容器中目录挂载至宿主机,因而挂载后容器的中的文件可在宿主机呈现,
       而指定路径挂载是将宿主机路径绑定给容器,会覆盖掉容器中原有目录或文件。

原文链接:https://blog.csdn.net/m0_37642477/article/details/125629475

栏目分类
最近更新