在CentOS7环境下安装Docker的默认模式

avatar 2019年9月14日22:48:34 评论 1,384 次浏览

安装要求:

我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核3.8以上, RHEL/Centos 的内核修补过, centos6.5的版本就可以——这个可以试试)

注:内核在3.10.0-693以下版本不管是使用yum还是使用rpm都不能使用overlay2存储模式,不过使用direct-lvm模式可以使用3.10.0-229.el7.x86_64系统安装,下面安装模式使用的是overlay2模式

YUM安装方式

1、查看一下内核版本

# uname -a
Linux linux-server.shared 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

官方建议是使用内核4.*版本才可以使用dockeroverlay2存储模式,这里使用的是3.10.0-957.el7.x86_64没有升级内核也可以使用overlay2模式。

2、更新yum源

# yum update

注:为啥更新yum源

3、安装必要的包

# yum install -y yum-utils device-mapper-persistent-data lvm2

4、设置yum源

# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
阿里云的
# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

5、查看一下Docker中所有仓库的版本,然后根据自己的需求选择特定的版本。

# yum list docker-ce --showduplicates | sort -r

6、安装自己选择的docker

# yum install -y docker-ce-17.12.1.ce

7、配置Docker overlay2存储驱动并启动

# mkdir /etc/docker
# vim /etc/docker/daemon.json
{
  "storage-driver": "overlay2",
  "storage-opts": ["overlay2.override_kernel_check=true"]
}

注:配置设置的是overlay2的模式,如果使用overlay模式,只需要把overlay2改成overlay即可。

8、启动自己安装的docker,并加入开机自动启动中

# systemctl start docker
# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

9、验证自己安装中的版本

#docker version 
Client:
 Version:       17.12.1-ce
 API version:   1.35
 Go version:    go1.9.4
 Git commit:    7390fc6
 Built: Tue Feb 27 22:15:20 2018
 OS/Arch:       linux/amd64

Server:
 Engine:
  Version:      17.12.1-ce
  API version:  1.35 (minimum version 1.12)
  Go version:   go1.9.4
  Git commit:   7390fc6
  Built:        Tue Feb 27 22:17:54 2018
  OS/Arch:      linux/amd64
  Experimental: false

10、查看docker相信信息

# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 17.12.1-ce
Storage Driver: overlay2  #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 logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9b55aab90508bd389d7654c4baf173a981477d55
runc version: 9f9c96235cc97674e935002fc3d78361b696a69e
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.791GiB
Name: centos-linux-7.shared
ID: AE3L:OX4X:XO4H:TTZV:UTKI:O7RR:AZ6O:Y6Q5:OCFM:KXYO:3C5U:G3UG
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
Live Restore Enabled: false

RPM安装方式

1、更新yum源

# yum -y update

2、启用ELREPO仓库,

 # rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
Retrieving http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
Retrieving http://elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:elrepo-release-7.0-4.el7.elrepo  ################################# [100%]

3、安装最新版的4.x内核

# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * elrepo-kernel: mirrors.tuna.tsinghua.edu.cn
elrepo-kernel                                                                                                                                         | 2.9 kB  00:00:00     
elrepo-kernel/primary_db                                                                                                                              | 1.8 MB  00:00:02     
Available Packages
kernel-lt.x86_64                                                                      4.4.189-1.el7.elrepo                                                      elrepo-kernel
kernel-lt-devel.x86_64                                                                4.4.189-1.el7.elrepo                                                      elrepo-kernel
kernel-lt-doc.noarch                                                                  4.4.189-1.el7.elrepo                                                      elrepo-kernel
kernel-lt-headers.x86_64                                                              4.4.189-1.el7.elrepo                                                      elrepo-kernel
kernel-lt-tools.x86_64                                                                4.4.189-1.el7.elrepo                                                      elrepo-kernel
kernel-lt-tools-libs.x86_64                                                           4.4.189-1.el7.elrepo                                                      elrepo-kernel
kernel-lt-tools-libs-devel.x86_64                                                     4.4.189-1.el7.elrepo                                                      elrepo-kernel
kernel-ml.x86_64                                                                      5.2.9-1.el7.elrepo                                                        elrepo-kernel
kernel-ml-devel.x86_64                                                                5.2.9-1.el7.elrepo                                                        elrepo-kernel
kernel-ml-doc.noarch                                                                  5.2.9-1.el7.elrepo                                                        elrepo-kernel
kernel-ml-headers.x86_64                                                              5.2.9-1.el7.elrepo                                                        elrepo-kernel
kernel-ml-tools.x86_64                                                                5.2.9-1.el7.elrepo                                                        elrepo-kernel
kernel-ml-tools-libs.x86_64                                                           5.2.9-1.el7.elrepo                                                        elrepo-kernel
kernel-ml-tools-libs-devel.x86_64                                                     5.2.9-1.el7.elrepo                                                        elrepo-kernel
perf.x86_64                                                                           5.2.9-1.el7.elrepo                                                        elrepo-kernel
python-perf.x86_64                                                                    5.2.9-1.el7.elrepo

