Docker镜像管理

Usage:  docker image COMMAND

Manage images

Commands:
  build       从Dockerfile中构建镜像
  history     显示镜像历史信息,如元数据等。。。
  import      Import the contents from a tarball to create a filesystem image。
  inspect     详细镜像的详细信息
  load        Load an image from a tar archive or STDIN
  ls          列出所有已下载镜像
  prune       删除没有被引用的镜像
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository 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 pull [选项] <镜像名>[:TAG]

-a 拉取给定镜像的所有TAG【不指定则默认拉取最新的】

例子:

docker pull nginx:1.9.1

推送镜像到镜像仓库

docker push [OPTIONS] NAME[:TAG]

-a, --all-tags                Push all tagged images in the repository


不过一般推送前都会打上标签,使用docker tag 

导出镜像到本地docker save

作用和场景:你制作完一个镜像导出成tar包,然后给其他人导入使用。

应用场景:假设A主机才有网络,B主机没有,那么B主机时无法从镜像仓库下载镜像的,所以可以在A主机中导出镜像,然后分享给B主机(如:通过SCP方式)然后B主机导入镜像使用。

docker save [选项] <镜像名> <镜像名>...
  -o  指定输出的路径
  
  **注意:可以同时导出多个(镜像名可以为多个),如你可以将mogodb和nginx镜像都导出到一个tar包中**

例子:

# 将postgres和mongo镜像导出到/swq/docker/myimages.tar
[root@localhost html]# docker save -o myimages.tar postgres:9.6 mongo:3.4


导入本地镜像文件docker load

docker load [选项] 
  -i 从给定的文件中导入,而不是STDIN
 
  如:docker load -i myimage.tar

打包导出容器的文件系统docker export

导出export

docker export -o <文件名> <容器名/容器ID>

导入import

docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Import the contents from a tarball to create a filesystem image

 ** # docker import将container导入后会成为一个image,而不是恢复为一个container。**
  **另外一点是,docker import可以指定IMAGE[:TAG],说明我们可以为镜像指定新名称。如果本地镜像库中已经存在同名的镜像,则原有镜像的名称将会被剥夺,赋给新的镜像。原有镜像将成为孤魂野鬼,只能通过IMAGE ID进行操作。**
  
Options:
  -c, --change list       Apply Dockerfile instruction to the created image
  -m, --message string    Set commit message for imported image
      --platform string   Set platform if server is multi-platform capable

save和export的区别:

Usage:  docker save [OPTIONS] IMAGE [IMAGE...]
# 保存一个或者多个镜像到tar归档文件中(默认是保存到STDOUT)
Save one or more images to a tar archive (streamed to STDOUT by default)




Usage:  docker export [OPTIONS] CONTAINER
# 导出容器的文件系统到tar归档文件中
Export a container's filesystem as a tar archive


从官方的介绍来看,我们可以清楚的知道,save保存的是镜像,export保存的是容器的文件系统

  docker的镜像你就好比是装系统的iso镜像,
  容器的文件系统就是好比装完系统的文件系统,你可以在装完后对系统添加、删除文件
  
  那么:
    1、你导出镜像实际上还是镜像(docker中还包含镜像历史记录)
    2、你导出容器容器的文件系统,就包含了你对容器修改后的文件,好比你用自己封装一个gho系统。
    
 
 注意:
   1、docker save 默认是指定镜像名的,如果你指定container容器id/容器名的话,实际是去保存运行这个容器背后的镜像
     应用场景:
       1、你下载一个镜像是国外的,由于防火墙的原因,你只能到外国服务器下,下完后你打包回来,然后再导入到你国内服务器的docker中。
       2、你的服务器不允许访问外国,那么你刚有个项目需要新的镜像才能部署,这时候也是可以使用docker save 与 docker load配合以导入镜像到服务器中。
       
    

其他选项:

#  docker tag的使用:
[root@localhost html]# docker image tag nginx tanginx:666
[root@localhost html]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       7         bd90b3d386b3   3 hours ago     460MB
nginx        latest    605c77e624dd   9 months ago    141MB
tanginx      666       605c77e624dd   9 months ago    141MB
centos       <none>    eeb6ee3f44bd   13 months ago   204MB

posted @ 2023-10-23 11:23  蕝戀  阅读(4)  评论(0编辑  收藏  举报