Docker学习笔记(二)-- 镜像

290 阅读10分钟

本章介绍一些镜像的基本操作。镜像时docker的核心概念之一,也是创建容器的前提。

查看镜像

docker images  #查看所有本地主机上的镜像
​
​
REPOSITORY    TAG                       IMAGE ID       CREATED        SIZE
flink         1.13.2-scala_2.12-java8   fd24aa55e89e   30 hours ago   625MB
mysql         5.7                       6c20ffa54f86   9 days ago     448MB
hello-world   latest                    d1165f221234   5 months ago   13.3kB
​
# 解释
REPOSITORY  # 镜像仓库源
TAG                 # 镜像的标签(版本)
IMAGE ID           # 镜像的ID,唯一标识一个镜像
CREATED           # 镜像的创建时间
SIZE # 镜像的大小

搜索镜像

可以直接在dockerHub上搜索需要的镜像

这里以mysql为例

image-20200616173050756.png

点击进入可以看到有哪些版本可供下载。

image-20210902195744495.png

也能够直接用命令进行搜索

#搜索mysql镜像 OFFICEIAL标注为官方镜像
docker search mysql  
​
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11361     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4313      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   844                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   91                   
...
docker search --help 查看帮助文档
Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output

添加一些过滤条件

# 搜索出Stars大于3000的
docker search mysql --filter=STARS=3000 
​
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11361     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4313      [OK]  

下载镜像

尝试下载一个版本为5.7的mysql和最新版本的mysql镜像,软件支持什么版本可以直接在官网进行查看。上文的图片展示了mysql支持的版本。

docker pull [OPTIONS] NAME[:TAG|@DIGEST] # 如果不指定版本,那么默认下载latest的版本,即最新版
​
docker pull mysql:5.7 # 下载5.7版本的mysql镜像 
docker pull mysql # 下载最新的mysql镜像 
#docker下载使用的是分层下载,这样对于不同版本的容器,只要下载不同的层,减少磁盘使用。

