作者:邓聪聪
yum update
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker
docker卸载旧版本(如果安装过旧版本的话)
rpm -qa|grep docker
yum remove docker docker-common docker-selinux docker-engine
yum erase docker-common-<FQPN>
安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
yum install -y yum-utils device-mapper-persistent-data lvm2
设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y epel-release #也可以使用epel源安装docker
可以查看所有仓库中所有docker版本,并选择特定版本安装
yum list docker-ce --showduplicates | sort -r
安装docker
yum install -y docker-ce #由于repo中默认只开启stable仓库,故这里安装的是最新稳定版17.12.0
yum install -y docker-ce-17.12.0.ce # 例如yum install docker-ce-17.12.0.ce
systemctl start docker
验证安装是否成功(有client和service两部分表示docker安装启动都成功了)
docker version
docker ps #查看docker的运行的任务
docker ps -a #查看docker运行过的任务
docker start <CONTAINER ID> #重新启动docker容器的任务
安装实例
docker部署nginx:
docker pull nginx //下载nginx
docker images
对容器的镜像打标签
docker tag nginx nginx:3.23
删除镜像、和容器
docker rmi <image ID> #删除一个或多个image
docker rm <container...> #删除一个或多个container
运行
docker run --name mynginx -p 8080:80 -v /date/nginx/html:/usr/share/nginx/html -d nginx
命名 定义主机端口8080,容器内的端口80 挂载主机上的目录给容器(主机上和docker容器上都必须为绝对路径) 镜像文件
docker inspect CONTAINER_ID #查看对应路径"Mounts"
"
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io
yum install docker-ce-18.09.1 docker-ce-cli-18.09.1 containerd.io
"
一键环境的安装:
RELEASE=$(rpm -q centos-release | cut -d '-' -f 3)
if [ "$RELEASE" = "6" ];
then
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
elif [ "$RELEASE" = "7" ];
then
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
elif [ "$RELEASE" = "8" ];
then
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
fi
yum clean all
yum makecache
yum install -y yum-utils device-mapper-persistent-data lvm2 vim wget python3 python3-devel vim wget lsof git ntpdate
ln -s /usr/bin/pip3 /usr/bin/pip
ntpdate ntp.aliyun.com
# Step 2: 更新并安装Docker-CE
if [ "$RELEASE" = "8" ];
then
dnf install -y https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/Packages/containerd.io-1.3.7-3.1.el8.x86_64.rpm
dnf install -y https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/Packages/docker-ce-19.03.13-3.el8.x86_64.rpm
dnf install -y https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/Packages/docker-ce-cli-19.03.13-3.el8.x86_64.rpm
dnf install -y python3-devel && ln -s /usr/bin/pip3 /usr/bin/pip
else
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce python3-pip
fi
# Step 3: 添加DOCKER加速器
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{ "registry-mirrors": ["https://r2xanz27.mirror.aliyuncs.com"] }
EOF
# Step 4: 开启Docker服务
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
pip --version
pip install --upgrade pip
pip install docker-compose==1.24.1
docker-compose version