Docker容器技术包含三个概念: 镜像,容器,仓库.

接下来我们会从命令方面开始讲解Docker容器技术!

镜像(image)

镜像是Docker学习的基础,后续的一切操作都需要基于镜像。现在我们来讲讲关于镜像的操作命令吧.

获取镜像

docker pull 服务名:版本号

如果我们要从docker的镜像网站上获取一个nginx镜像,我们只需要执行以下命令

docker pull nginx
#或是
docker pull nginx:latest

在没有指定要拉取镜像的版本时,默认docker会拉取最新版本的镜像 

如果我们要拉取1.23版本的nginx,可以执行以下命令

docker pull nginx:1.23

列出镜像

在获取镜像后,我们需要查看docker中已下载的镜像

查看全部镜像

docker images

这样就可以列出所有在docker本地的镜像了

查看指定镜像

假如我们的docker中有非常多的镜像,我们又不想花时间去找我们想要的镜像,可以使用以下命令

docker images 镜像名

#例如我们要找nginx的镜像
docker images nginx

关于docker images的参数选项

我们可以通过docker images --help来查看该命令的所有参数选项

[root@localhost ~]# docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs

--filter(可以简写为-f):用作过滤参数使用

#显示在下载完nginx镜像之后下载的镜像
docker images -f since=nginx
#显示在下载nginx镜像之前下载的镜像
docker images -f before=nginx

--format:格式化,用作格式化镜像展示结果使用的 

#格式化输出之前
[root@localhost ~]# docker images
REPOSITORY                      TAG                        IMAGE ID       CREATED         SIZE
busybox                         latest                     beae173ccac6   8 months ago    1.24MB
nginx                           latest                     605c77e624dd   8 months ago    141MB
elasticsearch                   7.14.0                     e347b2b2d6c1   13 months ago   1.04GB
kibana                          7.14.0                     58dffcbc8caa   13 months ago   1.33GB



#格式化输出之后
#只显示  镜像ID   镜像大小   镜像名  table的含义是将该输出以表格形式显示,看起来更加的整齐
[root@localhost ~]# docker images --format "table {{.ID}}\t{{.Size}}\t{{.Repository}}"
IMAGE ID       REPOSITORY                      SIZE
beae173ccac6   busybox                         1.24MB
605c77e624dd   nginx                           141MB
e347b2b2d6c1   elasticsearch                   1.04GB
58dffcbc8caa   kibana                          1.33GB

--no-trunc:不截断输出(了解就行,不常用)

#不截断输出主要体现在镜像ID上,它会将镜像ID的信息完整显示出来
[root@localhost ~]# docker images --no-trunc
REPOSITORY                      TAG                        IMAGE ID                                                                  CREATED         SIZE
busybox                         latest                     sha256:beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a   8 months ago    1.24MB
nginx                           latest                     sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85   8 months ago    141MB
elasticsearch                   7.14.0                     sha256:e347b2b2d6c139e00250755db2a77c993176bdbbc5daecc5c0c3a3b04004b186   13 months ago   1.04GB
kibana                          7.14.0                     sha256:58dffcbc8caa43c7bb0084fb51b29706bc0dca39405b39b67f4923988b11c527   13 months ago   1.33GB

-q:只显示镜像id

#只显示镜像ID
[root@localhost ~]# docker images -q
beae173ccac6
605c77e624dd
e347b2b2d6c1
58dffcbc8caa

查看镜像,容器,数据卷所占用的磁盘空间

docker system df

执行后结果如下:

[root@localhost ~]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          21        12        4.526GB   1.874GB (41%)
Containers      22        6         932.4MB   124.7MB (13%)
Local Volumes   9         9         48B       0B (0%)
Build Cache     0         0         0B        0B

运行镜像

docker run [选项] 镜像ID[镜像名:镜像版本]

常用选项:

-d:后台运行

-i:交互模式启动

-t:终端

-p:建立端口映射,将docker容器内部的服务端口映射到宿主机上.

-p  主机端口:容器内端口

案例:

接到领导要求,我们需要在后台启动一个nginx容器,并且需要将nginx容器的端口映射到我们主机的80端口

docker run -d -p 80:80 nginx

在完成上面的案例后,领导觉得不错,但是nginx的默认页面太丑了,需要我们对nginx的默认网页进行修改,修改内容为 "I Love Docker!!!"

#先后台启动nginx并建立端口映射

docker run -d -p 80:80 nginx

#使用linux终端格式来对容器内部进行交互设置

[root@localhost ~]# docker exec -it ba4f3a6b72ae /bin/bash
root@ba4f3a6b72ae:/# echo "<h1 style='text-align:center'>I Love Docker</h1>">/usr/share/nginx/html/index.html 
root@ba4f3a6b72ae:/# exit
exit
[root@localhost ~]#

访问刷新浏览器,发现nginx的默认页面变为居中的"I Love Docker"

ctf 容器镜像格式 容器镜像标准_ctf 容器镜像格式

 

暂停容器运行

如果我们想要停止容器运行,可以使用以下命令

docker stop 镜像ID

运行暂停容器

docker start 镜像ID

删除暂停容器

如果想要将容器删除,可以使用以下命令:

docker rm 镜像ID

查看已运行的容器

#查看现在正在运行的容器信息
docker ps

#查看容器运行历史,就是显示所有运行过的容器(曾经被运行过的容器也会被显示)
docker ps -a

删除镜像(rmi)

说完了获取和查看镜像,接下来说说删除镜像操作 

#删除镜像,个人推荐删除时用镜像ID
docker rmi 镜像ID[镜像名:镜像版本]

镜像ID就像是人的身份证,是唯一的标识,所以在删除镜像的时候使用镜像ID会更加的方便.

批量删除镜像

如果想要一次性删除多个镜像,可以使用以下命令实现

#删除所有nginx镜像
docker rmi $(docker images -q nginx)

以下命令慎用,其伤害等级不亚于rm -rf /* 

#删除所有已下载镜像
docker rmi $(docker images -q)

如果想要删除一个正在运行的容器,直接删除会报错

[root@localhost ~]# docker rmi nginx
Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container 41e3272f5668 is using its referenced image 605c77e624dd

 使用 docker rmi -f 可以对正在运行的容器进行强制删除

-f:强制删除

[root@localhost ~]# docker rmi -f nginx
Untagged: nginx:latest
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85

以上就是关于Docker容器镜像基础的所有内容,欢迎评论指导!!