目前我这最高版本的是5.x版本的,如果选择4.x版本的把kernel-ml改成kernel-lt即可。使用4.x的可以,下面使用5.x的版本。

# yum --enablerepo=elrepo-kernel install kernel-ml

4、设置grub2

查看系统上所有的可用内核  
#awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (5.2.9-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)
2 : CentOS Linux (0-rescue-121c0b3e3b657c4c8d627ffaa061a0be) 7 (Core)

设置5.x kernel为默认启动,“0”为上面AWK命令的结果
#grub2-set-default 0

生成grub配置文件并启动
#grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.2.9-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-5.2.9-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-957.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-121c0b3e3b657c4c8d627ffaa061a0be
Found initrd image: /boot/initramfs-0-rescue-121c0b3e3b657c4c8d627ffaa061a0be.img
done

如果系统盘不是XFS格式需要添加一个盘,关机添加一个盘,因为我的系统是XFS格式,所以模拟一下。

5、创建XFS文件下系统

# fdisk /dev/sdb   #先给添加的盘做分区
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xae8285bb.

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-62914559, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): 
Using default value 62914559
Partition 1 of type Linux and of size 30 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

创建PV
 # pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
创建VG 
# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended

# lvcreate -L 5G -n lv_docker centos
  Logical volume "lv_docker" created.

# mkfs.xfs -n ftype=1 /dev/mapper/centos-lv_docker
meta-data=/dev/mapper/centos-lv_docker isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

# mkdir /usr/lib/docker
# mount /dev/mapper/centos-lv_docker /usr/lib/docker

6、安装Docker-CE

# yum install -y container-selinux libtool-ltdl libseccomp yum-plugin-ovl yum-utils device-mapper-persistent-data lvm2
# wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-17.12.1.ce-1.el7.centos.x86_64.rpm
# rpm -ivh docker-ce-17.12.1.ce-1.el7.centos.x86_64.rpm
warning: docker-ce-17.12.1.ce-1.el7.centos.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:docker-ce-17.12.1.ce-1.el7.centos################################# [100%]

7、配置Docker overlay2存储驱动并启动

# mkdir /etc/docker
# systemctl daemon-reload
# systemctl start docker
# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 17.12.1-ce
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 logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9b55aab90508bd389d7654c4baf173a981477d55
runc version: 9f9c96235cc97674e935002fc3d78361b696a69e
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 5.2.9-1.el7.elrepo.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.94GiB
Name: centos-linux-7.shared
ID: DMUC:PEVS:R57X:LKO3:LRFA:TQ7K:GNVA:KWLX:XU3U:2T2U:TPHM:4XD5
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
Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

8、修改默认存储路径

# vim /usr/lib/systemd/system/docker.service 
..............
ExecStart=/usr/bin/dockerd  --graph /usr/lib/docker
..............

# systemctl daemon-reload 
# systemctl restart docker 
# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 17.12.1-ce
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 logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9b55aab90508bd389d7654c4baf173a981477d55
runc version: 9f9c96235cc97674e935002fc3d78361b696a69e
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 5.2.9-1.el7.elrepo.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.94GiB
Name: centos-linux-7.shared
ID: DMUC:PEVS:R57X:LKO3:LRFA:TQ7K:GNVA:KWLX:XU3U:2T2U:TPHM:4XD5
Docker Root Dir: /usr/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
Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

9、解决WARNING问题

# vi /etc/sysctl.conf
..................
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
..................
# sysctl -p
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1

