CentOS上装Docker,常用的方法就那么几种。下面把最主流的四种方式梳理一下,从官方源安装到脚本一键部署,再到非root用户配置和国内镜像加速,一次性讲清楚。
一、从Docker官方YUM源安装
这是最稳妥的方式,适合对网络环境有把控的场景。先添加官方仓库,再执行安装。
1、添加Docker官方源
cat >/etc/yum.repos.d/docker.repo <<-EOF [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7 enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF
2、安装和启动
# 安装 sudo yum install docker-engine # 启动 sudo systemctl enable docker.service # 开机自启 systemctl enable docker.service # 验证安装 sudo docker run hello-world
二、用脚本的方式安装
如果你不想手动配源,Docker官方提供了一键安装脚本,一条命令搞定——前提是网络通畅。
curl -sSL https://get.docker.com/ | sh
脚本会自动判断系统版本并完成安装,适合快速验证环境。
三、非特权用户运行docker命令
安装完成后,默认只有root能执行docker命令。但日常开发中,我们更习惯用普通用户操作。解决办法是把当前用户加入docker用户组。
sudo usermod -a -G docker $USER
执行完这一步,需要登出再登入,组权限才会生效。之后普通用户就能直接运行docker命令了,不需要每次都加sudo。
四、使用国内加速源
国内访问Docker Hub经常慢到怀疑人生,所以镜像加速基本上是刚需。DaoCloud提供了一个工具集,可以当作翻跟斗使用。
1、安装DaoCloud工具
curl -sSL https://get.daocloud.io/daomonit/install.sh | sh -s 6d3f36d42ae3f3d4237943c65c8f3e5446e49a71
2、使用dao命令拉取镜像
dao pull ubuntu
执行后会有类似下面的输出:
➜ ~ dao pull ubuntu Dao from DaoCloud Initializing, Please wait a minute Using default tag: latest Pulling repository daocloud.io/daocloud/daocloud-toolset aa5dc2eecd4a: Download complete Status: Image is up to date for daocloud.io/daocloud/daocloud-toolset:latest daocloud.io/daocloud/daocloud-toolset: this image was pulled from a legacy registry. Important: This registry version will not be supported in future versions of docker. Inital Success # ----------------------------------------------------------------------------# # DaoCloud ToolBox for Docker # # DaoCloud, Inc. (c) 2015 # # Fastest way to pull image from Docker Registry # ----------------------------------------------------------------------------# Pulling repository ubuntu Pulling ubuntu image metadata Pulling ubuntu:latest tag metadata 0105f98ced6d: Load layer complete 66395c31eb82: Load layer complete 002fa881df8a: Load layer complete a005e6b7dd01: Load layer complete Loading image to docker ... ** Pull ubuntu success. ** You can find it with 'docker images ubuntu'
拉取完成后,用docker images ubuntu就能看到刚下载的镜像了。这样一来,即使是国内环境,也能比较顺畅地体验Docker。
