安装docker
一、基于yum方式安装docker:
(1)卸载旧版本的docker环境
yum -y remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
(2)添加docker的软件源
curl -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
(3)安装docker-ce软件包
yum -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin
(4)启动docker服务并设置开机自启动
systemctl --now enable docker
(5)查看docker的版本信息
docker version
二、基于二进制安装docker
(1)下载docker二进制软件包
yum -y install wget
wget https://download.docker.com/linux/static/stable/x86_64/docker-19.03.15.tgz
(2)解压二进制文件
tar xf docker-19.03.15.tgz && mv docker/* /usr/local/bin && rm -rf docker
(3)编写启动脚本
cat > /usr/lib/systemd/system/docker.service <<EOF
[Unit]
Description= Docker Application Container Engine
[Service]
Type=simple
ExecStart=/usr/local/bin/dockerd
[Install]
WantedBy=multi-user.target
EOF
(4)启动docker服务并设置开机自启动
systemctl daemon-reload
systemctl enable --now docker
(5)检查docker版本
[root@docker ~]# docker -v
Docker version 19.03.15, build 99e3ed8
二进制安装脚本
[root@docker ~]# cat install_binary_docker.sh
#!/bin/bash
# DOCKER_VERSION=19.03.15
DOCKER_VERSION=20.10.23
URL=https://download.docker.com
# URL=https://mirrors.tuna.tsinghua.edu.cn
# URL=https://mirrors.aliyun.com
ARCH=x86_64
function prepare {
# 判断是否下载wget,若不存在则下载wget
[ -f /usr/bin/wget ] || yum -y install wget
# 判断是否下载docker软件包,若不存在则下载即可
[ -f docker-${DOCKER_VERSION}.tgz ] || wget ${URL}/linux/static/stable/${ARCH}/docker-${DOCKER_VERSION}.tgz
}
function install {
# 安装docker环境
tar xf docker-${DOCKER_VERSION}.tgz && mv docker/* /usr/local/bin && rm -rf docker
# 编写启动脚本
cat > /usr/lib/systemd/system/docker.service <<EOF
[Unit]
Description= Docker Application Container Engine
[Service]
Type=simple
ExecStart=/usr/local/bin/dockerd
[Install]
WantedBy=multi-user.target
EOF
# 启动docker服务并检查版本
systemctl daemon-reload
systemctl enable --now docker
sleep 3
docker version
}
prepare
install
[root@docker ~]# bash install_binary_docker.sh
三、docker-compose安装
可以自己选择版本进行更换
http://mirror.azure.cn/docker-toolbox/linux/compose/
# 从微软镜像下载docker-compose安装
curl -L http://mirror.azure.cn/docker-toolbox/linux/compose/v2.15.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
# 设置权限
chmod +x /usr/local/bin/docker-compose
四、自动补全功能:
yum -y install bash-completion
source /usr/share/bash-completion/bash_completion
五、docker镜像加速:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://tuv7rqqq.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker