跟我学Docker:docker镜像的常用命令(三)

1,114 阅读6分钟
原文链接: www.sudo.ren

当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/,我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个centos的镜像来作为我们的服务器。我们可以通过 docker search 命令搜索 centos 来寻找适合我们的镜像。

搜索镜像:docker search
选镜像的建议: 1.优先考虑官方  2.stars数量多

[root@docker01 ~]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5461                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              122                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.6.1810 x86…   110                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   93                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   59                                      
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              57                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      44                                      
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   38                                      
kinogmt/centos-ssh                 CentOS with SSH                                 28                                      [OK]
centos/php-56-centos7              Platform for building and running PHP 5.6 ap…   21                                      
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   10                                      
guyton/centos6                     From official centos6 container with full up…   9                                       [OK]
drecom/centos-ruby                 centos ruby                                     6                                       [OK]
mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                                       [OK]
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
pivotaldata/centos                 Base centos, freshened up a little with a Do…   3                                       
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   2                                       
miko2u/centos6                     CentOS6 日本語環境                                   2                                       [OK]
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   2                                       
mcnaughton/centos-base             centos base image                               1                                       [OK]
indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developmen…   1                                       [OK]
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0                                       
smartentry/centos                  centos with smartentry                          0                                       [OK]
pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0                                       
[root@docker01 ~]#

#Name:镜像名称,
#DESCRIPTION:镜像的描述
#STARS:别星标的数量,数量越大说明受人认可度越高
#OFFICIAL:是否为官方产品
#AUTOMATED:是否为自动化构建

获取镜像:     docker pull(push) (因为墙的原因,需要镜像加速,否则连接超时,无法下载)
镜像加速器:阿里云加速器,daocloud加速器,中科大加速器,Docker 中国官方镜像加速:https://registry.docker-cn.com
         docker pull centos:6.8(没有指定版本,默认会下载最新版)     
         docker pull daocloud.io/huangzhichong/alpine-cn:latest

[root@docker01 ~]# docker pull centos:6.8
6.8: Pulling from library/centos
7ce0cebb9dca: Pull complete 
Digest: sha256:39abd0c8e375de6fb7334d42ec2a46643f34cbc1bbaf37e2b484065f05eaa7a2
Status: Downloaded newer image for centos:6.8
[root@docker01 ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8ba884070f61: Pull complete 
Digest: sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c
Status: Downloaded newer image for centos:latest
[root@docker01 ~]# 

扩展:查询docker镜像所有版本https://hub.docker.com/r/library/
国内是所有镜像网站:www.douban.com/note/517555…
私有仓库的镜像名称都较长(网站地址,用户名,版本名等)


docker镜像其他操作

1.查看镜像:docker images(等效命令:docker image ls)

[root@docker01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              98ebf73aba75        5 days ago          109MB
centos              6.8                 82f3b5f3c58f        4 months ago        195MB
centos              latest              9f38484d220f        4 months ago        202MB
[root@docker01 ~]# 
[root@docker01 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              98ebf73aba75        5 days ago          109MB
centos              6.8                 82f3b5f3c58f        4 months ago        195MB
centos              latest              9f38484d220f        4 months ago        202MB
[root@docker01 ~]# 

REPOSITORY:镜像名字
TAG:标签(可自定义)
IMAGE ID:镜像ID(唯一)
CREATED:创建时间
SIZE:镜像大小

2.删除镜像 docker rmi 等效命令(docker image rm)   例子:docker image rm centos:latest

[root@docker01 ~]# docker rmi centos:6
Untagged: centos:6
Untagged: centos@sha256:dec8f471302de43f4cfcf82f56d99a5227b5ea1aa6d02fa56344986e1f4610e7
Deleted: sha256:d0957ffdf8a2ea8c8925903862b65a1b6850dbb019f88d45e927d3d5a3fa0c31
Deleted: sha256:af6bf1987c2eb07d73f33836b0d8fd825d7c785273526b077e46780e8b4b2ae9
[root@docker01 ~]# 
[root@docker01 ~]# docker image rm centos:latest
Untagged: centos:latest
Untagged: centos@sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c
Deleted: sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1
Deleted: sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854
[root@docker01 ~]# 

#docker rmi centos:6,需要在所删容器后加上标签,否则(docker rmi centos)默认删掉最新版

3.导出镜像  docker save 等效命令(docker image save)  例子:docker image save centos > docker-centos7.4.tar.gz

[root@docker01 ~]# docker image save centos:6.8 >docker_centos6.8.tar.gz
[root@docker01 ~]# ll
total 197480
-rw-------. 1 root root      1757 Nov 27  2018 anaconda-ks.cfg
-rw-r--r--  1 root root 202213376 Jul 23 09:36 docker_centos6.8.tar.gz
[root@docker01 ~]# 


4.导入镜像  docker load  等效命令(docker image load)例子:docker image load -i docker-centos7.4.tar.gz

[root@docker01 ~]# docker image load -i docker_centos6.8.tar.gz 
ad337ac82f03: Loading layer [==================================================>]  202.2MB/202.2MB
Loaded image: centos:6.8
[root@docker01 ~]# 

-i:指定导入的镜像

5.docker其他命令,通过docker image可查看其他docker命令

[root@docker01 ~]# docker image

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        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.
[root@docker01 ~]# 

#build:构建镜像,可手动创建镜像
#history:查看构建镜像的历史记录
#import:导入镜像(已弃用)
#inspect:查看镜像详情信息,属性
#load:导入镜像
#ls:查看镜像列表
#prune:删除镜像(已弃用)
#pull:下载镜像
#push:上传镜像
#rm:删除镜像
#save:导出镜像
#tag:给镜像取别名