docker安装
一、安装Docker CE先决条件
1.1、系统要求
Docker CE支持64位版本CentOS 7,并且要求内核版本不低于3.10。CentOS 7满足最低内核的要求,但由于内核版本比较低,部分功能(如overlay2存储层驱动)无法使用,并且部分功能可能不太稳定。
1.2、查看系统内核版本
1 2 3 4 | [root@server04 ~]# cat /proc/version Linux version 3.10.0-1160.90.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Thu May 4 15:21:22 UTC 2023 [root@server04 ~]# uname -a Linux server04 3.10.0-1160.90.1.el7.x86_64 #1 SMP Thu May 4 15:21:22 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux |
1.3、卸载旧版本
老版本的Docker被称为docker或docker-engine。如果安装了它们,请卸载他们以及相关的依赖项
1 2 3 4 5 6 7 8 9 10 | yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine |
二、安装docker ce
使用存储库进行安装#
首次在新的主机上安装Docker CE之前,需要设置Docker存储库,之后,您可以从存储安装和更新Docker。
设置存储库#
安装所需要的包。yum-utils提供了yum-config-manager,并device-mapper-persistent-data和lvm2由需要 devicemapper存储驱动程序。
1 | yum -y install yum-utils device-mapper-persistent-data lvm2 |
使用以下命令设置稳定的存储库
1 | yum-config-manager --add-repo https: //mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo |
如果需要使用最新版的Docker CE使用以下命令(默认处于禁用状态)
1 | yum-config-manager --enable docker-ce-edge |
如果使用测试版本的Docker CE请使用以下命令(默认处于禁用状态)
1 | yum-config-manager --enable docker-ce-test |
可以通过使用该标志运行命令来禁用边缘或测试存储库 。要重新启用它,请使用标志。以下命令禁用边缘存储库。yum-config-manager
--disable
--enable
1 | yum-config-manager --disable docker-ce-edge |
安装指定版本docker
1 2 3 4 5 6 7 8 9 10 11 12 13 | yum list docker-ce --showduplicates | sort -r yum install -y docker-ce-19.03.3 docker-ce-cli-19.03.3 [root@server04 ~]# docker version Client: Docker Engine - Community Version: 19.03.3 API version: 1.40 Go version: go1.12.10 Git commit: a872fc2f86 注意:如果不指定cli,就会默认最新版本,与指定的Server版本不相同 |
注意:从Docker 17.06开始,稳定版本也被推到边缘并测试版本库。
yum安装docker
1 2 | #安装 yum -y install docker-ce |
启动docker并设为开机自启
1 2 3 4 5 | systemctl start docker systemctl enable docker systemctl status docker |
测试Docker是否安装正确(此操作可以不执行)
三、创建docker用户组
docker守护程序绑定到一个Unix套接字而不是TCP端口。默认情况下,Unix套接字由root用户拥有,其它用户只能使用sudo来访问它,该docker守护进程始终运行的root用户。
处于安全考虑,一般Linux系统上不会直接使用root用户,因此,更好的做法是将需要使用docker的用户加入docker用户组。当docker守护进程启动时,它使得Unix套接字的所有权可以被docker组读/写
创建docker组
1 | groupadd docker |
将您的用户添加到docker组中
1 | usermod -aG docker $USER |
注销并重新登录,以便重新评估您的组成员资格。
如果在虚拟机上进行测试,则可能需要重新启动虚拟机才能使更改生效。
在桌面Linux环境(如X Windows)上,完全退出会话并重新登录。
验证您可以不使用运行docker命令sudo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | [root@server04 ~]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 719385e32844: Pull complete Digest: sha256:4f53e2564790c8e7856ec08e384732aa38dc43c52f02952483e3f003afbf23db 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/ [root@server04 ~]# docker run -it ubuntu bash Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 445a6a12be2b: Pull complete Digest: sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054 Status: Downloaded newer image for ubuntu:latest root@3f6092d8fd4b:/# exit exit |
四、内核参数修改
添加内核参数
默认配置下,如果在CentOS使用Docker CE看到下面的这些警告信息:
1 2 | WARING: bridge-nf-call-iptables is disabled WARING: bridge-nf-call-ip6tables is disabled |
请添加内核配置参数以启用这些功能
1 2 3 4 | tee -a /etc/sysctl.conf <<-EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF |
然后重新加载sysctl.conf即可
1 | sysctl -p |
五、配置加速器
建议安装Docker之后配置国内镜像加速。
1 2 3 4 5 | vim /etc/docker/daemon.json { "registry-mirrors" : [ "https://registry.docker-cn.com" ] } |
保存文件并重新加载Docker以使更改生效
1 | systemctl restart docker |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY