Linux-docker安装及基本参数详解
国内源安装docker
centos系类通过yum源安装,通过阿里云源
[root@centos7-liyj ~]#cd /etc/yum.repos.d/ [root@centos7-liyj /etc/yum.repos.d]#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [root@centos7-liyj /etc/yum.repos.d]#wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo [root@centos7-liyj /etc/yum.repos.d]#ll total 88 -rw-r--r-- 1 root root 1862 Apr 25 09:57 CentOS-Base.repo -rw-r--r--. 1 root root 1309 Nov 23 2020 CentOS-CR.repo -rw-r--r--. 1 root root 649 Nov 23 2020 CentOS-Debuginfo.repo -rw-r--r--. 1 root root 326 Apr 18 18:33 CentOS-fasttrack.repo -rw-r--r--. 1 root root 630 Nov 23 2020 CentOS-Media.repo -rw-r--r--. 1 root root 1331 Nov 23 2020 CentOS-Sources.repo -rw-r--r--. 1 root root 8515 Nov 23 2020 CentOS-Vault.repo -rw-r--r--. 1 root root 640 Apr 18 18:33 CentOS-x86_64-kernel.repo -rw-r--r-- 1 root root 2081 Jul 28 13:14 docker-ce.repo -rw-r--r-- 1 root root 664 Dec 26 2020 epel.repo
安装docker
[root@centos7-liyj /etc/yum.repos.d]#yum install docker-ce Dependencies Resolved ===================================================================================================== Package Arch Version Repository Size ===================================================================================================== Installing: docker-ce x86_64 3:20.10.17-3.el7 docker-ce-stable 22 M Installing for dependencies: audit-libs-python x86_64 2.8.5-4.el7 BASE 76 k checkpolicy x86_64 2.5-8.el7 BASE 295 k container-selinux noarch 2:2.119.2-1.911c772.el7_8 extras 40 k containerd.io x86_64 1.6.6-3.1.el7 docker-ce-stable 33 M docker-ce-cli x86_64 1:20.10.17-3.el7 docker-ce-stable 29 M docker-ce-rootless-extras x86_64 20.10.17-3.el7 docker-ce-stable 8.2 M docker-scan-plugin x86_64 0.17.0-3.el7 docker-ce-stable 3.7 M fuse-overlayfs x86_64 0.7.2-6.el7_8 extras 54 k fuse3-libs x86_64 3.6.1-4.el7 extras 82 k libcgroup x86_64 0.41-21.el7 BASE 66 k libseccomp x86_64 2.3.1-4.el7 BASE 56 k libsemanage-python x86_64 2.5-14.el7 BASE 113 k policycoreutils-python x86_64 2.5-34.el7 BASE 457 k python-IPy noarch 0.75-6.el7 BASE 32 k setools-libs x86_64 3.3.8-4.el7 BASE 620 k slirp4netns x86_64 0.4.3-4.el7_8 extras 81 k Transaction Summary ===================================================================================================== Install 1 Package (+16 Dependent packages)
[root@centos7-liyj ~]#rpm -ql docker-ce /usr/bin/docker-init /usr/bin/docker-proxy /usr/bin/dockerd /usr/lib/systemd/system/docker.service /usr/lib/systemd/system/docker.socket
Ubuntu清华源apt安装
参考:https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/
如果你过去安装过 docker,先删掉: sudo apt-get remove docker docker-engine docker.io containerd runc
安装依赖关系:
[15:57:44 root@ubuntu-lyj ~]#apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
信任 Docker 的 GPG 公钥:
[16:02:43 root@ubuntu-lyj ~]#mkdir /etc/apt/keyrings [18:27:48 root@ubuntu-lyj ~]#curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
添加软件仓库
[18:27:55 root@ubuntu-lyj ~]#echo \ > "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \ > $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
更新源,安装docker
[18:28:20 root@ubuntu-lyj ~]#sudo apt-get update [18:30:00 root@ubuntu-lyj ~]#sudo apt-get install docker-ce
验证docker服务启动
[18:33:03 root@ubuntu-lyj ~]#systemctl start docker [18:33:11 root@ubuntu-lyj ~]#systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2022-07-28 18:30:58 CST; 2min 22s ago Docs: https://docs.docker.com Main PID: 19480 (dockerd) Tasks: 8 CGroup: /system.slice/docker.service └─19480 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock Jul 28 18:30:57 ubuntu-lyj dockerd[19480]: time="2022-07-28T18:30:57.425321563+08:00" level=info msg="ClientConn Jul 28 18:30:57 ubuntu-lyj dockerd[19480]: time="2022-07-28T18:30:57.506293241+08:00" level=warning msg="Your ke Jul 28 18:30:57 ubuntu-lyj dockerd[19480]: time="2022-07-28T18:30:57.506326683+08:00" level=warning msg="Your ke Jul 28 18:30:57 ubuntu-lyj dockerd[19480]: time="2022-07-28T18:30:57.506443341+08:00" level=info msg="Loading co Jul 28 18:30:57 ubuntu-lyj dockerd[19480]: time="2022-07-28T18:30:57.771477872+08:00" level=info msg="Default br Jul 28 18:30:57 ubuntu-lyj dockerd[19480]: time="2022-07-28T18:30:57.991872435+08:00" level=info msg="Loading co Jul 28 18:30:58 ubuntu-lyj dockerd[19480]: time="2022-07-28T18:30:58.040367678+08:00" level=info msg="Docker dae Jul 28 18:30:58 ubuntu-lyj dockerd[19480]: time="2022-07-28T18:30:58.040623154+08:00" level=info msg="Daemon has Jul 28 18:30:58 ubuntu-lyj systemd[1]: Started Docker Application Container Engine. Jul 28 18:30:58 ubuntu-lyj dockerd[19480]: time="2022-07-28T18:30:58.095797519+08:00" level=info msg="API listen
Docker基本参数详解
docker版本
[18:33:24 root@ubuntu-lyj ~]#docker version Client: Docker Engine - Community Version: 20.10.17 API version: 1.41 Go version: go1.17.11 Git commit: 100c701 Built: Mon Jun 6 23:02:56 2022 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.17 API version: 1.41 (minimum version 1.12) Go version: go1.17.11 Git commit: a89b842 Built: Mon Jun 6 23:01:02 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.6.6 GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1 runc: Version: 1.1.2 GitCommit: v1.1.2-0-ga916309 docker-init: Version: 0.19.0 GitCommit: de40ad0
验证docker0网卡:
在docker安装启动之后,默认会生成一个名称为docker0的网卡并且默认IP地址为172.17.0.1的网卡。
[20:29:37 root@ubuntu-lyj ~]#ifconfig docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:2a:8e:8d:1f txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::20c:29ff:fe3e:d37e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:3e:d3:7e txqueuelen 1000 (Ethernet) RX packets 47 bytes 5878 (5.8 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 46 bytes 7141 (7.1 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 80 bytes 5920 (5.9 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 80 bytes 5920 (5.9 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
验证docker信息:
[20:29:42 root@ubuntu-lyj ~]#docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Docker Buildx (Docker Inc., v0.8.2-docker) scan: Docker Scan (Docker Inc., v0.17.0) Server: Containers: 0 #当前主机运行的容器总数 Running: 0 #有几个容器是正在运行的
Paused: 0 #有几个容器是暂停的 Stopped: 0 #有几个容器是停止的 Images: 0 #当前服务器的镜像数 Server Version: 20.10.17 #服务端版本 Storage Driver: overlay2 #正在使用的存储引擎 Backing Filesystem: extfs #后端文件系统,即服务器的磁盘文件系统 Supports d_type: true #是否支持d_type Native Overlay Diff: true #是否迟滞差异数据存储 userxattr: false Logging Driver: json-file #日志类型 Cgroup Driver: cgroupfs #Cgroup类型 Cgroup Version: 1 Plugins: #插件 Volume: local #卷 Network: bridge host ipvlan macvlan null overlay #overlay夸主机通信 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog #日志类型 Swarm: inactive #是否支持swarm Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc #已安装的容器运行时 Default Runtime: runc #默认使用的容器运行时 Init Binary: docker-init #初始化容器的守护京城,即PID为1的进程 containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1 #版本 runc version: v1.1.2-0-ga916309 #runc版本 init version: de40ad0 #init版本 Security Options: #安全选项 apparmor #安全模块 seccomp #审计(操作) Profile: default #默认的配置文件 Kernel Version: 4.15.0-112-generic #宿主机内核版本 Operating System: Ubuntu 18.04.5 LTS #宿主机操作系统 OSType: linux #宿主机操作系统类型 Architecture: x86_64 #宿主机架构 CPUs: 2 #宿主机CPU书香 Total Memory: 1.924GiB #宿主机总内存 Name: ubuntu-lyj #宿主机hostname ID: 2QBC:36LO:K7N6:3TNA:DL63:BPX3:L6GU:2JLI:XC56:IDQT:4DPS:GLUD #宿主机ID Docker Root Dir: /var/lib/docker #宿主机数据保存目录 Debug Mode: false #是否开启debug Registry: https://index.docker.io/v1/ #镜像仓库 Labels: #其他标签 Experimental: false #是否测试版 Insecure Registries: #非安全的镜像仓库 127.0.0.0/8 Live Restore Enabled: false #是否开启活动重启(重启docker-daemon不关闭容器) WARNING: No swap limit support #系统警告信息(没有开启swap资源限制)
解决不支持swap限制警告:
[20:46:20 root@ubuntu-lyj ~]#grep "^[a-Z]" /etc/default/grub GRUB_DEFAULT=0 GRUB_TIMEOUT_STYLE=hidden GRUB_TIMEOUT=2 GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian` GRUB_CMDLINE_LINUX_DEFAULT="" GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 cgroup_enable=memory swapaccount=1"
docker存储引擎
目前docker的默认存储引擎为overlay2,不同的存储引擎需要相应的系统支持,如需要磁盘分区的时候传递d-type文件分层功能,即需要传递内核参数开启格式化磁盘的时候的指定功能。
docker官方关于存储引擎的选择文档:
https://docs.docker.com/storage/storagedriver/select-storage-driver/
存储驱动类型:
AUFS是一种Union FS,是文件级的存储驱动。 UnionFS就是把不同物理位置的目录合并挂载到同一个目录中。 简单来说就是支持将不同目录挂载到同一个虚拟文件系统下的文件系统。 这种文件系统可以一层一层地叠加修改文件。无论地下有多少层都是只读的,只有最上层的文件系统是可写的。 当需要修改一个文件时,AUFS创建该文件的一个副本,使用CoW将文件从只读层复制到可写层进行修改,结果也保存在可写层。 在Docker中,地下的只读层就是image,可写层就是Container,是Docker 18.06及更早版本的首选存储驱动程序,在内核3.13上运行Ubuntu 14.04时不支持overlay2. Overlay:一种Union FS文件系统,Linux内核3.18后支持。 overlay2:Overlay的升级版,到目前位置,所有Linux发行版推荐使用的存储类型。 devicemapper:时Centos和RHEL的推荐存储驱动程序,因为之前的内核版本不支持overlay2,但是当前较新版本的Centos和RHEL现在已经支持哦verlay2,因此推荐按使用overlay2。 ZFS:目前没有广发使用。 vfs:用于测试环境,适用于无法使用copy-on-write文件系统的情况。此存储驱动程序的性能很差,通常不建议用于生产。
容器的创建与管理过程:
通信流程: 1.docker通过grpc和containerd模块通信(runc)交换,dockerd和containerd通信的socket文件:/run/containerd/container.sock。 2.containerd在dockerd启动时被启动,然后containerd启动grpc请求监听,containerd处理grpc请求,根据请求做相应动作。 3.若是创建容器,containerd拉起要给container-shim容器进程,并进行相应的创建操作。 4.container-shim被拉起后,start/exec/create拉起runc进程,通过exit、control文件和containerd通信,通过父子进程关系和SIGCHLD(信号)监控容器中进程状态。 5.在真个容器生命周期中,containerd通过epoll监控容器3文件,监控容器事件。
docker镜像加速配置:
国内下载国外的镜像有时会很慢,因此可以更改docker配置文件添加要给加速器。可以通过加速器道道加速下载镜像的目的。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)