# docker info        
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 17.12.1-ce
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 logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9b55aab90508bd389d7654c4baf173a981477d55
runc version: 9f9c96235cc97674e935002fc3d78361b696a69e
init version: 949e6fa
Security Options:
 seccomp
  Profile: default
Kernel Version: 5.2.9-1.el7.elrepo.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.94GiB
Name: centos-linux-7.shared
ID: DMUC:PEVS:R57X:LKO3:LRFA:TQ7K:GNVA:KWLX:XU3U:2T2U:TPHM:4XD5
Docker Root Dir: /usr/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
Live Restore Enabled: false

docker的基本命令扩展

docker ps 查看当前正在运行的容器
docker ps -a 查看所有容器的状态
docker start/stop id/name 启动/停止某个容器
docker attach id 进入某个容器(使用exit退出后容器也跟着停止运行)
docker exec -ti id 启动一个伪终端以交互式的方式进入某个容器(使用exit退出后容器不停止运行)
docker images 查看本地镜像
docker rm id/name 删除某个容器
docker rmi id/name 删除某个镜像
docker run --name test -ti ubuntu /bin/bash  复制ubuntu容器并且重命名为test且运行,然后以伪终端交互式方式进入容器,运行bash
docker build -t soar/centos:7.1 .  通过当前目录下的Dockerfile创建一个名为soar/centos:7.1的镜像
docker run -d -p 2222:22 --name test soar/centos:7.1  以镜像soar/centos:7.1创建名为test的容器,并以后台模式运行,并做端口映射到宿主机2222端口,P参数重启容器宿主机端口会发生改变

卸载Docker

yum remove docker  docker-common docker-selinux docker-engine

Docker删除容器与镜像(不建议使用,使用要注意)

1.停止所有的container,这样才能够删除其中的images:

docker stop $(docker ps -a -q)
docker kill $(docker ps -a -q)

如果想要删除所有container的话再加一个指令:

docker rm $(docker ps -a -q)

查看当前有些什么images

docker images

删除images,通过image的id来指定删除谁

docker rmi <image id>

想要删除untagged images,也就是那些id为的image的话可以用

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

要删除全部image的话

docker rmi $(docker images -q)

强制删除全部image的话

docker rmi -f $(docker images -q)

在安装过程中遇到的问题以及解决方法:

1、因为之前已经安装过旧版本的docker,在安装的时候报错如下:

Transaction check error:
  file /usr/bin/docker from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  file /usr/bin/docker-containerd from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  file /usr/bin/docker-containerd-shim from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64
  file /usr/bin/dockerd from install of docker-ce-17.12.0.ce-1.el7.centos.x86_64 conflicts with file from package docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64

卸载旧版本、 $ sudo yum erase docker-common-2:1.12.6-68.gitec8512b.el7.centos.x86_64

然后再再次安装

$ sudo yum install docker-ce

2、docker启动报错信息 在使用systemctl start docker启动失败时,使用systemctl status docker.service 报错信息:

[root@wulaoer ~]# systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled;
 vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2018-07-25 00:24:56 
EDT; 2min 4s ago
     Docs: http://docs.docker.com
 Main PID: 2600 (code=exited, status=1/FAILURE)
Jul 25 00:24:54 localhost.localdomain systemd[1]: Starting Docker...
Jul 25 00:24:54 localhost.localdomain dockerd-current[2600]: time...
Jul 25 00:24:54 localhost.localdomain dockerd-current[2600]: time...
Jul 25 00:24:55 localhost.localdomain dockerd-current[2600]: time...
Jul 25 00:24:56 localhost.localdomain dockerd-current[2600]: Erro...
Jul 25 00:24:56 localhost.localdomain systemd[1]: docker.service:...
Jul 25 00:24:56 localhost.localdomain systemd[1]: Failed to start...
Jul 25 00:24:56 localhost.localdomain systemd[1]: Unit docker.ser...
Jul 25 00:24:56 localhost.localdomain systemd[1]: docker.service ...
Hint: Some lines were ellipsized, use -l to show in full.
在vi /etc/sysconfig/docker中把–selinux-enabled后增加=false,然后重启
查资料得知:这里是linux的内核中的SELinux不支持 overlay2 graph driver ,解决方法有两个,要么启动一个新内核,要么就在docker里禁用selinux,–selinux-enabled=false

至此,Docker的基本安装已经基本完成,可以尝试一下自己安装操作一下,wulaoer.org********

 

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: