centos下 Docker-修改磁盘存储目录(实操)
阅读原文时间:2023年07月08日阅读:1

预备知识:

Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目。它基于 Google 公司推出的 Go 语言实现。 项目后来加入了 Linux 基金会,遵从了 Apache 2.0 协议,Docker project 于 2013 年在 PyCon 上首次正式亮相,并逐步成长为社区项目,所以当时就注册了 dockerproject.org 和 dockerproject.com 的域名,并且后来在这两个域名之下托管了 APT 和 YUM 仓库 后期随着 Docker Inc. 的成立,为了更好的专注于 Docker 的产品 (CE 和 EE),所以就注册了 docker.com 的域名。并且正式将 APT 和 YUM 仓库托管到了 download.docker.com。 现在几乎所有人都已经在使用新的 download.docker.com 的仓库了 Docker 自开源后受到广泛的关注和讨论,以至于 dotCloud 公司后来都改名为 Docker Inc。Redhat 已经在其 RHEL6.5 中集中支持 Docker;Google 也在其 PaaS 产品中广泛应用。Docker 项目的目标是实现轻量级的操作系统虚拟化解决方案。 Docker 的基础是 Linux 容器(LXC)等技术。
2014年08月23,Docker 官方正式发布 Docker 1.2.0 版本,改进内容包括 Docker 引擎、Docker Hub 和文档。2017年6月2日,Linux 容器引擎Docker1.3 正式发布了,该版本会自动的使用数字签名验证所有官方仓库的出处和一致性2019 年 11 月 15 日 Docker v19.03.5 发布
Docker支持以下的CentOS版本:CentOS 7 (64-bit) 和 CentOS 6.5 (64-bit) 或更高的版本目前,CentOS 仅发行版本中的内核支持 Docker。Docker 运行在 CentOS 7 上,要求系统为64位、系统内核版本为 3.10 以上。Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位、系统内核版本为 2.6.32-431 或者更高版本。2017 年 3 月开始 docker 在原来的基础上分为两个分支版本: Docker CE 和 Docker EE。Docker CE 即社区免费版,Docker EE 即企业版,强调安全,但需付费使用。
Docker 1.13.1 版本之前,Docker 软件包的名称有两次变化:从 docker-io(http://docker.io) 到 docker-engine,再到 docker。
Docker 发展到 1.13.1 版本后,Docker 公司把 Docker 分成了两种形式:docker-ce 社区版,免费docker-ee 商业版,收费并且版本号的命名方式也改了,以前都是那种常用的版本号命名方式,比如0.1、0.2、1.0之类的,现在分社区版和商业版后,版本号是『YY.MM-xx』的形式命名的,比如2019年10月发布的,版本号就是19.10。
Docker 1.13.1 之后,直接是 Docker-ce 17.03.0 版本了。所想用Docker,一定要用最新的软件包,也就是 docker-ce,像 http://docker.io、docker-io、docker-engine、docker 这些都是旧版本,已经不适合使用了,特别是 http://docker.io 的这个版本时期,很多功能是没有的。

我们服务器上基本都是18.03.0-ce或18.06.1-ce版本

[root@fp-web-118 /]# docker version

Client: ###客户端
Version: 18.03.0-ceAPI version: 1.37Go version: go1.9.4Git commit: 0520e24Built: Wed Mar 21 23:09:15 2018OS/Arch: linux/amd64Experimental: falseOrchestrator: swarm
Server: ###服务器端Engine:Version: 18.03.0-ceAPI version: 1.37 (minimum version 1.12)Go version: go1.9.4Git commit: 0520e24Built: Wed Mar 21 23:13:03 2018OS/Arch: linux/amd64Experimental: false

清华的开源镜像网站:

https://mirror.tuna.tsinghua.edu.cn/help/docker-ce/

一、docker安装

  1. 查看内核版本:         \[root@fp-web-118 /\]#  uname -r                  \[root@fp-web-118 /\]# cat /etc/redhat-release          CentOS Linux release 7.2.1511 (Core)  

  2. 安装命令 [root@fp-web-118 /]# yum -y install docker-ce-18.03.0.ce-1.el7.centos
[root@fp-web-118 /]# systemctl start docker [root@fp-web-118 /]# systemctl stop docker docker 安装好后默认路径为 /var/lib/docker ,其下的containers文件夹为容器文件夹,image为镜像文件夹 安装之后要确保,你能正常的启动和停止docker,因为有的机器不一定能够正常的启动或者关闭docker.

二、挂载系统盘:

  1. 挂载新硬盘;

  2. 查看可使用的硬盘列表:fdisk -l

结果进行翻译如下:

##磁盘sda
磁盘 /dev/sda: 107.4 GB, 107374182400 字节, 209715200 个分区Units = 扇区 of 1 * 512 = 512 字节扇区大小 (逻辑/物理/): 512 字节/ 512 字节I/O 大小(最小/最佳): 512 字节/ 512 字节磁盘标签 类型: dos磁盘标识符: 0x0006d834Device Boot Start End Blocks Id System/dev/sda1 * 2048 1026047 512000 83 Linux ##带*代表启动分区/dev/sda2 1026048 209715199 104344576 8e Linux LVM##磁盘sdbDisk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 扇区
Units = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes

  3. 对新的数据盘进行分区:(n(新建分区)、p(显示分区信息)、l、回车、回车、wq)

fdisk操作说明    查看帮助信息:输入m,看到如下信息

  • m :显示菜单和帮助信息

  • a :活动分区标记/引导分区

  • d :删除分区 (删除已有分区)

  • l :显示分区类型

  • n :新建分区 (添加主分区)

  • p :显示现有分区信息

  • q :退出不保存

  • t :设置分区号

  • v :进行分区检查

  • w :保存并退出

  • x :扩展应用,高级功能
    具体每个参数的含义,请仔细阅读。常用的就是:d l m p q t w

    fdisk /dev/sdb (fdisk的使用,请看我其他文章)

      4. 当分区完成后,就可以找到对应的磁盘信息:fdisk -l
           

      5. 分区完,进行格式化:mkfs.ext3 /dev/sdb1
          

[root@fp-web-118 storage]# blkid

/dev/sda1: UUID="301c75d3-73ce-4342-befa-c8f1f788bcdf" TYPE="xfs"

/dev/sda2: UUID="dJcKYm-10WN-Vsy8-n11y-B8wY-vsFt-N76qpy" TYPE="LVM2_member"

/dev/mapper/centos-root: UUID="c73e2c36-261b-4f51-9131-4faf79d86a7d" TYPE="xfs"

/dev/mapper/centos-swap: UUID="b28efdea-e1a7-425d-bca0-23eac0dd22e0" TYPE="swap"

/dev/sdb1: UUID="69a09a5b-4384-4092-837c-601eef42d7a5" TYPE="ext3"

  6. 追加分区信息:echo '/dev/sdb1  /mnt ext3    defaults    0  0' >> /etc/fstab

    此时挂载完成后,对于当前系统盘的路径是“ /mnt”
         

  7. 查看分区信息:cat /etc/fstab

  8. 通过修改 /etc/fstab 然后执行下面的mount -a 挂载命令即可

执行mount -a之前(这个时候如果执行 mkdir -p /mnt/docker/storage 会报错)

执行mount -a之后

  9. 查看当前磁盘信息:df -h

三. 修改docker默认路径:

1、docker信息(存储目录):docker info命令查看

    [root@fp-web-118 /]#mkdir -p /mnt/docker/storage
       [root@fp-web-118 mnt]# systemctl stop docker  ##停止docker
       文件拷贝或者利用rsync 工具同步
       [root@fp-web-118 mnt]#cp -r /var/lib/docker/* /mnt/docker/storage

[root@fp-web-118 /]#vi /usr/lib/systemd/system/docker.service   ###ubuntu、debian他们的文件名称不同

   修改docker 启动入口文件配置文件信息:

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --graph /mnt/docker/storage
   ExecReload=/bin/kill -s HUP $MAINPID

2、重新加载配置文件

[root@fp-web-118 mnt]# systemctl daemon-reload

3、启动还是报错,查看日志

 [root@fp-web-118 mnt]# systemctl status docker.service 

docker.service - Docker Application Container Engine

Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)

