Skip to content

Docker镜像命令

1. 帮助命令

docker --help: 查看docker总体帮助文档
docker 具体命令 --help: 查看docker命令帮助文档
docker info: 查看docker概要信息

sh
[jack@hadoop102 ~]$ docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Log in to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx
  checkpoint  Manage checkpoints
  compose*    Docker Compose
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Swarm Commands:
  config      Manage Swarm configs
  node        Manage Swarm nodes
  secret      Manage Swarm secrets
  service     Manage Swarm services
  stack       Manage Swarm stacks
  swarm       Manage Swarm

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Global Options:
      --config string      Location of client config files (default "/home/jack/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket to connect to
  -l, --log-level string   Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/home/jack/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/home/jack/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/home/jack/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Run 'docker COMMAND --help' for more information on a command.

For more help on how to use Docker, head to https://docs.docker.com/go/guides/

2. docker images命令

格式: docker images [OPTIONS] 列出本地主机上的镜像。
OPTIONS说明:

  • -a :列出本地所有的镜像(含历史映像层)
  • -q :只显示镜像ID。
sh
[jack@hadoop102 ~]$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   11 months ago   13.3kB
[jack@hadoop102 ~]$ sudo docker images -q
d2c94e258dcb

返回结果行说明:
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签版本号
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小

提示

同一仓库源可以有多个TAG版本,代表这个仓库源的不同个版本,我们使用REPOSITORY:TAG来定义不同的镜像。如果你不指定一个镜像的版本标签,例如你使用ubuntu镜像,Docker将默认选择使用ubuntu:latest镜像。

3. docker search命令

格式:docker search [OPTIONS] 镜像名字 查询某个XXX镜像
OPTIONS说明:

  • --limit: 只列出N个镜像,默认25个
sh
[jack@hadoop102 ~]$ sudo docker search redis
NAME                                DESCRIPTION                                      STARS     OFFICIAL
redis                               Redis is an open source key-value store that…   12774     [OK]
redislabs/redisearch                Redis With the RedisSearch module pre-loaded…   63        
redislabs/redisinsight              RedisInsight - The GUI for Redis                 101       
redis/redis-stack-server            redis-stack-server installs a Redis server w…   75        
redis/redis-stack                   redis-stack installs a Redis server with add…   106       
redislabs/rebloom                   A probablistic datatypes module for Redis        27        
redislabs/redis                     Clustered in-memory database engine compatib…   40        
redislabs/rejson                    RedisJSON - Enhanced JSON data type processi…   53        
redislabs/redismod                  An automated build of redismod - latest Redi…   46        
redislabs/redisgraph                A graph database module for Redis                27        
redislabs/redistimeseries           A time series database module for Redis          12        
redislabs/operator                                                                   7         
redislabs/operator-internal         This repository contains pre-released versio…   1         
redislabs/redis-py                                                                   5         
redislabs/redis-webcli              A tiny Flask app to provide access to Redis   5         
redislabs/k8s-controller-internal                                                    0         
redislabs/redisgears                An automated build of RedisGears                 4         
redislabs/memtier_benchmark         Docker image to run memtier_benchmark            0         
redislabs/redisai                                                                    6         
redislabs/k8s-controller                                                             2         
redislabs/ng-redis-raft             Redis with redis raft module                     0         
redislabs/olmtest                   Test artefact for OLM CSV                        1         
redis/redisinsight                  Redis Insight - our best official GUI for Re…   5         
redislabs/olm-bundle                                                                 0         
bitnami/redis                       Bitnami container image for Redis                277 

[jack@hadoop102 ~]$ sudo docker search --limit 5 redis
NAME                       DESCRIPTION                                      STARS     OFFICIAL
redis                      Redis is an open source key-value store that…   12774     [OK]
redislabs/redisearch       Redis With the RedisSearch module pre-loaded…   63        
redislabs/redisinsight     RedisInsight - The GUI for Redis                 101       
redis/redis-stack-server   redis-stack-server installs a Redis server w…   75        
redis/redis-stack          redis-stack installs a Redis server with add…   106

返回结果行说明:
NAME:表示镜像的名称
DESCRIPTION: 镜像说明 STARS: 点赞数量 OFFICIAL: 是否官方

4. docker pull命令

格式: docker pull 镜像名字[:TAG]: 下载某个版本的镜像。没有TAG就是最新版,等同于docker pull 镜像名字:latest

sh
[jack@hadoop102 ~]$ sudo docker pull gitlab/gitlab-ce
Using default tag: latest
latest: Pulling from gitlab/gitlab-ce
7021d1b70935: Pull complete 
84ce88366b95: Pull complete 
e1c92fb77de1: Retrying in 10 seconds 
719e91a15767: Retrying in 13 seconds 
d680b9b016aa: Retrying in 16 seconds 
f0027cb8bf12: Waiting 
8090ecb94c61: Waiting 
2d67bfd969b5: Waiting 
216d686bfe05: Waiting

扩展

🤔谈谈docker虚悬镜像是什么?
仓库名、标签都是<none>的镜像,俗称虚悬镜像(dangling image),如图所示, 一般要把这种镜像清除。 Alt text

5. docker system df命令

格式:docker system df 查看镜像/容器/数据卷所占的空间

sh
[jack@hadoop102 ~]$ sudo docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          1         1         13.26kB   0B (0%)
Containers      1         0         0B        0B
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B

6. docker rmi命令

格式:docker rmi [-f] 镜像ID1[:TAG] 镜像ID2[:TAG]... 删除本地指定镜像,多个用空格隔开,-f表示强制删除

sh
[jack@hadoop104 ~]$ sudo docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container a3a239208eb3 is using its referenced image d2c94e258dcb

如果镜像被用过或者正在使用,会提示删除失败。

sh
[jack@hadoop104 ~]$ sudo docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:53cc4d415d839c98be39331c948609b659ed725170ad2ca8eb36951288f81b75
Deleted: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a

7. docker tag命令

格式:docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] 用于给 Docker 镜像打标签。

sh
[root@hadoop104 ~]# docker images
REPOSITORY                                                      TAG           IMAGE ID       CREATED         SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/tomcat       8.5-jdk8      6113f55245f6   6 months ago    338MB
[root@hadoop104 ~]# docker tag 6113f55245f6 tomcat:8.5-jdk8
[root@hadoop104 ~]# docker images
REPOSITORY                                                      TAG           IMAGE ID       CREATED         SIZE
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/tomcat       8.5-jdk8      6113f55245f6   6 months ago    338MB
tomcat                                                          8.5-jdk8      6113f55245f6   6 months ago    338MB