docker镜像操作

发布时间 2023-11-27 23:29:10作者: tom_log

docker镜像操作

[root@hecs-74102 ~]# docker image --help
Usage:  docker image COMMAND
Manage images
Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Download an image from a registry
  push        Upload an image to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.

镜像查找

语法 docker search 镜像:标签(指定版本)

描述 查找仓库的可用镜像

[root@hecs-74102 ~]# docker search redis
NAME(镜像名称)                  DESCRIPTION(描述信息)           STARS(点赞数)     OFFICIAL(官方认证的)   AUTOMATED(官方自动化流程构建的,意义不大)
redis                               Redis is an open source key-value store that…   12503     [OK]       
redislabs/redisearch                Redis With the RedisSearch module pre-loaded…   61                   
redislabs/redisinsight              RedisInsight - The GUI for Redis                94                   
redis/redis-stack-server            redis-stack-server installs a Redis server w…   62                   
redis/redis-stack                   redis-stack installs a Redis server with add…   82                   
redislabs/rebloom                   A probablistic datatypes module for Redis       25                   [OK]

拉取镜像到本地

语法 docker pull nginx:1.20.1

镜像名称:版本号(标签)//默认可以不添加标签表示最新

[root@hecs-74102 ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
1f7ce2fa46ab: Pull complete 
3c6368585bf1: Pull complete 
3911d271d7d8: Pull complete 
ac88aa9d4021: Pull complete 
127cd75a68a2: Pull complete 
4f4fb700ef54: Pull complete 
f3993c1104fc: Pull complete 
Digest: sha256:2976bc0437deff693af2dcf081a1d1758de8b413e6de861151a5a136c25eb9e4
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

查看本地镜像

语法 docker images

语法 docker image ls

[root@hecs-74102 ~]# docker images 
REPOSITORY                                                          TAG                     IMAGE ID       CREATED         SIZE
redis                                                               latest                  961dda256baa   2 weeks ago     138MB
openresty                                                           1.21.4.1-2-buster-fat   61a7383111bc   17 months ago   136MB
openjdk                                                             8-jdk-oracle            5783fe09fd20   23 months ago   359MB
nginx                                                               1.20.1                  c8d03f6b8b91   2 years ago     133MB

删除镜像

语法 docker rmi 镜像ID

语法 docker image rm 镜像ID

批量删除 docker images -q -a | xargs docker rmi 或 docker rmi $(docker images -q -a)

[root@hecs-74102 ~]# docker rmi 961dda256baa
Untagged: redis:latest
Untagged: redis@sha256:2976bc0437deff693af2dcf081a1d1758de8b413e6de861151a5a136c25eb9e4
Deleted: sha256:961dda256baa7a35297d34cca06bc2bce8397b0ef8b68d8064c30e338afc5a7d
Deleted: sha256:09f4ac3c2997331779d99bc625d4224756d374ae83b6aff36784e60a172e3150
Deleted: sha256:a515e0e35074ce754676135fba9991adcd13ec0cb1c828e3fb10120696c098f0
Deleted: sha256:c0e7da6567edbdff320fe4b4cd930466d448bfec67e21dcc4546ef03a9567c63
Deleted: sha256:2880e2e2c6fde92bdbeb7b7f4fa0311de5c14a6f6fa81a970d2a8ac266246398
Deleted: sha256:acf561667659ef44a6073d3f87952952d58a8c8638fff9d0e11af01db02c0855
Deleted: sha256:695fd6f5bcc5701e6e92f5e6acea3ffc272f7a38e4919d959d52c287264a23ff
Deleted: sha256:92770f546e065c4942829b1f0d7d1f02c2eb1e6acf0d1bc08ef0bf6be4972839
[root@hecs-74102 ~]# 

删除镜像2

prune删除所有未被 tag 标记和未被容器使用的镜像

[root@hecs-74102 ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] 

镜像标签操作

描述 标签用于区分版本,在很多场景中也用于区分不同仓库地址,如相同的仓库下的不同名称空间等。

语法 docker tag [{镜像名称}:{版本名称}] swr.cn-north-4.myhuaweicloud.com/{组织名称}/{镜像名称}:{版本名称}

[root@hecs-74102 ~]# docker images
REPOSITORY                                                          TAG                     IMAGE ID       CREATED         SIZE
redis                                                               latest                  961dda256baa   2 weeks ago     138MB
[root@hecs-74102 ~]# docker tag redis www.harbor.cn/test-namespace/redis:7.0.0
[root@hecs-74102 ~]# docker images
REPOSITORY                                                          TAG                     IMAGE ID       CREATED         SIZE
redis                                                               latest                  961dda256baa   2 weeks ago     138MB
www.harbor.cn/test-namespace/redis                                  7.0.0                   961dda256baa   2 weeks ago     138MB

登录仓库

语法 docker login -u 用户 -p 密码 仓库地址

描述 用于拉取仓库中镜像,或者上传镜像时登录仓库

# 可先用 docker info 查看当前登录仓库
[root@hecs-74102 ~]# docker login -u xxxx -p xxxx  swrwvxxx.xx.myhuaweicloud.com
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

上传镜像

语法 docker push 镜像:版本

描述 将本地制作好的镜像或保存的镜像上传到仓库中

sudo docker push swr.xxxxx.myhuaweicloud.com/{组织名称}/{镜像名称}:{版本名称}

查看镜像详细描述信息

描述 查看镜像分层 网络等详细信息

[root@hecs-74102 ~]# docker inspect 961dda256baa #镜像ID

查看镜像构建历史(流程或分层信息)

语法 docker image history 镜像ID 或 docker history 镜像ID # 此时为缩写docker的很多命令均可缩写

[root@hecs-74102 ~]# docker history 961dda256baa
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
961dda256baa   2 weeks ago   CMD ["redis-server"]                            0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   EXPOSE map[6379/tcp:{}]                         0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   ENTRYPOINT ["docker-entrypoint.sh"]             0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   COPY docker-entrypoint.sh /usr/local/bin/ # …   661B      buildkit.dockerfile.v0
<missing>      2 weeks ago   WORKDIR /data                                   0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   VOLUME [/data]                                  0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   RUN /bin/sh -c mkdir /data && chown redis:re…   0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   RUN /bin/sh -c set -eux;   savedAptMark="$(a…   58.8MB    buildkit.dockerfile.v0
<missing>      2 weeks ago   ENV REDIS_DOWNLOAD_SHA=3e2b196d6eb4ddb9e7430…   0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   ENV REDIS_DOWNLOAD_URL=http://download.redis…   0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   ENV REDIS_VERSION=7.2.3                         0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   RUN /bin/sh -c set -eux;  savedAptMark="$(ap…   4.13MB    buildkit.dockerfile.v0
<missing>      2 weeks ago   ENV GOSU_VERSION=1.16                           0B        buildkit.dockerfile.v0
<missing>      2 weeks ago   RUN /bin/sh -c groupadd -r -g 999 redis && u…   4.3kB     buildkit.dockerfile.v0
<missing>      2 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
<missing>      2 weeks ago   /bin/sh -c #(nop) ADD file:d261a6f6921593f1e…   74.8MB    

镜像的导入导出

描述 用于镜像的转存,通常导出为tar包再导入到其他主机内

导出语法 docker docker save > 指定命名的tar包 源镜像:版本

导入语法 docker load -i 需要导入的镜像tar包

# docker docker save > myredis.tar www.harbor.cn/test-namespace/redis:7.0.0
# docker load -i myredis.tar

镜像的导入导出2

描述 除了load与save外我们还有import与export可用导入导出镜像

导入语法 docker import xxx.tar newname:tag

导出语法 docker export 需要的tar文件 源镜像