docker 应用数据的管理

容器数据存储的三种方式

docker volume docker管理素质及文件系统的一部分,保存数据最佳方式

bind mounts   将宿主机的文件映射到容器里

tmpfs   存储在宿主机的内存里

查看docker 的默认存储驱动

Containers: 9
 Running: 5
 Paused: 0
 Stopped: 4
Images: 3
Server Version: 18.09.2
Storage Driver: overlay2   默认存储驱动
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce
runc version: 09c8266bf2fcf9519a651b04ae54c967b9ab86ec
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-862.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.622GiB
Name: localhost.localdomain
ID: BEO4:3VTY:XQ4A:UCJE:QLQL:OGXD:VOBQ:WMUY:V5PL:LTL5:3KPH:7YIX
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
 127.0.0.0/8
Registry Mirrors:
 http://f1361db2.m.daocloud.io/
Live Restore Enabled: false
Product License: Community Engine

容器数据卷的管理

查看当前系统数据卷

[root@localhost ~]# docker volume ls
DRIVER              VOLUME NAME

创建数据卷

[root@localhost ~]# docker volume ls
DRIVER              VOLUME NAME
local               chenxi_vol

查看数据卷的详细信息

[root@localhost ~]# docker volume inspect chenxi_vol
[
    {
        "CreatedAt": "2019-02-22T14:19:31+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/chenxi_vol/_data",   实际宿主机的目录
        "Name": "chenxi_vol",
        "Options": {},
        "Scope": "local"
    }
]

  

删除已存在的所有容器

[root@localhost ~]# docker rm -f $(docker ps -a | awk ‘{print $1}‘ )
7638633dc06a
0771848dd42e
25aff37e7dd0
ab4b909c4e68
a3bdc2062cd7
d2394de6c3a1
53dfa07e6f2c
4279c14287c1
dc2a611dae83
Error: No such container: CONTAINER

  启动容器并挂载数据卷

[root@localhost ~]# docker run -d --name web-1 -p 80:80 --mount
 src=chenxi_vol,dst=/usr/share/nginx/html nginxc074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22
