CentOS 安装 Docker
Docker 是一个开源的商业产品,有两个版本:社区版(Community Edition,缩写为 CE)和企业版(Enterprise Edition,缩写为 EE)。企业版包含了一些收费服务,个人开发者一般用不到。下面的介绍都针对社区版。
Docker CE 的安装请参考官方文档,我们这里以CentOS为例:
Docker 要求 CentOS 系统的内核版本高于 3.10
通过 uname -r 命令查看你当前的内核版本
uname -r
使用 root 权限登录 Centos。确保 yum 包更新到最新。
yum -y update
卸载旧版本(如果安装过旧版本的话)
sudo yum remove -y docker*
安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
yum install -y yum-utils
设置yum源,并更新 yum 的包索引
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
可以查看所有仓库中所有docker版本,并选择特定版本安装
yum list docker-ce --showduplicates | sort -r
安装docker
yum install -y docker-ce-3:24.0.2-1.el7.x86_64 # 这是指定版本安装
启动并加入开机启动
systemctl start docker && systemctl enable docker
验证安装是否成功(有client和service两部分表示docker安装启动都成功了)
docker version
配置docker镜像加速器
我们可以借助阿里云的镜像加速器,登录阿里云(阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台)
可以看到镜像加速地址如下图:
cd /etc/docker
查看有没有 daemon.json。这是docker默认的配置文件。
如果没有新建,如果有,则修改。
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://o9kj12c8.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
卸载docker
# 关闭docker
systemctl stop docker
# 删除docker 相关
yum remove -y docker*
#删除docker 服务
rm -rf /etc/systemd/system/docker.service.d
#删除镜像、容器、卷、自定义配置等文件
rm -rf /var/lib/docker
rm -rf /var/run/docker
运行HelloWorld测试
docker run hello-world
Ubuntu安装Docker
卸载旧版本
卸载旧版本Docker(如果已经安装):
sudo apt-get remove docker docker-engine docker.io containerd runc
安装依赖
安装必要的依赖:
sudo apt-get update sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
添加docker软件源
官网源:(不推荐,可能会连不上报错)
# 导入源仓库的 GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# 将 Docker APT 软件源添加到你的系统
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
阿里云源(推荐):
# 添加Docker GPG密钥
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 添加Docker软件源信息
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
安装docker
更新软件源并安装Docker:
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
启动并测试Docker
sudo systemctl start docker sudo docker run hello-world
离线安装
下载安装
从docker官网(或者国内镜像站)下载对应linux版本的安装包,例如docker-27.0.3.tgz。
将安装包上传到服务器,解压:
tar -zxvf docker-27.0.3.tgz
解压出来的文件的所有者可能不是root,修改为root用户:
chown -R root:root docker/
将解压的文件移动到
\cp -f docker/* /usr/bin
启动测试:
dockerd
配置成系统服务
将docker添加到systemd:
编辑文件/usr/lib/systemd/system/docker.service
文件内容参考:github的docker-ce官方文档
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service
Wants=network-online.target containerd.service
Requires=docker.socket
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500
[Install]
WantedBy=multi-user.target
为docker.service添加执行权限:
chmod +x /usr/lib/systemd/system/docker.service
配置docker
创建docker组
groupadd docker
编辑daemon.json配置文件:
文件:/etc/docker/daemon.json
{
// 还可以在这个文件中配置加速器、镜像仓库地址等
"insecure-registries": [
// 配置不验证https证书、允许http连接
"192.168.xxx.xxx:8088"
],
"data-root":"/data/docker",
"log-driver":"json-file",
"log-opts":{
"max-size":"200m","max-file":"3"
}
}
重载配置:
systemctl daemon-reload
启动docker并设置开机自启
启动docker:
systemctl start docker
设置开机自启:
docker version
配置命令自动补全
从docker的github官方仓库获取命令补全的文件:docker
将该文件放到/usr/share/bash-completion/completions,启用该文件:
source /usr/share/bash-completion/completions/docker