当前位置:   article > 正文

Docker 镜像常用命令总结_docker拉取镜像命令

docker拉取镜像命令

目录

1. Docker镜像相关命令(重点掌握)

1.1 docker pull 镜像名称 [:tag] 拉取镜像

1.2 docker search 镜向名称  (查询某个镜像)

1.3 docker images 列出当前主机上所有镜像

1.4 docker rmi 镜像ID 删除镜像

1.5 docker system df 查看所有镜像容器所占空间

1.6 docker ps 查看正在运行的镜像列表

1.7 docker restart "容器ID" 重启容器

1.8 docker stop "镜像ID" 停止容器

1.9 docker rm "镜像ID" 删除镜像

1.10 docker kill "强制删除容器"

2. Docker 虚悬镜像是什么?

3. 启动Docker

4. 查看Docker运行状态

5. 重启Docker

6. 停止Dokcer

7. 配置开机启动docker

8. 查看 docker 所有命令


1. Docker镜像相关命令(重点掌握)

1.1 docker pull 镜像名称 [:tag] 拉取镜像

可以看到我在后面加上了[:tag],这个是版本号的意思,因为同一个镜像也许会有多种不同的版本,如果你加,默认就是给你拉取 latest最新版的,这一点要记住哦!

如下所示,我拉取 redis 镜像,它默认就是下载的最新的。

  1. [root@localhost docker]# docker pull redis
  2. Using default tag: latest
  3. latest: Pulling from library/redis
  4. a378f10b3218: Pull complete
  5. b266cd8112a6: Pull complete
  6. 7ba86e6448de: Pull complete
  7. 3aeb7c9e9a5f: Pull complete
  8. de3be2a98bda: Pull complete
  9. 4f4fb700ef54: Pull complete
  10. 98e18d21aa3b: Pull complete
  11. Digest: sha256:1f1bd4adf5dabf173b235ba373faef55f3ad53394791d1473763bf5a2181780d
  12. Status: Downloaded newer image for redis:latest
  13. docker.io/library/redis:latest
1.2 docker search 镜向名称  (查询某个镜像)

如下,我运行此命令查询 nginx 镜像都有哪些,返回了一大堆

  1. [root@localhost ~]# docker search nginx
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. nginx Official build of Nginx. 19145 [OK]
  4. unit Official build of NGINX Unit: Universal Web … 15 [OK]
  5. nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 127
  6. nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 82
  7. nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 33
  8. nginxinc/nginx-s3-gateway Authenticating and caching gateway based on … 2
  9. nginx/unit This repository is retired, use the Docker o… 64
  10. nginx/nginx-ingress-operator NGINX Ingress Operator for NGINX and NGINX P… 1
  11. nginxinc/amplify-agent NGINX Amplify Agent docker repository 1
  12. nginx/nginx-quic-qns NGINX QUIC interop 1
  13. nginxinc/ingress-demo Ingress Demo 4
  14. nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 115
  15. nginxproxy/acme-companion Automated ACME SSL certificate generation fo… 125
  16. bitnami/nginx Bitnami nginx Docker Image 176 [OK]
  17. bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 30 [OK]
  18. ubuntu/nginx Nginx, a high-performance reverse proxy & we… 102
  19. nginxinc/nginmesh_proxy_debug 0
  20. nginxproxy/docker-gen Generate files from docker container meta-da… 12
  21. nginxinc/mra-fakes3 0
  22. kasmweb/nginx An Nginx image based off nginx:alpine and in… 6
  23. rancher/nginx-ingress-controller 11
  24. nginxinc/ngx-rust-tool 0
  25. nginxinc/mra_python_base 0
  26. nginxinc/nginmesh_proxy_init 0

如果有同学觉得比较多,我们可以在 search 后面加后缀 --limit N 显示几个,如下 --limit 5 表示显示排行榜前5个。

  1. [root@localhost ~]# docker search --limit 5 redis
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. redis Redis is an open source key-value store that… 12425 [OK]
  4. redislabs/redisearch Redis With the RedisSearch module pre-loaded… 59
  5. redislabs/redisinsight RedisInsight - The GUI for Redis 91
  6. redis/redis-stack-server redis-stack-server installs a Redis server w… 57
  7. redislabs/rebloom A probablistic datatypes module for Redis 24 [OK]

最上面的NAME,DESCRIPT,STARTS,OFFICIAL,AUTOMATED是参数,这些参数的含义如下图中所示,一般情况下选择第一个镜像就可以了,第一个通常是官方镜像。

1.3 docker images 列出当前主机上所有镜像

刚才我已经拉取了一个 redis 镜像,下面我们使用这个命令查看一下,可以看到已经查询到了

