目录
Docker发展史
2010年,几个热爱IT的年轻人,在美国成立了一家公司叫做dotCloud,主要做一些Paas的云计算服务和LXC有关的容器技术赚钱盈利,他们将自己的容器技术命名为Docker!
Docker刚刚诞生的时候,并没有引起行业的注意!这个时候 dotCloud公司难以在行业中存活,所以这几个年轻人选择开放Docker源代码来引起注意!
2013年,Docker开源!越来越多的人发现docker的优点,在这一段时间docker每个月都会更新一个版本。
2014年4月9日,Docker1.0发布!
docker基本用法
命令 | 功能 |
docker search | 在Docker Hub上搜索映像 |
docker pull | 从注册表中提取映像或存储库 |
docker images | 图片列表 |
docker create | 创建一个新的容器 |
docker start | 启动一个或多个停止的容器 |
docker run | 在新容器中运行命令 |
docker attach | 连接到运行的容器 |
docker ps | 列表容器 |
docker logs | 获取容器日志 |
docker restart | 重启一个容器 |
docker stop | 停止一个或多个运行中的容器 |
docker kill | 杀死一个或多个运行中的容器 |
docker rm | 移除onne或多个容器 |
docker exec | 在运行的容器中运行命令 |
docker info | 显示整个系统的信息 |
docker inspect | 返回Docker对象的低级信息 |
docker安装
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1919 100 1919 0 0 13514 0 --:--:-- --:--:-- --:--:-- 13514
[root@zzh ~]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@zzh ~]# ls
anaconda-ks.cfg docker-ce.repo
[root@zzh ~]# mv docker-ce.repo /etc/yum.repos.d/
[root@zzh ~]# ls /etc/yum.repos.d/
CentOS-Stream-AppStream.repo CentOS-Stream-Debuginfo.repo CentOS-Stream-HighAvailability.repo CentOS-Stream-PowerTools.repo docker-ce.repo
CentOS-Stream-BaseOS.repo CentOS-Stream-Extras.repo CentOS-Stream-Media.repo CentOS-Stream-RealTime.repo
[root@zzh ~]# yum -y install docker-ce
Failed to set locale, defaulting to C.UTF-8
CentOS Stream 8 - AppStream 6.4 kB/s | 4.4 kB 00:00
CentOS Stream 8 - AppStream 11 MB/s | 24 MB 00:02
CentOS Stream 8 - BaseOS 622 B/s | 3.9 kB 00:06
[root@zzh ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@zzh ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-08-07 06:54:15 EDT; 6s ago
Docs: https://docs.docker.com
Main PID: 29076 (dockerd)
Tasks: 9
Memory: 38.9M
CGroup: /system.slice/docker.service
└─29076 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Aug 07 06:54:14 zzh dockerd[29076]: time="2022-08-07T06:54:14.184199872-04:00" level=warning msg="Your kernel does not support cgroup blkio weight"
Aug 07 06:54:14 zzh dockerd[29076]: time="2022-08-07T06:54:14.184241200-04:00" level=warning msg="Your kernel does not support cgroup blkio weight_device"
Aug 07 06:54:14 zzh dockerd[29076]: time="2022-08-07T06:54:14.184455962-04:00" level=info msg="Loading containers: start."
Aug 07 06:54:15 zzh dockerd[29076]: time="2022-08-07T06:54:15.305977590-04:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be>
Aug 07 06:54:15 zzh dockerd[29076]: time="2022-08-07T06:54:15.426146199-04:00" level=info msg="Firewalld: interface docker0 already part of docker zone, returning"
Aug 07 06:54:15 zzh dockerd[29076]: time="2022-08-07T06:54:15.547213737-04:00" level=info msg="Loading containers: done."
Aug 07 06:54:15 zzh dockerd[29076]: time="2022-08-07T06:54:15.563278104-04:00" level=info msg="Docker daemon" commit=a89b842 graphdriver(s)=overlay2 version=20.10.17
Aug 07 06:54:15 zzh dockerd[29076]: time="2022-08-07T06:54:15.563481916-04:00" level=info msg="Daemon has completed initialization"
Aug 07 06:54:15 zzh systemd[1]: Started Docker Application Container Engine.
Aug 07 06:54:15 zzh dockerd[29076]: time="2022-08-07T06:54:15.583126826-04:00" level=info msg="API listen on /var/run/docker.sock"
[root@zzh ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
docker加速
[root@zzh ~]# vim /etc/docker/daemon.json //在阿里云中找到自己的加速器
{
"registry-mirrors": ["https://chg2yeus.mirror.aliyuncs.com"]
}
[root@zzh ~]# systemctl restart docker //重启
[root@zzh ~]# 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:03:11 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:29 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
[root@zzh ~]# 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: xfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
Default Runtime: runc
Init Binary: docker-init
containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc version: v1.1.2-0-ga916309
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 4.18.0-257.el8.x86_64
Operating System: CentOS Stream 8
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 8.312GiB
Name: zzh
ID: DL22:XNO7:VJAX:GPQV:2S2O:UCAN:2RIZ:ZXYV:6AHZ:N5UR:3USS:GYB7
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://chg2yeus.mirror.aliyuncs.com/
Live Restore Enabled: false
[root@zzh ~]# docker pull centos //拉取镜像
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
docker常用操作
[root@zzh ~]# docker search httpd //在Docker Hub上搜索映像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 4109 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 44
centos/httpd 35 [OK]
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 2
hypoport/httpd-cgi httpd-cgi 2 [OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
dockerpinata/httpd 1
nnasaki/httpd-ssi SSI enabled Apache 2.4 on Alpine Linux 1
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
inanimate/httpd-ssl A play container with httpd, ssl enabled, an… 1 [OK]
publici/httpd httpd:latest 1 [OK]
dariko/httpd-rproxy-ldap Apache httpd reverse proxy with LDAP authent… 1 [OK]
manageiq/httpd Container with httpd, built on CentOS for Ma… 1 [OK]
centos/httpd-24-centos8 1
manasip/httpd 0
amd64/httpd The Apache HTTP Server Project 0
patrickha/httpd-err 0
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
paketobuildpacks/httpd 0
httpdss/archerysec ArcherySec repository 0 [OK]
httpdocker/kubia 0
sandeep1988/httpd-new httpd-new 0
e2eteam/httpd 0
19022021/httpd-connection_test This httpd image will test the connectivity … 0
sherazahmedvaival/httpd-php-fpm74 0
[root@zzh ~]# docker pull busybox //拉取镜像
Using default tag: latest
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
[root@zzh ~]# docker images //列出镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest beae173ccac6 7 months ago 1.24MB
centos latest 5d0da3dc9764 10 months ago 231MB
[root@zzh ~]# docker create busybox //创建容器
6a2158b682a53b9ed20866edf0cdc708d1ee4c5683ad7c609bbd8411611dd156
[root@zzh ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a2158b682a5 busybox "sh" 7 seconds ago Created upbeat_austin
[root@zzh ~]# docker start a40586c08b1f //启动容器
Error response from daemon: No such container: a40586c08b1f
Error: failed to start containers: a40586c08b1f
[root@zzh ~]# docker start 6a2158b682a5
6a2158b682a5
[root@zzh ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a2158b682a5 busybox "sh" About a minute ago Exited (0) 9 seconds ago upbeat_austin
[root@zzh ~]# docker run -d --name zzh -p 80:80 httpd //在新容器中运行命令
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete
dcc4698797c8: Pull complete
41c22baa66ec: Pull complete
67283bbdd4a0: Pull complete
d982c879c57e: Pull complete
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
9524fe64ef685a2844c1d156277baab6fd2b8bb3a24129faf805a2fef12f7b7c
[root@zzh ~]# docker logs zzh //查看日志
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Sun Aug 07 11:08:07.524877 2022] [mpm_event:notice] [pid 1:tid 140388697234752] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Sun Aug 07 11:08:07.524988 2022] [core:notice] [pid 1:tid 140388697234752] AH00094: Command line: 'httpd -D FOREGROUND'
[root@zzh ~]# docker restart zzh //docker重启、停止web容器
zzh
[root@zzh ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6fa86c386173 httpd "httpd-foreground" 55 seconds ago Up 18 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp zzh
[root@zzh ~]# docker stop zzh
zzh
[root@zzh ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@zzh ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f8379d715606 httpd "httpd-foreground" 6 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp wrq
[root@zzh ~]# docker kill f8379d715606 //杀死容器
f8379d715606
[root@zzh ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@zzh ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6fa86c386173 httpd "httpd-foreground" 55 seconds ago Up 18 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp zzh
[root@zzh ~]# docker rm -f zzh //强制删除容器
zzh
[root@zzh ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker存储驱动
docker提供了多种存储驱动来实现不同的方式存储镜像,下面是常用的几种存储驱动:
AUFS
OverlayFS
Devicemapper
Btrfs
VFS
AUFS
AUFS(AnotherUnionFS)是一种Union FS,是文件级的存储驱动。AUFS是一个能透明覆盖一个或多个现有文件系统的层状文件系统,把多层合并成文件系统的单层表示。简单来说就是支持将不同目录挂载到同一个虚拟文件系统下的文件系统。这种文件系统可以一层一层地叠加修改文件。无论底下有多少层都是只读的,只有最上层的文件系统是可写的。当需要修改一个文件时,AUFS创建该文件的一个副本,使用CoW将文件从只读层复制到可写层进行修改,结果也保存在可写层。在Docker中,底下的只读层就是image,可写层就是Container。
AUFS文件系统据说有3W行代码,而ext4文件系统却只有4000-5000行左右代码,这些代码是要被整合进内核的,后来AUFS申请要被合并进内核代码的时候,linuz觉得它这代码太过臃肿,于是拒绝了。因此AUFS这个文件系统一直以来就不是linux内核中自有的文件系统,想用AUFS这个文件系统的话,必须自己向内核打补丁并去编译使用它,但redhat系列的操作系统一向以稳定著称,不会干这种出格的事,所以在redhat系列操作系统中使用AUFS并无可能。而ubuntu上的docker默认使用的就是AUFS。
OverlayFS
Overlay是Linux内核3.18后支持的,也是一种Union FS,和AUFS的多层不同的是Overlay只有两层:一个upper文件系统和一个lower文件系统,分别代表Docker的镜像层和容器层。当需要修改一个文件时,使用CoW将文件从只读的lower复制到可写的upper进行修改,结果也保存在upper层。在Docker中,底下的只读层就是image,可写层就是Container。目前最新的OverlayFS为Overlay2。
AUFS和Overlay都是联合文件系统,但AUFS有多层,而Overlay只有两层,所以在做写时复制操作时,如果文件比较大且存在比较低的层,则AUSF会慢一些。而且Overlay并入了linux kernel mainline,AUFS没有。目前AUFS已基本被淘汰。
DeviceMapper
Device mapper是Linux内核2.6.9后支持的,提供的一种从逻辑设备到物理设备的映射框架机制,在该机制下,用户可以很方便的根据自己的需要制定实现存储资源的管理策略。AUFS和OverlayFS都是文件级存储,而Device mapper是块级存储,所有的操作都是直接对块进行操作,而不是文件。Device mapper驱动会先在块设备上创建一个资源池,然后在资源池上创建一个带有文件系统的基本设备,所有镜像都是这个基本设备的快照,而容器则是镜像的快照。所以在容器里看到文件系统是资源池上基本设备的文件系统的快照,并没有为容器分配空间。当要写入一个新文件时,在容器的镜像内为其分配新的块并写入数据,这个叫用时分配。当要修改已有文件时,再使用CoW为容器快照分配块空间,将要修改的数据复制到在容器快照中新的块里再进行修改。
OverlayFS是文件级存储,Device mapper是块级存储,当文件特别大而修改的内容很小,Overlay不管修改的内容大小都会复制整个文件,对大文件进行修改显然要比小文件要消耗更多的时间,而块级无论是大文件还是小文件都只复制需要修改的块,并不是整个文件,在这种场景下,显然device mapper要快一些。因为块级的是直接访问逻辑盘,适合IO密集的场景。而对于程序内部复杂,大并发但少IO的场景,Overlay的性能相对要强一些。
docker registry
启动容器时,docker daemon会试图从本地获取相关的镜像,本地镜像不存在时,其将从Registry中下载该镜像并保存到本地。
Registry用于保存docker镜像,包括镜像的层次结构和元数据。用户可以自建Registry,亦可使用官方的Docker Hub。
docker registry的分类:
Sponsor Registry:第三方的Registry,供客户和Docker社区使用
Mirror Registry:第三方的Registry,只让客户使用
Vendor Registry:由发布docker镜像的供应商提供的registry
Private Registry:通过设有防火墙和额外的安全层的私有实体提供的registry
docker registry的组成:
Repository
由某特定的docker镜像的所有迭代版本组成的镜像仓库
一个Registry中可以存在多个Repository
Repository可分为“顶层仓库”和“用户仓库”
用户仓库名称格式为“用户名/仓库名”
每个仓库可包含多个Tag(标签),每个标签对应一个镜像
Index
维护用户帐户、镜像的检验以及公共命名空间的信息
相当于为Registry提供了一个完成用户认证等功能的检索接口
Docker Registry中的镜像通常由开发人员制作,而后推送至“公共”或“私有”Registry上保存,供其他人员使用,例如“部署”到生产环境。