Docker 环境搭建
windows安装docker
不建议使用,会出现各种莫名其妙的问题
一、Linux安装docker(离线),文末附上安装脚本
1、前提条件
Docker版本:docker-20.10.3
系统版本:centos7.9,不要相信百度的贴子说centos7以上就可以
内核版本:3.10以上
[root@localhost ~]# uname -r
3.10.0-957.el7.x86_64
2、将 docker-20.10.3.tgz拷贝到任意路径并解压
[root@localhost ~]# tar zxvf docker-20.10.3.tgz
3、将docker离线包解压并移动到/usr/bin下
[root@localhost ~]# cp docker/* /usr/bin/
4、添加docker配置文件
[root@localhost ~]# cat >/etc/systemd/system/docker.service<<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[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
ExecReload=/bin/kill -s HUP $MAINPID
# 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
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# 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
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
5、添加权限
[root@localhost ~]# chmod +x /etc/systemd/system/docker.service
6、载入Docker,重启Docker,加入系统服务
systemctl daemon-reload
systemctl start docker
systemctl enable docker.service
7、离线卸载docker
rm -rf /usr/bin/docker*
rm -rf /usr/bin/containerd*
rm -rf /usr/bin/ctr
rm -rf /usr/bin/runc
rm -rf /etc/systemd/system/docker.service.d
rm -rf /var/lib/docker
rm -rf /var/run/docker
二、Linux安装docker(在线),文末附上安装脚本
1、卸载老版本
[root@localhost ~]# yum remove docker docker-common docker-selinux docker-engine
2、安装需要的软件包
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
3、设置yum源,清华镜像仓库,速度很快
[root@localhost ~]# yum -y install wget #如果没有安装wget则安装,如已安装则会跳过
[root@localhost ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo # 根据你的发行版下载repo文件:
[root@localhost ~]# sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo #把软件仓库地址替换为 TUNA
4、安装docker
[root@localhost ~]# yum makecache fast #清缓存
[root@localhost ~]# yum install docker-ce #安装docker-ce
[root@localhost ~]# systemctl daemon-reload #载入docker镜像源
[root@localhost ~]# systemctl start docker #启动
[root@localhost ~]# systemctl enable docker.service #加入开机启动
5、一键安装脚本 docker-install.sh
#!/bin/bash
# 卸载旧版本
echo "# 卸载旧版本 -- 开始 #"
yum -y remove docker docker-common docker-selinux docker-engine
echo "# 卸载旧版本 -- 结束 #"
# 安装需要的依赖软件包
echo "# 安装需要的软件包 -- 开始 #"
yum install -y yum-utils device-mapper-persistent-data lvm2
echo "# 安装需要的软件包 -- 结束 #"
# 设置 yum 源,清华镜像仓库,速度很快
echo "# 设置 yum 源 -- 开始 #"
#安装wget,如果已安装则会跳过
yum -y install wget
#根据你的发行版下载repo文件:
wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
#把软件仓库地址替换为 TUNA:
sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
echo "# 设置 yum 源 -- 结束 #"
# 安装 docker-ce
echo "# 安装 docker-ce -- 开始 #"
yum -y install docker-ce
echo "# 安装 docker-ce -- 结束 #"
# 启动并加入开机启动
echo "# 启动并加入开机启动 -- 开始 #"
systemctl daemon-reload
systemctl start docker
systemctl enable docker.service
echo "# 启动并加入开机启动 -- 结束 #"
# 验证安装是否成功
echo "# 验证安装是否成功 -- 开始 #"
docker version
echo "# 验证安装是否成功 -- 结束 #"
# 安装docker-compose
echo "# 安装docker-compose -- 开始 #"
# 从国内镜像 get.daocloud.io 快速下载
curl -L https://get.daocloud.io/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
echo "# 安装docker-compose -- 结束 #"
# 验证安装是否成功
echo "# 验证安装是否成功 -- 开始 #"
docker-compose --version
echo "# 验证安装是否成功 -- 结束 #"
# 添加 docker 国内镜像站点并重启docker
echo "# 添加 docker 国内镜像站点并重启docker -- 开始 #"
#创建目录:/etc/docker
mkdir -p /etc/docker
echo '{ "registry-mirrors": [ "https://registry.docker-cn.com" ] }' >> /etc/docker/daemon.json
systemctl restart docker
echo "# 添加 docker 国内镜像站点并重启docker -- 结束 #"
echo ""
echo ""
echo "install OK!" # 提示安装完成
卸载Docker,对于旧版本没安装成功,卸掉。
1.查询安装过的包
yum list installed | grep docker
本机安装过旧版本
docker.x86_64,docker-client.x86_64,docker-common.x86_64
2.删除安装的软件包
yum -y remove docker.x86_64
yum -y remove docker-client.x86_64
yum -y remove docker-common.x86_64
问题解决
CentOS7下载docker后启动会出现的错误:Job for docker.service failed because the control process exited with error code. See “systemctl status docker.service” and “journalctl -xe” for details
说明docker就没有启动成功
解决方法:
添加配置文件中:
vim /etc/sysconfig/docker-storage
DOCKER_STORAGE_OPTIONS="--selinux-enabled --log-driver=journald --signature-verification=false"
写入指定参数:
vim /etc/docker/daemon.json
{ "storage-driver": "devicemapper" }
方法二:
【docker的安装】
1.更新yum
yum install -y yum-utils
2.设置镜像地址,推荐阿里云的地址
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.更新yum的索引
yum makecache fast
4.安装docker,ce是社区版
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
- 查看版本
docker version
6.启动docker
systemctl start docker
7.查看状态
systemctl status docker
8.关闭docker
systemctl stop docker