找一台centos7.2安装docker
# yum install -y vim wget telnet lrzsz tree yum-utils device-mapper-persistent lvm2 bash-comple* # source /usr/share/bash-completion/bash_completion # rm -rf /etc/yum.repos.d/* # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # wget -O /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # yum makecache fast # wget https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-18.09.9-3.el7.x86_64.rpm # yum localinstall docker-ce-*
# systemctl start docker
查看存储引擎
[root@localhost ~]# docker info ... Server Version: 18.09.9 Storage Driver: devicemapper Pool Name: docker-8:3-33951780-pool Pool Blocksize: 65.54kB Base Device Size: 10.74GB Backing Filesystem: xfs Udev Sync Supported: true Data file: /dev/loop0 Metadata file: /dev/loop1 Data loop file: /var/lib/docker/devicemapper/devicemapper/data Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata Data Space Used: 11.8MB Data Space Total: 107.4GB ... WARNING: bridge-nf-call-iptables is disabled WARNING: bridge-nf-call-ip6tables is disabled WARNING: the devicemapper storage-driver is deprecated, and will be removed in a future release. WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
配置镜像加速
[root@localhost ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://05eotbde.mirror.aliyuncs.com"] }
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
拉取一个镜像并启动容器
[root@localhost ~]# docker pull nginx [root@localhost ~]# docker run -itd -p 80:80 --name test nginx eed6fd358800d695eb83e4b509dfe082351a005adf07c00e733eaadf730fbb43
修改docker存储引擎为overlay
修改或创建 /etc/docker/daemon.json,并添加
[root@localhost ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://05eotbde.mirror.aliyuncs.com"], "storage-driver": "overlay" }
或者修改unit文件
[root@localhost ~]# vim /lib/systemd/system/docker.service ... ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -s=overlay ExecReload=/bin/kill -s HUP $MAINPID ...
重启docker,这时候会有报错
[root@localhost ~]# systemctl daemon-reload [root@localhost ~]# systemctl restart docker Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
查看日志
Jul 10 23:57:57 localhost dockerd: Error starting daemon: error initializing graphdriver: overlay: the backing xfs filesystem is formatted without d_type support, which leads to incorrect behavior. Reformat the filesystem with ftype=1 to enable d_type support. Backing filesystems without d_type support are not supported.
添加一块200G的硬盘,重启使磁盘识别
[root@localhost ~]# fdisk -l /dev/sdb Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
摘掉挂载umount /var/lib/docker
注意:可能会出现umount: /var/lib/docker: target is busy. 关闭占用进程fuser -km /var/lib/docker(慎用fuser -km)。在psmisc包中yum install psmisc -y 或者systemctl stop docker再次进行卸载
创建文件系统为xfs,使用参数 -n ftype=1
[root@localhost ~]# mkfs.xfs -n ftype=1 /dev/sdb meta-data=/dev/sdb isize=256 agcount=4, agsize=13107200 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=52428800, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=25600, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
备份:需要使用docker save备份镜像
注意:如果你原先有不同存储驱动的层数据,更换存储驱动后将不可用,建议备份镜像并清除 /var/lib/docker 下所有数据。
备份镜像可以用 docker save 导出镜像,之后用 docker load 导入镜像。
挂载磁盘,启动docker
[root@localhost ~]# mount /dev/sdb /var/lib/docker [root@localhost ~]# systemctl start docker
查看docker存储引擎
[root@localhost ~]# docker info ... Server Version: 18.09.9 Storage Driver: overlay Backing Filesystem: xfs Supports d_type: 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 ...
查看之前拉取的nginx镜像和容器是否存在
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
下面演示恢复数据
# docker run -itd --name test -p 80:80 nginx # docker commit test test1 sha256:5cbae254377047bcb127a7bd8e42c30022513ec0cbbaeabec580b486a38fdc1e # docker images REPOSITORY TAG IMAGE ID CREATED SIZE test1 latest 5cbae2543770 7 seconds ago 133MB nginx latest 4cdc5dd7eaad 4 days ago 133MB # docker save nginx >nginx.tar.gz # docker save test1 >test1.tar.gz
修改存储引擎,重启docker
# vim /etc/docker/daemon.json { "registry-mirrors": ["https://05eotbde.mirror.aliyuncs.com"], "storage-driver": "overlay" } # systemctl daemon-reload # systemctl restart docker
恢复数据
# docker load -i nginx.tar.gz ace9ed9bcfaf: Loading layer [==================================================>] 64.86MB/64.86MB 48b4a40de359: Loading layer [==================================================>] 3.072kB/3.072kB c553c6ba5f13: Loading layer [==================================================>] 4.096kB/4.096kB d97733c0a3b6: Loading layer [==================================================>] 3.584kB/3.584kB 9d1af766c818: Loading layer [==================================================>] 7.168kB/7.168kB Loaded image: nginx:latest # docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 4cdc5dd7eaad 4 days ago 133MB redis latest 08502081bff6 2 weeks ago 105MB
另外:如果是存储引擎修改回来,那么改回来就可以使用原来的镜像了(下面只演示了overlay改为overlay2)
# vim /etc/docker/daemon.json { "registry-mirrors": ["https://05eotbde.mirror.aliyuncs.com"], "storage-driver": "overlay2" } # systemctl daemon-reload # systemctl restart docker
查看镜像
[root@docker04 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test1 latest 5cbae2543770 18 minutes ago 133MB nginx latest 4cdc5dd7eaad 4 days ago 133MB