CentOS7安装Docker-CE
2020/01/08
Linux
Docker
官方教程:[https://docs.docker.com/engine/install/centos/](https://docs.docker.com/engine/install/centos/) 阿里教程:[https://developer.aliyun.com/article/110806](https://developer.aliyun.com/article/110806) > 注:以下命令使用 `root` 账户 ### 如果之前安装过docker,请先卸载旧版本 ```bash yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine ``` ### 安装所需的软件包 ```bash yum install -y yum-utils ``` ### 添加软件源信息(设置存储库)并更新缓存 设置源(二选一) ```bash # 官方源 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # 阿里源 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo ``` 更新缓存 ```bash yum makecache fast ``` ### 安装 Docker-CE #### 安装最新版本 ```bash yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin ``` #### 安装指定版本 查看版本列表 ```bash yum list docker-ce --showduplicates ``` 查看到如下类似内容,返回的列表取决于启用了哪些存储库,并且特定于 `CentOS` 版本( `.el7` 在此示例中由后缀表示)。 ``` ... docker-ce.x86_64 3:20.10.20-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.21-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.22-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.23-3.el7 docker-ce-stable ``` 安装指定版本时通过其完全限定的包名称安装特定版本,即包名称 `docker-ce` 加上版本字符串(第二列从第一个冒号开始直到第一个连字符结束,例如 `20.10.23` ),由连字符分隔。例如: `docker-ce-20.10.23` 。安装脚本如下: ```bash # 替换 <VERSION_STRING> 为所需的版本,然后运行以下命令进行安装: yum -y install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-compose-plugin # 举例(推荐安装 20.10.x 向后的版本,一般情况下,列表的最后一个为最新版本) yum -y install docker-ce-20.10.23 docker-ce-cli-20.10.23 containerd.io docker-compose-plugin ``` #### 更新版本 命令同安装命令 ### 修改cgroupdriver & 修改日志文件记录方式 & 使用阿里容器镜像服务 `docker` 默认使用的文件驱动是 `cgroups` ,如果后期使用 `k8s` 时需要修改为 `Systemd` ,而且 `Systemd` 更强大 参考文档:[Cgroups 与 Systemd](https://www.cnblogs.com/sparkdev/p/9523194.html "Cgroups 与 Systemd") 日志文件官方教程:[JSON File logging driver](https://docs.docker.com/config/containers/logging/json-file/ "JSON File logging driver") `docker`默认容器下载地址在国外,国内下载很慢,需要使用阿里源。阿里镜像服务官方地址(使用阿里云账号或淘宝账号登录):[容器镜像服务](https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors) > 这里提供我的阿里源地址: ```bash mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://0zlpdgww.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } } EOF systemctl daemon-reload ``` ### 开启 Docker 服务,开启开机自启 ```bash systemctl start docker systemctl enable docker ``` ### 运行 hello-world 验证是否正确安装 执行以下脚本 ```bash docker run hello-world ``` 查看到如下类似内容 ``` [root@centos7 ~]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:aa0cc8055b82dc2509bed2e19b275c8f463506616377219d9642221ab53cf9fe Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ ```
53