[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
e1acddbe380c: Pull complete 
bed879327370: Pull complete 
03285f80bafd: Pull complete 
ccc17412a00a: Pull complete 
1f556ecc09d1: Pull complete 
adc5528e468d: Pull complete 
1afc286d5d53: Pull complete 
4d2d9261e3ad: Pull complete 
ac609d7b31f8: Pull complete 
53ee1339bc3a: Pull complete 
b0c0a831a707: Pull complete 
Digest: sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

#下载一个最新版本的mysql
[root@localhost ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
e1acddbe380c: Already exists #已存在的层
bed879327370: Already exists #已存在的层
03285f80bafd: Already exists #已存在的层
ccc17412a00a: Already exists #已存在的层
1f556ecc09d1: Already exists #已存在的层
adc5528e468d: Already exists #已存在的层
1afc286d5d53: Already exists #已存在的层
6c724a59adff: Pull complete  #需要新下载的层
0f2345f8b0a3: Pull complete #需要新下载的层
c8461a25b23b: Pull complete #需要新下载的层
3adb49279bed: Pull complete #需要新下载的层
77f22cd6c363: Pull complete #需要新下载的层
Digest: sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

这里先下载了5.7版本的Mysql,而后又下载了最新版本(latest)版本的mysql,两个版本的mysql层存在重叠。所以在下载最新版本的mysql时,由于存在已经相同的层,只需要下载不同的部分即可。节省了很多空间。

删除镜像

# 删除docker的镜像文件
docker rmi IMAGE[IMAGE...] 
[root@localhost ~]# docker images   #先查看有那些镜像
REPOSITORY    TAG                       IMAGE ID       CREATED        SIZE
mysql         5.7                       6c20ffa54f86   9 days ago     448MB
mysql         latest                    5a4e492065c7   9 days ago     514MB
hello-world   latest                    d1165f221234   5 months ago   13.3kB
​
#使用镜像 名称+TAG 来删除。latest表示最新的版本
#这里删除自己特有的层,与mysql5.7公用的层不会删除。
[root@localhost ~]# docker rmi mysql:latest  
Untagged: mysql:latest
Untagged: mysql@sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926
Deleted: sha256:5a4e492065c722ec8cc7413552bafc6fd5434c5ad90797e898ccc4e347e21aa5
Deleted: sha256:816bf92c28337cd97393cb1fab7697398c9b657baa8e80ad394e1193bdd7c323
Deleted: sha256:4d9cad11f64589b397b1033d6f14cea1794efbee0a9923932fb3394a67cb9812
Deleted: sha256:d8cc28a31b469770ffe2239a29aaef8745fedc4df3c545b1615ba864796709a3
Deleted: sha256:c65a377e07f1205f3c28f7067d2ac4da3a65fbe7c1b4606b48028db3818d2f4c
Deleted: sha256:7dcd840f1d4be1a565f470453489932a17c62ed36b959d7839f004606ba22f0b
​
# 也能直接使用镜像ID进行删除
[root@localhost ~]#  docker rmi 6c20ffa54f86
Untagged: mysql:5.7
Untagged: mysql@sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
Deleted: sha256:6c20ffa54f8674203e91e3225e489aa505fa04b8d482954a8b6d7414842c6de4
Deleted: sha256:7ecf077dd9d6725f0e594b9cd5eb214ad1b3764fa75bf5940ab08b05b8bae184
Deleted: sha256:c28edab85681de37f9d2a75dde941a20d2e1ac6e4efb7dee391913d3ec722fef
Deleted: sha256:6c11b187da1ecb65ce10b25f54354f3515ce9d26fabcd859cdb43dbd625df907
Deleted: sha256:d0dfc5be0883ed4af9f642a8e4f2312a93c4241ec8e89f22219b0f0a0d26c8d3
Deleted: sha256:1438a7497295aed687ea1875073a1f9faa6a308f787767b952da4a1a21e2c2c6
Deleted: sha256:c728863b4742db55b9630b0129abac718c8c9704e5ed86e8af93dfb47a0f6214
Deleted: sha256:0f12df3ff796b6fb619c63bf2f78f16d1033746e19f75ffc2d8d72ea69242ad4
Deleted: sha256:3072318e40ebff019298a59290e615ad6170a651f3121c0e356dcf3dd7426136
Deleted: sha256:a69e3133dd3c0a81bd439120abb8f98e43c1581629896725b968db906c8fdfac
Deleted: sha256:6d9498fd63c38c4b0b9bbc563087b4bc164b2fb5211832a8d43c60a9692a7f84
Deleted: sha256:f68ef921efae588b3dd5cc466a1ca9c94c24785f1fa9420bea15ecc2dedbe781

删除镜像的时候,由于有分层的原因,不会删除公有的部分。

如果由该镜像创建的容器存在时,镜像无法被删除。

#这里尝试删除安装docker时用来测试的 hello-world 镜像
[root@localhost ~]# docker rmi hello-world:latest
Error response from daemon: conflict: unable to remove repository reference "hello-world:latest" (must force) - container afc239a27ded is using its referenced image d1165f221234

如果想强行删除,可以加上 -f 参数,但不建议这么做。正确的做法是先删除这个镜像创建的容器,再删除这个镜像。

#强制删除镜像
docker rmi -f 
#强制删除hello-world镜像
[root@localhost ~]# docker rmi -f hello-world:latest
Untagged: hello-world:latest
Untagged: hello-world@sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726

提交镜像

基于已有的容器进行创建

基于mysql:5.7镜像,创建一个新的镜像

docker commit # 提交容器成为一个新的副本
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名
#1. 下载5.7镜像 ,并生成一个容器 mysql的密码被设置为root
[root@localhost ~]# docker run -d --name test-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:5.7
3d013a5ac23c35afb11af31e4deedbf148f076cbcab843e12a751ca5ed0c585c

# 2. 停止这个mysql容器
[root@localhost ~]# docker stop test-mysql
test-mysql
​
# 3.基于这个容器生成一个新的镜像,叫做new-mysql
[root@localhost ~]# docker commit -m="a new mysql" -a="gn" test-mysql new-mysql
sha256:1b7f4805990822b7b531c30625a529e0c4c93847e24e4c91edd0f2b8b08eea7a
​
# 4.查看所有的镜像,发现多了一个new-mysql
[root@localhost ~]# docker images
REPOSITORY   TAG                       IMAGE ID       CREATED         SIZE
new-mysql    latest                    1b7f48059908   9 seconds ago   448MB
mysql        5.7                       6c20ffa54f86   9 days ago      448MB
​
# 5.这时候以这个new-mysql创建容器,这个mysql的密码就不用设置,自动为root
docker run -d -p 3306:3306 new-mysql

存出和导入

docker save #存出镜像
docker load #导入镜像

例子略

基于Dockerfile创建

后文补充

上传镜像

#该命令能够将镜像上传到镜像仓库,默认会上传到dockerHub仓库。需要先在本地登入dockerHub后才能上传。
docker push 
# 1. 先将镜像规范化,设置版本为1.0
docker tag new-mysql new-mysql:1.0
# 2.上传
docker push 用户名/镜像名称:TAG 

常用命令

docker pull #拉取镜像
​
docker images #列出本地所有镜像
docker image ls 镜像id #查询指定镜像
docker images | grep 镜像名称  #使用docker images  列出所有镜像,然后过滤
​
docker tag 老的镜像名称:TAG 新的镜像名称:latest #将镜像重命名
​
docker rmi 镜像id #删除镜像
docker rmi -f id id id # 删除多个 用空格分隔id
docker rmi -f $(docker images -aq) # 删除所有  # images -aq就是查所有镜像id,从而递归删除
​
docker commit -m="a new mysql" -a="gn" test-mysql new-mysql #基于容器生成一个新的镜像
docker tag new-mysql new-mysql:1.0 #给镜像添加TAG
docker push 用户名/镜像名称:TAG # 上传镜像到仓库