Active: activating (start) since Fri 2022-02-04 01:33:03 CST; 21s ago

Docs: https://docs.docker.com

Main PID: 90483 (dockerd)

Memory: 19.2M

CGroup: /system.slice/docker.service

└─90483 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --graph="/mnt/docker/storage"

Feb 04 01:33:03 fp-web-118 systemd[1]: Starting Docker Application Container Engine…

Feb 04 01:33:03 fp-web-118 dockerd[90483]: time="2022-02-04T01:33:03+08:00" level=warning msg="The \"-g / --graph\" flag is deprecated. Please use \"--data-root\" instead"

4、再次编辑docker配置文件

[root@fp-web-118 /]#vi /usr/lib/systemd/system/docker.service  
ExecStart=/usr/bin/dockerd  --data-root /mnt/docker/storage

5、载配置和重新启动docker

[root@fp-web-118 mnt]# systemctl daemon-reload

[root@fp-web-118 /]# systemctl start docker
[root@fp-web-118 /]# systemctl enable docker [root@fp-web-118 /]# systemctl status docker

6、查看docker信息:

[root@fpvm-DBServer61 docker]# docker info
Containers: 9
Running: 0
Paused: 0
Stopped: 9
Images: 61
Server Version: 18.06.1-ce
Storage Driver: devicemapper
Pool Name: docker-253:0-1228875-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: /mnt/docker/storage/devicemapper/devicemapper/data
Metadata loop file: /mnt/docker/storage/devicemapper/devicemapper/metadata
Data Space Used: 17.28GB
Data Space Total: 107.4GB
Data Space Available: 20.91GB
Metadata Space Used: 15.67MB
Metadata Space Total: 2.147GB
Metadata Space Available: 2.132GB
Thin Pool Minimum Free Space: 10.74GB
Deferred Removal Enabled: true
Deferred Deletion Enabled: true
Deferred Deleted Device Count: 0
Library Version: 1.02.146-RHEL7 (2018-01-22)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
runc version: 69663f0bd4b60df09991c08812a60108003fa340
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 15.66GiB
Name: fpvm-DBServer61
ID: WJF2:VDH3:GEII:QHMF:RMWB:OTKU:FC2H:BEQC:A7BJ:ANBP:FWWM:HD3M
Docker Root Dir: /mnt/docker/storage
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use.
Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

