- 1. dockerhub的使用
- 1.1 dockerhub地址:
- 1.2 搜索下载,右侧为下载命令
- 1.3 阿里云镜像加速
- 2. 获取镜像
- 命令: docker pull [仓库的URL]/[名称空间]/[仓库的名称]:[版本号]
- 3. 查看镜像信息
- 4. 为镜像添加tag
- 使用history命令查看镜像历史 docker history repo:tag
- 5. 搜索镜像 语法为docker search [option] keyword
- 支持的命令选项主要包括:
- 输出参数解释
- 6. 删除和清理镜像
- 6.1 使用标签删除镜像
- docker rmi [REPOSITORY:TAG] 或者 imageId docker image rm [REPOSITORY:TAG]
- 7. 清理镜像
- 8. 构建镜像
- 构建镜像一般有三种情况,基于容器导入、基于本地模板导入、基于Dockerfile创建,主要记录基于容器保存镜像和本地保存镜像导入
- 8.1 基于容器保存
- 8.2 基于本地模板导入
- docker import 将docker导出的镜像文件恢复成为一个镜像
- 9. 保存镜像
- 9.1 export 和 import的针对点是容器,将本机的容器导出为镜像包
- 9.2 使用 save 和 load
https://hub.docker.com/
1.2 搜索下载,右侧为下载命令 1.3 阿里云镜像加速1. 访问: https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
2. 配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://lr3e0lji.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
2. 获取镜像
命令: docker pull [仓库的URL]/[名称空间]/[仓库的名称]:[版本号]
镜像是运行容器的模板,官方Docker Hub 仓库已经提供了许多镜像共开发者使用。 如果我们需要获取某个镜像则可以去docker仓库下载所需的镜像 docker pull [仓库的URL]/[名称空间]/[仓库的名称]:[版本号] eg: docker pull nginx:1.173. 查看镜像信息
docker images docker images ls
使用docker images 或 docker images ls命令可以列举本地主机上已有镜像的基本信息
子命令主要支持如下选项:
-a : 列出所有(包括临时文件)镜像文件 --digests=true|false:列出镜像的数字摘要值 [root@i-jnsxhfco local]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.17 9beeba249f3e 13 months ago 127MB [root@i-jnsxhfco local]# docker images -a --digests=true REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE nginx 1.17 sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699 9beeba249f3e 13 months ago 127MB -q : 仅显示ID信息 [root@i-jnsxhfco local]# docker images -q 9beeba249f3e4. 为镜像添加tag
为了方便后续工作中使用特定的镜像,还可以使用docker tag命令来为本地的镜像添加标签。
docker tag [REPOSITORY:TAG] [aliasName]
[root@i-jnsxhfco local]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.17 9beeba249f3e 13 months ago 127MB [root@i-jnsxhfco local]# docker tag nginx:1.17 my_nginx:1.0 [root@i-jnsxhfco local]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE my_nginx 1.0 9beeba249f3e 13 months ago 127MB nginx 1.17 9beeba249f3e 13 months ago 127MB
使用inspect命令查看详细信息
docker inspect repo:tag
[root@i-jnsxhfco local]# docker inspect nginx:1.17
[
{
"Id": "sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9",
"RepoTags": [
"my_nginx:1.0",
"nginx:1.17"
],
"RepoDigests": [
"nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699"
],
..........
}
]
使用history命令查看镜像历史 docker history repo:tag
[root@i-jnsxhfco local]# docker history nginx:1.17 IMAGE CREATED CREATED BY SIZE COMMENT 9beeba249f3e 13 months ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B5. 搜索镜像 语法为docker search [option] keyword 支持的命令选项主要包括:13 months ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B 13 months ago /bin/sh -c #(nop) EXPOSE 80 0B 13 months ago /bin/sh -c ln -sf /dev/stdout /var/log/nginx… 22B 13 months ago /bin/sh -c set -x && addgroup --system -… 57.6MB 13 months ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B 13 months ago /bin/sh -c #(nop) ENV NJS_VERSION=0.3.9 0B 13 months ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.17.10 0B 13 months ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B 13 months ago /bin/sh -c #(nop) CMD ["bash"] 0B 13 months ago /bin/sh -c #(nop) ADD file:7780c81c33e6cc5b6… 69.2MB
-f : 过滤输出内容 ## 搜索官方的镜像 [root@i-jnsxhfco local]# docker search -f is-official=true redis NAME DEscriptION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 9624 [OK] # 搜索被收藏超过8000个的并且关键词包括Redis的镜像 [root@i-jnsxhfco local]# docker search -f stars=8000 redis NAME DEscriptION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 9624 [OK] --limit: 限制输出结果 只查3个 [root@i-jnsxhfco local]# docker search --limit 3 redis NAME DEscriptION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 9624 [OK] bitnami/redis Bitnami Redis Docker Image 184 [OK] rediscommander/redis-commander Alpine image for redis-commander - Redis man… 60 [OK] --no-trunc: 不截断输出结果 desc全部显示了 [root@i-jnsxhfco local]# docker search -f stars=8 --limit 3 --no-trunc redis NAME DEscriptION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that functions as a data structure server. 9624 [OK] bitnami/redis Bitnami Redis Docker Image 184 [OK] rediscommander/redis-commander Alpine image for redis-commander - Redis management tool. 60 [OK]输出参数解释
- NAME: 镜像仓库源的名称 - DEscriptION: 镜像的描述 - OFFICIAL: 是否 docker 官方发布 - stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。 - AUTOMATED: 自动构建。6. 删除和清理镜像 6.1 使用标签删除镜像 docker rmi [REPOSITORY:TAG] 或者 imageId docker image rm [REPOSITORY:TAG]
docker rmi [REPOSITORY:TAG] 或者 imageId docker image rm [REPOSITORY:TAG] 参数: -f : 强制删除镜像。 # docker rmi -f nginx:1.177. 清理镜像
docker image prune 参数: -a :删除所有无用的镜像,不光是临时镜像。 -f :强制删除镜像,而不进行提示。 eg: docker image prune -a -f [root@i-jnsxhfco docker]# docker image prune -a WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N] y Deleted Images: untagged: nginx:1.17 untagged: nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699 deleted: sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9 deleted: sha256:8fb6373b4cca3383756d7fd7843dd92f95827e5f2913609e09a9621dcddb3752 deleted: sha256:8b09841626797a03a9fe5e73aa38aeacf9ff0ce85a3004236ff35234eec3b35c deleted: sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07 Total reclaimed space: 126.8MB [root@i-jnsxhfco docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE8. 构建镜像 构建镜像一般有三种情况,基于容器导入、基于本地模板导入、基于Dockerfile创建,主要记录基于容器保存镜像和本地保存镜像导入 8.1 基于容器保存
命令:
docker [container id] commit 参数: -a 作者信息 -m 提交信息 -p 提交时,暂停容器运行 [root@i-jnsxhfco docker]# docker run -d -it centos /bin/bash Unable to find image 'centos:latest' locally latest: Pulling from library/centos 7a0437f04f83: Pull complete Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 Status: Downloaded newer image for centos:latest d9b98f390a022be13015f1f34922917d056daf695cb3aa2a02b383d4f1f26f5a [root@i-jnsxhfco docker]# [root@i-jnsxhfco docker]# [root@i-jnsxhfco docker]# [root@i-jnsxhfco docker]# [root@i-jnsxhfco docker]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9b98f390a02 centos "/bin/bash" about a minute ago Up about a minute heuristic_curran [root@i-jnsxhfco docker]# docker exec d9b98f390a02 touch test [root@i-jnsxhfco docker]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9b98f390a02 centos "/bin/bash" about a minute ago Up about a minute heuristic_curran [root@i-jnsxhfco docker]# docker commit -m "Add a file" -a "Alvin" d9b98f390a02 sha256:7ae698389b457d4b1d0da802b44344c18343705b77ce4b0f1fd06a133ffd6881 [root@i-jnsxhfco docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE8.2 基于本地模板导入 docker import 将docker导出的镜像文件恢复成为一个镜像7ae698389b45 8 seconds ago 209MB centos latest 300e315adb2f 6 months ago 209MB
docker import centos_7.tar centos:7-new9. 保存镜像
两种方案: export 和 import save 和 load
区别:
两种方案的差别 文件大小不同: export导出的镜像文件体积小于 save 保存的镜像 是否可以对镜像重命名: docker import可以为镜像指定新名称,docker load不能对载入的镜像重命名。 是否可以同时将多个镜像打包到一个文件中: docker export不支持,docker save支持。 是否包含镜像历史 export 导出(import 导入)是根据容器拿到的镜像,再导入时会丢失镜像所有的历史记录和元数据信息(即仅保存容器当时的快照状态),所以无法进行回滚操作。 save 保存(load 加载)的镜像,没有丢失镜像的历史,可以回滚到之前的层(layer)。 建议 可以依据具体使用场景来选择命令 若是只想备份images,使用save、load即可 若是在启动容器后,容器内容有变化,需要备份,则使用export、import9.1 export 和 import的针对点是容器,将本机的容器导出为镜像包
## 使用export保存容器为镜像 [root@i-jnsxhfco docker]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9b98f390a02 centos "/bin/bash" 34 minutes ago Up 34 minutes heuristic_curran [root@i-jnsxhfco docker]# docker export d9b98f390a02 > centosbk.tar [root@i-jnsxhfco docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE9.2 使用 save 和 load7ae698389b45 32 minutes ago 209MB centos latest 300e315adb2f 6 months ago 209MB [root@i-jnsxhfco docker]# docker ps ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d9b98f390a02 centos "/bin/bash" 35 minutes ago Up 35 minutes heuristic_curran [root@i-jnsxhfco docker]# ll total 211460 *-rw-r--r-- 1 root root 216525824 Jun 29 15:39 centosbk.tar* ## 使用import导入包为镜像 [root@i-jnsxhfco docker]# docker import centosbk.tar test/centos:0.01 sha256:cdf9f4a286ef3088b8357d09b52be50581f07d5ec48b321e81bc7890239ff687 [root@i-jnsxhfco docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test/centos 0.01 cdf9f4a286ef 6 seconds ago 0B 为什么导出来的tar文件为空
save 和 load的针对的点是镜像,将本机的镜像导入、导出为镜像包。
save: docker save -o 文件位置/文件名称.tar 镜像ID/或者仓库名称:版本 docker save 镜像ID/或者仓库名称:版本 > 文件位置/文件名称.tar 保存多个: docker save IMAGEID1 Id2 Id3 > all.tar //这种方式load 之后需要手动tag repo和tag的信息 docker save REPO1:TAG1 REPO2:TAG2 > all.tar //这种方式不用 [root@i-jnsxhfco docker_images]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 08502081bff6 5 days ago 105MB centos latest 300e315adb2f 6 months ago 209MB [root@i-jnsxhfco docker_images]# docker save -o /usr/local/docker_images/redis_bk.tar 08502081bff6 [root@i-jnsxhfco docker_images]# ll total 106264 -rw------- 1 root root 108812800 Jun 29 16:00 redis_bk.tar load: docker load < all.tar [root@i-jnsxhfco docker_images]# docker save centos:latest redis:v1 > all.tar [root@i-jnsxhfco docker_images]# docker image prune -a -f Deleted Images: untagged: redis:v1 deleted: sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55 deleted: sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859 untagged: centos:latest deleted: sha256:08502081bff61084d64fc76f0f90ea39b89935cd071d9e12c5374ae191ff53c0 deleted: sha256:ae5155f07c6cd11da80503d9eeee8f6da1733ce844ae793d0a836f87331ccfe8 deleted: sha256:6be98c1f12093b274ce5a8a08aade2e317646a3d796f06bfbb1ceb64a3667d79 deleted: sha256:d39ca9a7caf57ae0172f935bedc3f00eb11793e49736b36894bbdad81c48da76 deleted: sha256:72b2f38a39460857c7f833d9fb5ae1ad23f1454c90bf85040545daab59245690 deleted: sha256:fc6698595ab9165eb0f1e07b0930f45e925ab604b8549f225c93a6f83631ae40 deleted: sha256:764055ebc9a7a290b64d17cf9ea550f1099c202d83795aa967428ebdf335c9f7 Total reclaimed space: 314.7MB [root@i-jnsxhfco docker_images]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@i-jnsxhfco docker_images]# docker load < all.tar 764055ebc9a7: Loading layer [==================================================>] 72.53MB/72.53MB 245c9d23f653: Loading layer [==================================================>] 338.4kB/338.4kB ebef6caacb96: Loading layer [==================================================>] 4.194MB/4.194MB 0b7b774038f0: Loading layer [==================================================>] 31.71MB/31.71MB a71d36a87572: Loading layer [==================================================>] 2.048kB/2.048kB 9e1fddfb3a22: Loading layer [==================================================>] 3.584kB/3.584kB Loaded image: centos:latest 2653d992f4ef: Loading layer [==================================================>] 216.5MB/216.5MB Loaded image: redis:v1 [root@i-jnsxhfco docker_images]# ll total 741708 -rw-r--r-- 1 root root 325345792 Jun 29 16:18 all.tar -rw-r--r-- 1 root root 325344768 Jun 29 16:08 double.tar -rw------- 1 root root 108812800 Jun 29 16:00 redis_bk.tar [root@i-jnsxhfco docker_images]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 08502081bff6 5 days ago 105MB redis v1 300e315adb2f 6 months ago 209MB



