Docker基础系列(一)-安装Docker服务

  • 2019-12-30
  • 68
  • 0

前言

Docker的基础教程系列,主要是Docker的基础使用,基础知识,以及使用中会遇到的各种问题和总结.本节主要是docker的基础,在linux环境上搭建docker服务.

题外话: 以前我安装各种linux系统下的应用,都是直接在宿主机上安装,然后对应的软件会安装各种其他依赖,文件,创建一大堆不知位置的文件夹.看着就头大.自从认识了docker之后,我发现我已经不会在linux宿主机上去安装应用了.哈哈.

Docker环境准备

关闭selinux

建议关闭Selinux功能,否则后续使用Docker时,会遇到一些难以预料的情况,并且启动docker容器时候,需要指定--privilege=true来解决.

// 查看linux的安全机制
getenforce  //查看Selinux状态
------
Disabled

// 关闭selinux
setenforce 0 //临时关闭Selinux

// 永久关闭
vim /etc/selinux/config   //修改Selinux配置文件
找到以下代码,将enforcing修改为disabled即可
SELINUX=enforcing  //修改此处为disabled

参考: 临时和永久关闭Selinux

替换yum源

yum安装docker的时候,需要访问国外网站的,由于GFW某些原因,可能无法访问.因此,建议在安装docker前,替换一下yum源,更换为国内的阿里或者网易的.

参考:

替换成阿里云的yum源.
替换成网易的yum源.

以下为替换阿里云指令:

cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.bak //备份原有的源.
wget -O CentOs-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

#yum源更新命令
yum clean all
yum makecache
yum update

卸载旧版本

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

安装基础依赖

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

配置稳定版本库

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

以上是国外的,访问速度比较慢.现替换成国内阿里云的docker yum源,大幅提高下载速度.

配置阿里云Docker Yum源

# Set up repository
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

# Use Aliyun Docker
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

配置完成后,在/etc/yum.repo.d/ 目录下多了一个 docker-ce.repo.

查看内容 cat /etc/yum.repo.d/docker-ce.repo:

[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge]
name=Docker CE Edge - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge-debuginfo]
name=Docker CE Edge - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-edge-source]
name=Docker CE Edge - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/edge
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

安装docker

查看所有可用的docker版本.

[root@iZwz98um4r4f2fyekyiocuZ data]# yum list docker-ce --showduplicates | sort -r
----------------
 * updates: mirrors.cloud.aliyuncs.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
Installed Packages
 * extras: mirrors.cloud.aliyuncs.com
docker-ce.x86_64            3:19.03.5-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: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:

// 安装指定版本: [去掉版本的前缀即可(含:)]
yum install docker-ce-19.03.4-3.el7
或者 yum install docker-ce-18.06.3.ce-3.el7
// 直接安装最新版本:
yum install docker-ce   //默认就是下载latest版本的.

问题点:
可能会存在冲突,注意查看冲突.然后直接使用yum remove删除指定的冲突即可.

OK,Docker安装完成!

启动docker

systemctl start docker

查看docker版本:

docker version
-----------------------
[root@iZwz98um4r4f2fyekyiocuZ data]# docker version
Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea
 Built:             Wed Nov 13 07:25:41 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea
  Built:            Wed Nov 13 07:24:18 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

设置docker自启动.指令备查

// 设置docker自启动
[root@iZwz98um4r4f2fyekyiocuZ data]# systemctl enable docker
------------------
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
===========
// 移除docker自启动
[root@iZwz98um4r4f2fyekyiocuZ data]# systemctl disable docker
------------------
Removed symlink /etc/systemd/system/multi-user.target.wants/docker.service.

配置国内镜像网站

编辑: /etc/docker/daemon.json 文件:

{
  "registry-mirrors": ["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"]
}
0

评论

还没有任何评论,你来说两句吧