docker基础用法
文章目录
什么是docker
docker中的容器:
lxc –> libcontainer –> runC
OCI&OCF
OCI
开发容器倡议
由Linux基金会主导于2015年6月创立
旨在围绕容器格式和运行时制定一个开放的工业化标准
包含两种规格
运行时规范
镜像规范
OCF
断续器
开放容器格式
runC 是一个 CLI 工具,用于根据 OCI 规范生成和运行容器
容器作为 runC 的子进程启动,可以嵌入到各种其他系统中,而无需运行守护程序
runC 建立在 libcontainer 之上,同样的容器技术为数百万个 Docker 引擎安装提供支持
docker提供了一个专门容纳容器镜像的点:https://hub.docker.com
docker架构

docker镜像与镜像仓库
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序
文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的
docker对象
使用 Docker 时,您正在创建和使用映像、容器、网络、卷、插件和其他对象。
图像
映像是一个只读模板,其中包含有关创建 docker 容器的说明。
通常,一个映像基于另一个映像,并具有一些额外的自定义。
您可以创建自己的映像,也可以仅使用其他人创建并在注册表中发布的映像。
器皿
控制器是映像的可运行实例。
您可以使用 Docker API 或 CLI 创建、运行、停止、移动或删除容器。
可以将容器连接到一个或多个网络,将存储附加到该容器,甚至可以基于其当前状态创建新映像
安装及使用docker
docker安装
docker加速
使用 Docker 时,您正在创建和使用映像、容器、网络、卷、插件和其他对象。
图像
映像是一个只读模板,其中包含有关创建 docker 容器的说明。
通常,一个映像基于另一个映像,并具有一些额外的自定义。
您可以创建自己的映像,也可以仅使用其他人创建并在注册表中发布的映像。
器皿
控制器是映像的可运行实例。
您可以使用 Docker API 或 CLI 创建、运行、停止、移动或删除容器。
可以将容器连接到一个或多个网络,将存储附加到该容器,甚至可以基于其当前状态创建新映像
[root@localhost ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors":[https://1swrs873.mirror.aliyuncs.com]
}
docker常用操作
| docker search | 在 Docker 中心搜索映像 |
|---|---|
| docker pull | 从注册表中拉取镜像或存储库 |
| docker images | 列出镜像 |
| docker create | 创建新的连接器 |
| docker start | 启动一个或多个已停止的容器 |
| docker run | 在新容器中运行命令 |
| docker attach | 连接到运行容器 |
| docker ps | 列出容器 |
| docker logs | 获取容器日志 |
| docker restart | 重新启动容器 |
| docker stop | 停止一个或多个正在运行的容器 |
| docker kill | 杀死一个或多个正在运行的命令 |
| docker rm | 删除容器 |
| docker exec | 在运行的容器中运行命令 |
| dcoker info | 显示系统范围的信息 |
| docker inspect | 返回有关docker对象的低级信息 |
docker search 从docker查找镜像
[root@localhost ~]# docker search httpd
NAME DESCRIPTION STARS OF
FICIAL AUTOMATED
httpd The Apache HTTP Server Project 4106 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 44
centos/httpd 35
[OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2
[OK]
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 2
hypoport/httpd-cgi httpd-cgi 2
[OK]
nnasaki/httpd-ssi SSI enabled Apache 2.4 on Alpine Linux 1
dockerpinata/httpd 1
jonathanheilmann/httpd-alpine-rewrite httpd:alpine with enabled mod_rewrite 1
[OK]
inanimate/httpd-ssl A play container with httpd, ssl enabled, an… 1
[OK]
centos/httpd-24-centos8 1
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]
publici/httpd httpd:latest 1
[OK]
httpdocker/kubia 0
patrickha/httpd-err 0
e2eteam/httpd 0
amd64/httpd The Apache HTTP Server Project 0
manageiq/httpd_configmap_generator Httpd Configmap Generator 0
[OK]
manasip/httpd 0
ppc64le/httpd The Apache HTTP Server Project 0
httpdss/archerysec ArcherySec repository 0
[OK]
paketobuildpacks/httpd 19022021/httpd-connection_test Thishttpdimagewill test the connectivity…0
sandeep1988/httpd-new httpd-new
docker pull 从镜像仓库中拉取镜像
[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
1efc276f4ff9: Pull complete
aed046121ed8: Pull complete
4340e7be3d7f: Pull complete
80e368ef21fc: Pull complete
80cb79a80bbe: Pull complete
Digest: sha256:343452ec820a5d59eb3ab9aaa6201d193f91c3354f8c4f29705796d9353d4cc6
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
docker images 列出本地镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest dabbfbe0c57b 7 months ago 144MB
[root@localhost ~]#
docker create 创建一个新的容器但不启动它
[root@localhost ~]# docker create --name web -p 80:80 httpd
99c8609a1b95ea324540fc703c230b6cd3944b0253c6e227683308c6503c7997
[root@localhost ~]#
docker ps 列出容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c8609a1b95 httpd "httpd-foreground" About a minute ago Created web
[root@localhost ~]#
docker start 启动容器 可使用名字 id
[root@localhost ~]# docker start web
web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c8609a1b95 httpd "httpd-foreground" 3 minutes ago Up 34 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
[root@localhost ~]#
docker stop 关闭容器 可以使用名字 id
[root@localhost ~]# docker stop 99c8609a1b95
99c8609a1b95
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
docker restart 重启容器
[root@localhost ~]# docker restart web
web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c8609a1b95 httpd "httpd-foreground" 7 minutes ago Up 7 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp web
docker kill 杀死运行中的容器 stop正常退出 kill强制关闭
[root@localhost ~]# docker kill web
web
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99c8609a1b95 httpd "httpd-foreground" 9 minutes ago Exited (137) 12 seconds ago web
[root@localhost ~]#
docker logs 查看日志
[root@localhost ~]# docker logs web
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
[Wed Aug 03 15:58:11.866001 2022] [mpm_event:notice] [pid 1:tid 140232533048640] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Wed Aug 03 15:58:11.866106 2022] [core:notice] [pid 1:tid 140232533048640] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Aug 03 16:00:56.414037 2022] [mpm_event:notice] [pid 1:tid 140232533048640] AH00492: caught SIGWINCH, shutting down gracefully
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
[Wed Aug 03 16:02:29.256279 2022] [mpm_event:notice] [pid 1:tid 140615301913920] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Wed Aug 03 16:02:29.256405 2022] [core:notice] [pid 1:tid 140615301913920] AH00094: Command line: 'httpd -D FOREGROUND'
docker rm 删除一个或多个容器 运行时不可以删除
docker run 创建一个新的容器并运行一个命令
[root@localhost ~]# docker run -it --name test busybox /bin/sh
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
/ # exit
docker attach 连接到正在运行中的容器
[root@localhost ~]# docker start test
test
[root@localhost ~]# docker attach test
/ # exit
docker exec 在运行的容器中执行命令
[root@localhost ~]# docker start test
test
[root@localhost ~]# docker exec -it test /bin/sh
/ # exit
[root@localhost ~]#
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3736976f08ce busybox "/bin/sh" 4 minutes ago Up About a minute test
99c8609a1b95 httpd "httpd-foreground" 16 minutes ago Exited (137) 7 minutes ago web
[root@localhost ~]#
docker info 显示系统范围的信息
[root@localhost ~]# 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: 2
Running: 1
Paused: 0
Stopped: 1
Images: 2
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.runc.v2 io.contain
docker inspect 返回有关Docker对象的低级信息
[root@localhost ~]# docker inspect 99c8609a1b95
[
{
"Id": "99c8609a1b95ea324540fc703c230b6cd3944b0253c6e227683308c6503c7997",
"Created": "2022-08-03T15:54:55.280855953Z",
"Path": "httpd-foreground",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "",
"StartedAt": "2022-08-03T16:02:29.247527711Z",
"FinishedAt": "2022-08-03T16:03:42.985827911Z"
},
"Image": "sha256:dabbfbe0c57b6e5cd4bc089818d3f664acfad496dc741c9a501e72d15e803b34",
"ResolvConfPath": "/var/lib/docker/containers/99c8609a1b95ea324540fc703c230b6cd3944b0253c6e227683308c6503c7997/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/99c8609a1b95ea324540fc703c230b6cd3944b0253c6e227683308c6503c7997/hostname",
"HostsPath": "/var/lib/docker/containers/99c8609a1b95ea324540fc703c230b6cd3944b0253c6e227683308c6503c7997/hosts",
"LogPath": "/var/lib/docker/containers/99c8609a1b95ea324540fc703c230b6cd3944b0253c6e227683308c6503c7997/99c8609a1b95ea324540fc703c230b6cd3944b0253c6e227683308c6503c7997-json.log",
"Name": "/web",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
docker rm 删除一个或多个容器 运行时不可以删除
[root@localhost ~]# docker stop test
test
[root@localhost ~]# docker rm test
test
docker event st

docker镜像管理基础
镜像的概念
镜像可以理解为应用程序的集装箱,而docker用来装卸集装箱。
docker镜像含有启动容器所需要的文件系统及其内容,因此,其用于创建并启动容器。
docker镜像采用分层构建机制,最底层为bootfs,其上为rootfs
bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以节约内存资源
rootfs:位于bootfs之上,表现为docker容器的根文件系统
传统模式中,系统启动之时,内核挂载rootfs会首先将其挂载为“只读”模式,完整性自检完成后将其重新挂载为读写模式
docker中,rootfs由内核挂载为“只读”模式,而后通过“联合挂载”技术额外挂载一个“可写”层
注意:当删除容器时,这个容器自有的“可写”层会一起被删除

docker镜像层

位于下层的镜像称为父镜像(parrent image),最底层的称为基础镜像(base image);
最上层为“可读写”层,其下的均为“只读”层。
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上保存,供其他人员使用,例如“部署”到生产环境。

3342




被折叠的 条评论
为什么被折叠?