如果你想重新安装

[root@fp-web-118 containerd]# rpm -qa |grep docker

docker-ce-18.03.0.ce-1.el7.centos.x86_64

[root@fp-web-118 containerd]#  yum list installed | grep docker

docker-ce.x86_64                      18.03.0.ce-1.el7.centos         @docker-ce-stable
[root@fp-web-118 storage]# yum remove  docker-ce-18.03.0.ce-1.el7.centos.x86_64
//删除这个下面的

[root@fp-web-118 containerd]# ls /var/lib/docker

docker/     dockershim/

//查看一下版本

[root@fp-web-118 containerd]# yum list docker-ce --showduplicates | sort -r

* updates: mirrors.aliyun.com

Loading mirror speeds from cached hostfile

Loaded plugins: fastestmirror, langpacks

Installed Packages

* extras: mirrors.aliyun.com

docker-ce.x86_64            3:20.10.9-3.el7                    docker-ce-stable

docker-ce.x86_64            3:20.10.8-3.el7                    docker-ce-stable

docker-ce.x86_64            3:20.10.7-3.el7                    docker-ce-stable

docker-ce.x86_64            3:20.10.6-3.el7                    docker-ce-stable

docker-ce.x86_64            3:20.10.5-3.el7                    docker-ce-stable

docker-ce.x86_64            3:20.10.4-3.el7                    docker-ce-stable

docker-ce.x86_64            3:20.10.3-3.el7                    docker-ce-stable

docker-ce.x86_64            3:20.10.2-3.el7                    docker-ce-stable

docker-ce.x86_64            3:20.10.1-3.el7                    docker-ce-stable

docker-ce.x86_64            3:20.10.12-3.el7                   docker-ce-stable

docker-ce.x86_64            3:20.10.11-3.el7                   docker-ce-stable

docker-ce.x86_64            3:20.10.10-3.el7                   docker-ce-stable

docker-ce.x86_64            3:20.10.0-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.9-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.8-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.7-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.6-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.5-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.4-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.3-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.2-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.15-3.el7                   docker-ce-stable

docker-ce.x86_64            3:19.03.14-3.el7                   docker-ce-stable

docker-ce.x86_64            3:19.03.1-3.el7                    docker-ce-stable

docker-ce.x86_64            3:19.03.13-3.el7                   docker-ce-stable

docker-ce.x86_64            3:19.03.12-3.el7                   docker-ce-stable

docker-ce.x86_64            3:19.03.11-3.el7                   docker-ce-stable

docker-ce.x86_64            3:19.03.10-3.el7                   docker-ce-stable

docker-ce.x86_64            3:19.03.0-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.9-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.8-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.7-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.6-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.5-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.4-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.3-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.2-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.1-3.el7                    docker-ce-stable

docker-ce.x86_64            3:18.09.0-3.el7                    docker-ce-stable

docker-ce.x86_64            18.06.3.ce-3.el7                   docker-ce-stable

docker-ce.x86_64            18.06.2.ce-3.el7                   docker-ce-stable

docker-ce.x86_64            18.06.1.ce-3.el7                   docker-ce-stable

docker-ce.x86_64            18.06.0.ce-3.el7                   docker-ce-stable

docker-ce.x86_64            18.03.1.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            18.03.0.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            18.03.0.ce-1.el7.centos            @docker-ce-stable

docker-ce.x86_64            17.12.1.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            17.12.0.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            17.09.1.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            17.09.0.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            17.06.2.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            17.06.1.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            17.06.0.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            17.03.3.ce-1.el7                   docker-ce-stable

docker-ce.x86_64            17.03.2.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable

docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable

* base: mirrors.aliyun.com

Available Packages

//添加yum源 [root@fp-web-118 storage]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo [root@fp-web-118 ~]#   yum -y install docker-ce-18.03.0.ce-1.el7.centos

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo