Docker 有两个版本:社区版和企业版,本文只针对社区版做介绍;
基于 CentOS Linux release 7.6.1810 (Core) 实践;
1. 系统要求
- 必须为CentOS 7 的正式维护版本;
- 必须使能 centos-extras 仓库,系统默认是使能的,如果你之前去使能,请参照以下方法:
- /etc/yum.repos.d/CentOS-Base.repo 文件 [extras] 项,添加 enabled=1;
- 清除仓库缓存:
yum clean all
; - 更详细的讨论可以查考:https://wiki.centos.org/AdditionalResources/Repositories
- 推荐使用 overlay2 的存储驱动;
2. 卸载旧版本
旧版本的 docker-ce 安装包命名为 docker 或者 docker-engine,现在的安装包命名为 docker-ce; 如果你之前有安装过旧版的,我们需要先卸载它;
[luizyao@centos_7_6_1810 ~]$ sudo yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
3. 安装
如果这是你第一次在新机器上安装 Docker,你需要设置 Docker 仓库,之后你就可以从这个仓库安装和更新 Docker;
3.1. 安装必要的包
[luizyao@centos_7_6_1810 ~]$ sudo yum install -y yum-utils \
> device-mapper-persistent-data lvm2
- yum-utils 提供 yum-config-manager 工具;
- lvm2 和 device-mapper-persistent-data 是 devicemapper 存储驱动(CentOS 上 Docker 默认的存储驱动)所必需的;
3.2. 设置 stable 仓库信息
[luizyao@centos_7_6_1810 ~]$ sudo yum-config-manager \
> --add-repo \
> https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
可以看到,在 /etc/yum.repos.d/ 目录下,新加了一个 docker-ce.repo 的仓库;
注意:Docker 有三种版本 stable、test 和 nightly;
如果你也想获取其他类型的版本,可以通过以下命令:
sudo yum-config-manager --enable docker-ce-test
可以去查看 /etc/yum.repos.d/docker-ce.repo 文件,其中 [docker-ce-test] 项的 enabled 的值变成了 1;
去使能,可以通过以下命令:
sudo yum-config-manager --disable docker-ce-nightly
更多关于 nightly 和 test 的讨论可以参考:https://docs.docker.com/install/
3.3. 安装最新版的 Docker
[luizyao@centos_7_6_1810 ~]$ sudo yum install docker-ce docker-ce-cli containerd.io
安装的过程中,其为系统创建了一个用户组:docker,只是目前还没有用户加入其中;
查看 /etc/group 文件,可以看到新添加的用户组;
3.4. 启动 Docker 服务
[luizyao@centos_7_6_1810 ~]$ sudo systemctl start docker
3.5. 验证安装结果
执行sudo docker run hello-world
命令,返回以下结果,表示安装成功;
[luizyao@centos_7_6_1810 ~]$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:b8ba256769a0ac28dd126d584e0a2011cd2877f3f76e093a7ae560f2a5301c00
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/
Docker 没有在本地找到 hello-world 的 image,转而去 docker-hub 下载最新的 image;
3.6. 查看安装版本
[luizyao@centos_7_6_1810 ~]$ docker --version
Docker version 19.03.2, build 6a30dfc
4. 优化
4.1. 添加当前用户到 docker 用户组中
docker 服务绑定一个 Unix 套接字,而不是一个 TCP 端口,而操作 Unix 套接字需要 root 权限,所以其他用户执行 docker 命令时,通常要加上 sudo 关键字;
如果你不想这样,你可以把当前用户加入到 docker 的用户组中(前面安装时,已经为我们创建这个用户组),因为当 docker 服务启动时,会创建一个可以由 docker 用户组成员访问的 Unix 套接字;
注意:docker 用户组的权限几乎等同于 root;
更多对系统安全性的影响的讨论:https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
通过
usermod
命令添加,groups
查看当前用户所属的用户组:[luizyao@centos_7_6_1810 ~]$ sudo usermod -aG docker $USER
[luizyao@centos_7_6_1810 ~]$ groups luizyao
luizyao : luizyao docker执行以下命令或者重新登录;
[luizyao@centos_7_6_1810 ~]$ newgrp docker
执行
docker run hello-world
验证效果:[luizyao@centos_7_6_1810 ~]$ docker run hello-world
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/我们已经不需要在输入 sudo 关键字了;
4.2. 自启动
[luizyao@centos_7_6_1810 ~]$ sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
4.3. 关闭自启动:
[luizyao@centos_7_6_1810 ~]$ sudo systemctl disable docker
Removed symlink /etc/systemd/system/multi-user.target.wants/docker.service.
5. 卸载
- 执行命令:
sudo yum remove docker-ce
; - 删除相关文件:
sudo rm -rf /var/lib/docker
;
喜欢请关注,有用请转发~
升职、加薪、无漏测-点“在看”