TAG:镜像版本;

IMAGE ID:镜像ID;

SIZE:镜像大小;

  1. [root@localhost docker]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. redis latest e579380d4317 6 days ago 138MB
1.4 docker rmi 镜像ID 删除镜像

我将刚才拉取下来的 redis 镜像再删除,然后 docker images 重新查看,可以看到,此时已经没有镜像了,redis 镜像已经没有了。

还有一点需要注意,后面的镜像ID可以写多个,表示删除多个。

  1. [root@localhost docker]# docker rmi e579380d4317
  2. Untagged: redis:latest
  3. Untagged: redis@sha256:1f1bd4adf5dabf173b235ba373faef55f3ad53394791d1473763bf5a2181780d
  4. Deleted: sha256:e579380d43178bcee8c8b219063605f45e035238335db5d3b3e95c5e38145700
  5. Deleted: sha256:b1f68d2cfdde30e7bfe3002347c37bf6b872216fa8faaa612d06c9ec59668021
  6. Deleted: sha256:562c06f2b95aea14af2496f9d4b9206efd39680b547232a7d4077137eb615d04
  7. Deleted: sha256:28a572fe2e73a8bf90e92ed1a072c9227d019cdc202c2c88cd34c44f7ab45eee
  8. Deleted: sha256:02b286a9390e2cd90268def73af145e1d09ac52aaa664d83bd0930373b2d62e2
  9. Deleted: sha256:3d1901f414d075be1012ec6ba638b0b2005c79d414f677e8317f3fb7a1cab474
  10. Deleted: sha256:3441e2762aa905a8939c32d5ac0d80e7f7222773fa1095825bfe9da360859be9
  11. Deleted: sha256:cb4596cc145400fb1f2aa56d41516b39a366ecdee7bf3f9191116444aacd8c90
  12. [root@localhost docker]# docker images
  13. REPOSITORY TAG IMAGE ID CREATED SIZE
1.5 docker system df 查看所有镜像容器所占空间

上面我说到了 docker images 可以查看所有镜像,而 docker system df 则可以帮助我们查看所有镜像/容器/数据卷占用的空间(这里补充一句,各个镜像独立运行,相互隔离,每个运行的镜像就是一个容器)

刚才我拉取了三个镜像,如下所示,运行命令之后,docker 就会把容器,镜像,数据卷各自占用的比例列出,这里我一个都没有运行,全都只是拉取下来的景象,所以占比为100%

  1. [root@localhost docker]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx latest 593aee2afb64 6 hours ago 187MB
  4. redis latest e579380d4317 6 days ago 138MB
  5. mysql latest ae2502152260 2 months ago 574MB
  6. [root@localhost docker]#
  7. [root@localhost docker]# docker system df
  8. TYPE TOTAL ACTIVE SIZE RECLAIMABLE
  9. Images 3 0 823.7MB 823.7MB (100%)
  10. Containers 0 0 0B 0B
  11. Local Volumes 0 0 0B 0B
  12. Build Cache 0 0 0B 0B
1.6 docker ps 查看正在运行的镜像列表

如下,执行该命令就会显示出当前 docker 容器中所有正在运行的镜像;

后面加上一个 -a 查看所有镜像容器;

1.7 docker restart "容器ID" 重启容器

镜像运行后形成容器,但如果我们把虚拟机关闭,容器也就随着关闭。如果没有设置容器随着docker 运行启动,那么下次我们再重新运行此容器,所以 "docker ps -a" 和 "docker restart 容器ID"这两条命令通常可以搭配使用;

运行此命令后,返回容器ID就说明容器已经重新启动;

1.8 docker stop "镜像ID" 停止容器
1.9 docker rm "镜像ID" 删除镜像

注意正在运行的容器无法删除,需要先 stop 停止而后才可以删除

1.10 docker kill "强制删除容器"

当一个容器无法停止时,可以使用此命令强制停止容器并将其删除

2. Docker 虚悬镜像是什么?

这也算是一个小的面试题,通过上面的学习,我们学会了使用 docker images 查看当前所有已有的docker 镜像。

但有些时候,查询出来的镜像的名字和版本都是none,只有镜像ID和大小,如下图。也就是说这个镜像我们不知道它是什么东西,不能确定,这种镜像称之为虚悬镜像。

这种镜像通常没什么用,建议删除,有时候是因为镜像下载过程中出现了问题,有时候是因为构建镜像时出现了问题。

3. 启动Docker

systemctl start docker

如下,在 Linux 操作系统中,只要没有报错就代表启动成功

[zhangsir@localhost ~]$ systemctl start docker

