一、Docker安装概述
Docker从17.03版本之后分为CE(Community Edition: 社区版)和EE(Enterprise Edition: 企业版)。相对于社区版本,企业版本强调安全性,但需付费使用。这里我们使用社区版本即可。
Docker支持64位版本的CentOS 7和CentOS 8及更高版本,它要求Linux内核版本不低于3.10。
查看Linux版本的命令这里推荐两种:
lsb_release -a
cat /etc/redhat-release
lsb_release -a查看效果:
[ ~]$ lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core
cat /etc/redhat-release查看版本效果:
[~]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
显然,当前Linux系统为CentOS7。再查一下内核版本是否不低于3.10。
查看内核版本有三种方式:
- cat /proc/version
- uname -a
- uname -r
三种形式都可以查看到内容版本,比如:
[ ~]$ uname -r
3.10.0-1160.45.1.el7.x86_64
可以看到,当前Linux内核版本满足Docker的需要。
二、自动安装
Docker官方和国内daocloud都提供了一键安装的脚本,使得Docker的安装更加便捷。
官方的一键安装方式:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
国内 daocloud一键安装命令:
curl -sSL https://get.daocloud.io/docker | sh
执行上述任一条命令,耐心等待即可完成Docker的安装。
三、手动安装
1. 卸载Docker(可选)
如果之前安装过旧版本的Docker,可以使用如下命令进行卸载:
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine \
docker-ce
2. Ubuntu 14.04 16.04 (使用apt-get进行安装)
# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
有以下三个源可选,官方一般比较慢
- 官方:https://download.docker.com/linux/centos/docker-ce.repo
- 阿里云:http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
- 清华大学源:https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
3. CentOS 7 (使用yum进行安装)
# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装 Docker-CE
sudo yum makecache fast
# 按需安装 sudo yum install -y docker-ce docker-ce-cli containerd.io
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start
4. 配置镜像加速器
针对Docker客户端版本大于 1.10.0 的用户
到阿里云官网找属于自己的加速器地址(注册即有,免费的,推荐自己去注册一个) https://cr.console.aliyun.com/cn-beijing/instances/mirrors
例如:
您可以通过修改daemon配置文件/etc/docker/daemon.json
来使用加速器,其中的registry-mirrors的https://cx9q1qal.mirror.aliyuncs.com是我去注册免费赠送的
# step 1
sudo mkdir -p /etc/docker
# step 2
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://cx9q1qal.mirror.aliyuncs.com"]
}
EOF
# step 3
sudo systemctl daemon-reload
# step 4
sudo systemctl restart docker
四、Docker启动
启动Docker的命令:
sudo systemctl start docker
通过运行hello-world镜像来验证是否正确安装了Docker Engine-Community。
// 拉取镜像
sudo docker pull hello-world
// 执行hello-world
sudo docker run hello-world
如果执行之后,控制台显示如下信息,则说明Docker安装和启动成功:
[root@localhost ~]# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
……
除了启动Docker,一些其他启动相关的命令:
- 守护进程重启:systemctl daemon-reload
- 重启Docker服务:systemctl restart docker / service docker restart
- 关闭Docker服务:systemctl stop docker / service docker stop
五、删除Docker
删除安装包:
yum remove docker-ce
删除镜像、容器、配置文件等内容:
rm -rf /var/lib/docker
六、Docker其他常见命令
安装完成Docker之后,这里汇总列一下常见的Docker操作命令:
- 搜索仓库镜像:
docker search 镜像名
- 拉取镜像:
docker pull 镜像名
- 查看正在运行的容器:
docker ps
- 查看所有容器:
docker ps -a
- 删除容器:
docker rm container_id
- 查看镜像:
docker images
- 删除镜像:
docker rmi image_id
- 启动(停止的)容器:
docker start 容器ID
- 停止容器:
docker stop 容器ID
- 重启容器:
docker restart 容器ID
- 启动(新)容器:
docker run -it ubuntu /bin/bash
- 进入容器:
docker attach 容器ID
或docker exec -it 容器ID /bin/bash
,推荐使用后者。
更多的命令可以通过docker help
命令来查看。
网友评论