[root@localhost ~]# docker inspect web-1
[
    {
        "Id": "c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22",
        "Created": "2019-02-22T06:35:20.789320917Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 22721,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-02-22T06:35:22.374476291Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:f09fe80eb0e75e97b04b9dfb065ac3fda37a8fac0161f42fca1e6fe4d0977c80",
        "ResolvConfPath": "/var/lib/docker/containers/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22/hostname",
        "HostsPath": "/var/lib/docker/containers/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22/hosts",
        "LogPath": "/var/lib/docker/containers/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22/c074c3141663aa7b52b45af4d15fc8a4a13d94b334b70c99fd2f7a5d6c065d22-json.log",
        "Name": "/web-1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "Mounts": [
                {
                    "Type": "volume",
                    "Source": "chenxi_vol",  挂载数据卷
                    "Target": "/usr/share/nginx/html"
                }
            ],
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/a0e351c43971e9ccab264bf25a49506959a354b635b2351a9afaedd50d78c135-init/diff:/var/lib/docker/overlay2/1491a86a89f7d4721b73639455d75fb153bc1
2c6cdaee687039d2e9317215741/diff:/var/lib/docker/overlay2/808c5a465c27497cb57ea709b083e855b6e11e2efa70887b1e3d6f2ece944ab3/diff:/var/lib/docker/overlay2/97f2bebcc1413f092415377b6d7e31dec473bd074f23afcde2d2394c65feed65/diff",                "MergedDir": "/var/lib/docker/overlay2/a0e351c43971e9ccab264bf25a49506959a354b635b2351a9afaedd50d78c135/merged",
                "UpperDir": "/var/lib/docker/overlay2/a0e351c43971e9ccab264bf25a49506959a354b635b2351a9afaedd50d78c135/diff",
                "WorkDir": "/var/lib/docker/overlay2/a0e351c43971e9ccab264bf25a49506959a354b635b2351a9afaedd50d78c135/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "chenxi_vol",
                "Source": "/var/lib/docker/volumes/chenxi_vol/_data",
                "Destination": "/usr/share/nginx/html",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "c074c3141663",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.15.8-1~stretch",
                "NJS_VERSION=1.15.8.0.2.7-1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "nginx",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "0439f377a7e2778a55457b8d48615adf0bba1fbe425bb72e03f4d5b95d31f0c5",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/0439f377a7e2",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "79bffed969d27453e7cc9e0bd1e42f59e2279374423673731221973ade7c5e6d",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "3b2c8016eb022a0e3a9896ff22b7aee66d3034fef8b301b91dc791ff184eef13",
                    "EndpointID": "79bffed969d27453e7cc9e0bd1e42f59e2279374423673731221973ade7c5e6d",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

[root@localhost ~]# ls /var/lib/docker/volumes/chenxi_vol/_data
50x.html index.html

  前端访问

修改网页内容

[root@localhost ~]# echo "haha" >  /var/lib/docker/volumes/chenxi_vol/_data/index.html

  

删除容器

[root@localhost ~]# docker rm -f web-2
web-2

  

重新再起一个挂载相同数据卷

[root@localhost ~]# docker run -d --name web-3 -p 80:80 --mount src=chenxi_vol,dst=/usr/share/nginx/html nginx
0a82046ce01576d2832c236c6d7cfe548ab09954a8757b2b8f32615712fdd078
[root@localhost ~]# ss -lntp
State       Recv-Q Send-Q                                                  Local Address:Port                                                                 Peer Address:Port
LISTEN      0      128                                                                 *:22                                                                              *:*
users:(("sshd",pid=921,fd=3))LISTEN      0      100                                                         127.0.0.1:25                                                                              *:*
users:(("master",pid=1310,fd=13))LISTEN      0      128                                                                :::80                                                                             :::*
users:(("docker-proxy",pid=23126,fd=4))LISTEN      0      128                                                                :::22                                                                             :::*
users:(("sshd",pid=921,fd=4))LISTEN      0      100                                                               ::1:25                                                                             :::*

  

创建容器使用另一种方式挂载数据目录

[root@localhost ~]# docker run -d --name web-4 -p 84:80 -v chenxi_vol:/usr/share/nginx/html nginx
d3c1a6e2d46ed8867f08a1eb9a7f4438bdfa1960af148e066be409e8c6072c44
[root@localhost ~]# ss -lntp
State       Recv-Q Send-Q                                                  Local Address:Port                                                                 Peer Address:Port
LISTEN      0      128                                                                 *:22                                                                              *:*
users:(("sshd",pid=921,fd=3))LISTEN      0      100                                                         127.0.0.1:25                                                                              *:*
users:(("master",pid=1310,fd=13))LISTEN      0      128                                                                :::80                                                                             :::*
users:(("docker-proxy",pid=23126,fd=4))LISTEN      0      128                                                                :::84                                                                             :::*
users:(("docker-proxy",pid=23221,fd=4))LISTEN      0      128                                                                :::22                                                                             :::*
users:(("sshd",pid=921,fd=4))LISTEN      0      100                                                               ::1:25                                                                             :::*

  

原文地址:https://www.cnblogs.com/rdchenxi/p/10418465.html

时间: 2024-11-09 12:01:43

docker 应用数据的管理的相关文章

docker 应用数据的管理之bind mounts

创建容器使用bind mounts 挂载文件系统.宿主机文件系统会覆盖掉容器里初始数据 [root@localhost ~]# mkdir /www/htpm -pv mkdir: 已创建目录 "/www" mkdir: 已创建目录 "/www/htpm" [root@localhost ~]# docker run -d --name web-7 -p 82:80 --mount type=bind,src=/www/htpm,dst=/usr/share/ngi

docker的数据卷管理

mkdir  -p  /data/volumes 挂载目录 [[email protected] ~]# docker run -d  --name nginx-volumes   -v  /data/volumes:/data  nginx  1263f51d5bd1114c7f1582a0efb68266ce367629f01a64693cd15cf115165392 本地的  /data/volumes   挂载到容器的/data  目录下 3.进入容器写文件测试 [[email prot

[docker]动态挂卷与数据卷管理

声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! 动态挂卷 目前docker不支持动态挂卷.有同事就提出,以下方法是否可行:1. 将块设备通过ln链接到容器与host的映射目录.2. 将块设备挂载到容器与host的映射目录. 以下是实验过程:由于非特权无法看到/dev下的设备,因此ln链接实际是无效的. 同理,对于其他块设备,也是这样的.当然,我们可以配置privileged

Docker之七:Docker数据卷管理

容器数据持久化 1.数据卷:将本地磁盘mount进入容器-v /dest-v src:dst [ro]|[rw] 2.数据卷容器:将外部容器分享给容器--volumes-from数据卷容器专门用来挂载数据卷的容器,以供其他容器引用和使用 3.数据卷管理命令docker volume 一.数据卷:1.给容器内部映射一个外部数据卷 -v src:dest[[email protected] nginx]# docker run -d --name web1 -v /opt/nginx:/usr/sh

Docker学习第三天(Docker数据卷管理)

1.Docker数据卷管理 在Docker中,要想实现数据的持久化(所谓Docker的数据持久化即数据不随着Container的结束而结束),需要将数据从宿主机挂载到容器中.目前Docker提供了三种不同的方式将数据从宿主机挂载到容器中: 1.volumes:Docker管理宿主机文件系统的一部分(默认文件位置:/var/lib/docker/volumes) 常用 由上图可知我们到了 /var/lib/docker/volumes 目录下有的文件名很长这时匿名 二有的则是我们创建时制定的 2.

理解Docker容器的进程管理

摘要: Docker在进程管理上有一些特殊之处,如果不注意这些细节中的魔鬼就会带来一些隐患.另外Docker鼓励"一个容器一个进程(one process per container)"的方式.这种方式非常适合以单进程为主的微服务架构的应用.然而由于一些传统的应用是由若干紧耦合的多个进程构成的,这些进程难以 Docker在进程管理上有一些特殊之处,如果不注意这些细节中的魔鬼就会带来一些隐患.另外Docker鼓励"一个容器一个进程(one process per contain

docker解决数据存储问题的方案

如今docker在云计算领域发展的势头非常猛,各个公司不论大小都開始研究这个开源工具和技术.环绕docker的开源项目和创业公司也多如牛毛,就是一个简单管理container的web ui都有非常多开源项目.只是还是一个人说的好.docker必须要是集群才好玩.并且越大越好玩. 当然这是从玩技术的人眼中看待的问题,假设要真正用于生产还是有非常多问题须要解决,非常多方案须要设计,非常多容错须要处理. 今天看资料学习到了docker是如何解决容器里面数据存储的问题的方案,曾经做PAAS遇到过这样的问

Docker背后的容器管理——Libcontainer深度解析

Libcontainer 是Docker中用于容器管理的包,它基于Go语言实现,通过管理namespaces.cgroups.capabilities以及文件系统来进行容器控制.你可以使用Libcontainer创建容器,并对容器进行生命周期管理. 容器是一个可管理的执行环境,与主机系统共享内核,可与系统中的其他容器进行隔离. 在2013年Docker刚发布的时候,它是一款基于LXC的开源容器管理引擎.把LXC复杂的容器创建与使用方式简化为Docker自己的一套命令体系.随着Docker的不断发

Docker可视化web界面管理-Portainer部署记录

Docker图形页面管理工具基本常用的有三种: Docker UI,Shipyard,Portainer,之前分别介绍了Docker UI和Shipyard部署,下面简单介绍下Portainer部署.Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控制台操作.Swarm集群和服务等集中管理和操作.登录用户管理和控制等功能, 功能十分全面,基本能满足中小型单位对容器管理的全部需