4. 查看Docker运行状态

systemctl status docker

刚才启动成功,查看一下运行状态,如下所示,Active为running表示正在运行中

  1. [root@localhost ~]# systemctl status docker
  2. ● docker.service - Docker Application Container Engine
  3. Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
  4. Active: active (running) since Wed 2023-10-25 11:33:51 CST; 6s ago
  5. Docs: https://docs.docker.com
  6. Main PID: 9895 (dockerd)
  7. Tasks: 7
  8. Memory: 71.2M
  9. CGroup: /system.slice/docker.service
  10. └─9895 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  11. Oct 25 11:33:50 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
  12. Oct 25 11:33:50 localhost.localdomain dockerd[9895]: time="2023-10-25T11:33:50.961478252+08:00" level=info msg="Starting up"
  13. Oct 25 11:33:51 localhost.localdomain dockerd[9895]: time="2023-10-25T11:33:51.003124975+08:00" level=info msg="[graphdriver] using prior storage driver: overlay2"
  14. Oct 25 11:33:51 localhost.localdomain dockerd[9895]: time="2023-10-25T11:33:51.003454992+08:00" level=info msg="Loading containers: start."
  15. Oct 25 11:33:51 localhost.localdomain dockerd[9895]: time="2023-10-25T11:33:51.063846884+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
  16. Oct 25 11:33:51 localhost.localdomain dockerd[9895]: time="2023-10-25T11:33:51.086338615+08:00" level=info msg="Loading containers: done."
  17. Oct 25 11:33:51 localhost.localdomain dockerd[9895]: time="2023-10-25T11:33:51.109714791+08:00" level=info msg="Docker daemon" commit=a61e2b4 graphdriver=overlay2 version=24.0.5
  18. Oct 25 11:33:51 localhost.localdomain dockerd[9895]: time="2023-10-25T11:33:51.109756158+08:00" level=info msg="Daemon has completed initialization"
  19. Oct 25 11:33:51 localhost.localdomain dockerd[9895]: time="2023-10-25T11:33:51.129061470+08:00" level=info msg="API listen on /run/docker.sock"
  20. Oct 25 11:33:51 localhost.localdomain systemd[1]: Started Docker Application Container Engine.

5. 重启Docker

systemctl restart docker

重启 docker,命令与启动docker相似,加了一个前缀 "re",只要没有报错就代表重启成功,在查询一下状态,也显示正在运行中

  1. [root@localhost ~]# systemctl restart docker
  2. [root@localhost ~]#
  3. [root@localhost ~]# systemctl status docker
  4. ● docker.service - Docker Application Container Engine
  5. Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
  6. Active: active (running) since Wed 2023-10-25 11:35:37 CST; 1min 21s ago
  7. Docs: https://docs.docker.com
  8. Main PID: 10094 (dockerd)
  9. Tasks: 7
  10. Memory: 30.2M
  11. CGroup: /system.slice/docker.service
  12. └─10094 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  13. Oct 25 11:35:36 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
  14. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.065508073+08:00" level=info msg="Starting up"
  15. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.082201618+08:00" level=info msg="[graphdriver] using prior storage driver: overlay2"
  16. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.082323526+08:00" level=info msg="Loading containers: start."
  17. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.136339904+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
  18. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.153493234+08:00" level=info msg="Loading containers: done."
  19. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.161727804+08:00" level=info msg="Docker daemon" commit=a61e2b4 graphdriver=overlay2 version=24.0.5
  20. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.161762409+08:00" level=info msg="Daemon has completed initialization"
  21. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.175532811+08:00" level=info msg="API listen on /run/docker.sock"
  22. Oct 25 11:35:37 localhost.localdomain systemd[1]: Started Docker Application Container Engine.

6. 停止Dokcer

systemctl stop docker

在停止之后,它给了一个警告 "Stopping docker.service, but it can still be activated by:
  docker.socket"

翻译过来就是 "停止docker.service,但它仍然可以由docker.socket激活"。

查看状态,此时docker已经停止,处于 dead 死亡状态。

  1. [root@localhost ~]# systemctl stop docker
  2. Warning: Stopping docker.service, but it can still be activated by:
  3. docker.socket
  4. [root@localhost ~]# systemctl status docker
  5. ● docker.service - Docker Application Container Engine
  6. Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
  7. Active: inactive (dead) since Wed 2023-10-25 11:38:17 CST; 1min 20s ago
  8. Docs: https://docs.docker.com
  9. Process: 10094 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=0/SUCCESS)
  10. Main PID: 10094 (code=exited, status=0/SUCCESS)
  11. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.136339904+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used to set a preferred IP address"
  12. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.153493234+08:00" level=info msg="Loading containers: done."
  13. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.161727804+08:00" level=info msg="Docker daemon" commit=a61e2b4 graphdriver=overlay2 version=24.0.5
  14. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.161762409+08:00" level=info msg="Daemon has completed initialization"
  15. Oct 25 11:35:37 localhost.localdomain dockerd[10094]: time="2023-10-25T11:35:37.175532811+08:00" level=info msg="API listen on /run/docker.sock"
  16. Oct 25 11:35:37 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
  17. Oct 25 11:38:17 localhost.localdomain systemd[1]: Stopping Docker Application Container Engine...
  18. Oct 25 11:38:17 localhost.localdomain dockerd[10094]: time="2023-10-25T11:38:17.418003207+08:00" level=info msg="Processing signal 'terminated'"
  19. Oct 25 11:38:17 localhost.localdomain dockerd[10094]: time="2023-10-25T11:38:17.418822780+08:00" level=info msg="Daemon shutdown complete"
  20. Oct 25 11:38:17 localhost.localdomain systemd[1]: Stopped Docker Application Container Engine.

7. 配置开机启动docker

systemctl enable docker

运行这个命令之后,以后只要你启动了 Linux 服务器,Doker就会跟着启动,不需要每次都使用上面的启动命令

  1. [root@localhost ~]# systemctl enable docker
  2. Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

8. 查看 docker 所有命令

docker --help

运行该命令,docker会给我们返回所有可执行命令,后面还有它的英文解释,大多也都不常用,同学们可以用到哪个查哪个。

  1. [root@localhost ~]# docker --help
  2. Usage: docker [OPTIONS] COMMAND
  3. A self-sufficient runtime for containers
  4. Common Commands:
  5. run Create and run a new container from an image
  6. exec Execute a command in a running container
  7. ps List containers
  8. build Build an image from a Dockerfile
  9. pull Download an image from a registry
  10. push Upload an image to a registry
  11. images List images
  12. login Log in to a registry
  13. logout Log out from a registry
  14. search Search Docker Hub for images
  15. version Show the Docker version information
  16. info Display system-wide information
  17. Management Commands:
  18. builder Manage builds
  19. buildx* Docker Buildx (Docker Inc., v0.11.2)
  20. compose* Docker Compose (Docker Inc., v2.20.2)
  21. container Manage containers
  22. context Manage contexts
  23. image Manage images
  24. manifest Manage Docker image manifests and manifest lists
  25. network Manage networks
  26. plugin Manage plugins
  27. system Manage Docker
  28. trust Manage trust on Docker images
  29. volume Manage volumes
  30. Swarm Commands:
  31. swarm Manage Swarm
  32. Commands:
  33. attach Attach local standard input, output, and error streams to a running container
  34. commit Create a new image from a container's changes
  35. cp Copy files/folders between a container and the local filesystem
  36. create Create a new container
  37. diff Inspect changes to files or directories on a container's filesystem
  38. events Get real time events from the server
  39. export Export a container's filesystem as a tar archive
  40. history Show the history of an image
  41. import Import the contents from a tarball to create a filesystem image
  42. inspect Return low-level information on Docker objects
  43. kill Kill one or more running containers
  44. load Load an image from a tar archive or STDIN
  45. logs Fetch the logs of a container
  46. pause Pause all processes within one or more containers
  47. port List port mappings or a specific mapping for the container
  48. rename Rename a container
  49. restart Restart one or more containers
  50. rm Remove one or more containers
  51. rmi Remove one or more images
  52. save Save one or more images to a tar archive (streamed to STDOUT by default)
  53. start Start one or more stopped containers
  54. stats Display a live stream of container(s) resource usage statistics
  55. stop Stop one or more running containers
  56. tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  57. top Display the running processes of a container
  58. unpause Unpause all processes within one or more containers
  59. update Update configuration of one or more containers
  60. wait Block until one or more containers stop, then print their exit codes
  61. Global Options:
  62. --config string Location of client config files (default "/root/.docker")
  63. -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")
  64. -D, --debug Enable debug mode
  65. -H, --host list Daemon socket to connect to
  66. -l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
  67. --tls Use TLS; implied by --tlsverify
  68. --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
  69. --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
  70. --tlskey string Path to TLS key file (default "/root/.docker/key.pem")
  71. --tlsverify Use TLS and verify the remote
  72. -v, --version Print version information and quit
  73. Run 'docker COMMAND --help' for more information on a command.
  74. For more help on how to use Docker, head to https://docs.docker.com/go/guides/
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/1008201
推荐阅读
相关标签
  

闽ICP备14008679号