image 镜像

命令:docker image 镜像管理命令

Usage: docker image COMMAND

  1. PS C:\Users\DELL> docker image --help
  2. Usage: docker image COMMAND
  3. Commands:
  4. build Build an image from a Dockerfile
  5. history Show the history of an image
  6. import Import the contents from a tarball to create a filesystem image
  7. inspect Display detailed information on one or more images
  8. load Load an image from a tar archive or STDIN
  9. ls List images
  10. prune Remove unused images
  11. pull Pull an image or a repository from a registry
  12. push Push an image or a repository to a registry
  13. rm Remove one or more images
  14. save Save one or more images to a tar archive (streamed to STDOUT by default)
  15. tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  16. Run 'docker image COMMAND --help' for more information on a command.

例子:管理命令举例

Usage: docker image ls [OPTIONS] [REPOSITORY[:TAG]]

  1. PS C:\Users\DELL> docker image ls --help
  2. Usage: docker image ls [OPTIONS] [REPOSITORY[:TAG]]
  3. List images
  4. Aliases:
  5. ls, list
  6. Options:
  7. -a, --all Show all images (default hides intermediate images)
  8. --digests Show digests
  9. -f, --filter filter Filter output based on conditions provided
  10. --format string Pretty-print images using a Go template
  11. --no-trunc Don't truncate output
  12. -q, --quiet Only show image IDs
  13. PS C:\Users\DELL> docker image ls -a
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. minio/minio latest f55b2545c435 2 days ago 191MB
  16. iredmail/mariadb stable e4899534d71b 3 weeks ago 761MB
  17. docker/getting-started latest 3ba8f2ff0727 2 months ago 27.9MB
  18. lejmr/iredmail mysql-latest f719a06cbe14 9 months ago 861MB
  19. PS C:\Users\DELL>

命令使用

image 镜像

查看镜像列表 docker images

Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]

  1. PS C:\Users\DELL> docker images --help
  2. Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
  3. List images
  4. Options:
  5. -a, --all Show all images (default hides intermediate images)
  6. --digests Show digests
  7. -f, --filter filter Filter output based on conditions provided
  8. --format string Pretty-print images using a Go template
  9. --no-trunc Don't truncate output
  10. -q, --quiet Only show image IDs
  11. PS C:\Users\DELL> docker images
  12. REPOSITORY TAG IMAGE ID CREATED SIZE
  13. minio/minio latest f55b2545c435 2 days ago 191MB
  14. iredmail/mariadb stable e4899534d71b 3 weeks ago 761MB
  15. docker/getting-started latest 3ba8f2ff0727 2 months ago 27.9MB
  16. lejmr/iredmail mysql-latest f719a06cbe14 9 months ago 861MB
  17. PS C:\Users\DELL>

拉取镜像 docker pull

Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]

  1. PS C:\Users\DELL> docker pull --help
  2. Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  3. Pull an image or a repository from a registry
  4. Options:
  5. -a, --all-tags Download all tagged images in the repository
  6. --disable-content-trust Skip image verification (default true)
  7. --platform string Set platform if server is multi-platform
  8. capable
  9. -q, --quiet Suppress verbose output
  10. PS C:\Users\DELL> docker image pull --help
  11. Usage: docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
  12. Pull an image or a repository from a registry
  13. Options:
  14. -a, --all-tags Download all tagged images in the repository
  15. --disable-content-trust Skip image verification (default true)
  16. --platform string Set platform if server is multi-platform
  17. capable
  18. -q, --quiet Suppress verbose output
  19. PS C:\Users\DELL>

例子 docker pull

  1. PS C:\Users\DELL> docker pull httpd
  2. Using default tag: latest
  3. latest: Pulling from library/httpd
  4. 69692152171a: Pull complete
  5. 7284b4e0cc7b: Pull complete
  6. 3678b2d55ccd: Pull complete
  7. ab492cf0b2a4: Pull complete
  8. 991f7f97a9d8: Pull complete
  9. Digest: sha256:e4c2b93c04762468a6cce6d507d94def02ef4dc285278d0d926e09827f4857db
  10. Status: Downloaded newer image for httpd:latest
  11. docker.io/library/httpd:latest
  12. PS C:\Users\DELL>

默认拉取最新的tag latest

查询镜像 docker search

Usage: docker search [OPTIONS] TERM

  1. PS C:\Users\DELL> docker search --help
  2. Usage: docker search [OPTIONS] TERM
  3. Search the Docker Hub for images
  4. Options:
  5. -f, --filter filter Filter output based on conditions provided
  6. --format string Pretty-print search using a Go template
  7. --limit int Max number of search results (default 25)
  8. --no-trunc Don't truncate output
  9. PS C:\Users\DELL> docker search redis
  10. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  11. redis Redis is an open source key-value store that… 9446 [OK]
  12. bitnami/redis Bitnami Redis Docker Image 180 [OK]
  13. sameersbn/redis 83 [OK]
  14. grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2 78
  15. rediscommander/redis-commander Alpine image for redis-commander - Redis man… 58 [OK]
  16. redislabs/redisearch Redis With the RedisSearch module pre-loaded… 34
  17. redislabs/redisinsight RedisInsight - The GUI for Redis 30
  18. redislabs/redis Clustered in-memory database engine compatib… 30
  19. oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Supp… 25
  20. redislabs/rejson RedisJSON - Enhanced JSON data type processi… 23
  21. arm32v7/redis Redis is an open source key-value store that… 23
  22. bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 22 [OK]
  23. redislabs/redisgraph A graph database module for Redis 15 [OK]
  24. arm64v8/redis Redis is an open source key-value store that… 12
  25. redislabs/redismod An automated build of redismod - latest Redi… 12 [OK]
  26. webhippie/redis Docker images for Redis 11 [OK]
  27. insready/redis-stat Docker image for the real-time Redis monitor… 10 [OK]
  28. s7anley/redis-sentinel-docker Redis Sentinel 10 [OK]
  29. goodsmileduck/redis-cli redis-cli on alpine 9 [OK]
  30. circleci/redis CircleCI images for Redis 7 [OK]
  31. centos/redis-32-centos7 Redis in-memory data structure store, used a… 5
  32. clearlinux/redis Redis key-value data structure server with t… 3
  33. wodby/redis Redis container image with orchestration 1 [OK]
  34. tiredofit/redis Redis Server w/ Zabbix monitoring and S6 Ove… 1 [OK]
  35. xetamus/redis-resource forked redis-resource 0 [OK]
  36. PS C:\Users\DELL>

NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建。

删除镜像 docker rmi

Usage: docker rmi [OPTIONS] IMAGE [IMAGE…]

  1. PS C:\Users\DELL> docker rmi --help
  2. Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
  3. Remove one or more images
  4. Options:
  5. -f, --force Force removal of the image
  6. --no-prune Do not delete untagged parents
  7. PS C:\Users\DELL> docker images
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. minio/minio latest f55b2545c435 2 days ago 191MB
  10. redis latest bc8d70f9ef6c 5 days ago 105MB
  11. httpd latest f3cffeea581b 5 days ago 138MB
  12. iredmail/mariadb stable e4899534d71b 3 weeks ago 761MB
  13. docker/getting-started latest 3ba8f2ff0727 2 months ago 27.9MB
  14. lejmr/iredmail mysql-latest f719a06cbe14 9 months ago 861MB
  15. PS C:\Users\DELL> docker rmi httpd:latest
  16. Untagged: httpd:latest
  17. Untagged: httpd@sha256:e4c2b93c04762468a6cce6d507d94def02ef4dc285278d0d926e09827f4857db
  18. Deleted: sha256:f3cffeea581b3306a13d80b25a437f73f767b8f27afdb29393b3764b9dfaea69
  19. Deleted: sha256:887d0217b7251e4d5ad46486c19c5e2c82ae8aa6d53c3ffb729f032028a5f1c9
  20. Deleted: sha256:e04f6a8ae4f2939d3b73fd06085169c625486a81d3823fba3fdad029b2017347
  21. Deleted: sha256:85643304b5484a30ffe669196e71d6b763661800e12beb7ff131615040906adb
  22. Deleted: sha256:7524155083fa19d15b8902105916a8da484d1ff5d362d3530d871511c1f13f2a
  23. PS C:\Users\DELL>

更新 update、构建 build、提交 commit、发布 push 镜像

参考:https://www.runoob.com/docker/docker-image-usage.html

  1. 创建镜像
  2. 当我们从 docker 镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。
  3. 1、从已经创建的容器中更新镜像,并且提交这个镜像
  4. 2、使用 Dockerfile 指令来创建一个新的镜像
  5. 更新镜像
  6. 更新镜像之前,我们需要使用镜像来创建一个容器。
  7. runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
  8. root@e218edb10161:/#
  9. 在运行的容器内使用 apt-get update 命令进行更新。
  10. 在完成操作之后,输入 exit 命令来退出这个容器。
  11. 此时 ID e218edb10161 的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit 来提交容器副本。
  12. runoob@runoob:~$ docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2
  13. sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8
  14. 各个参数说明:
  15. -m: 提交的描述信息
  16. -a: 指定镜像作者
  17. e218edb10161:容器 ID
  18. runoob/ubuntu:v2: 指定要创建的目标镜像名
  19. 我们可以使用 docker images 命令来查看我们的新镜像 runoob/ubuntu:v2
  20. runoob@runoob:~$ docker images
  21. REPOSITORY TAG IMAGE ID CREATED SIZE
  22. runoob/ubuntu v2 70bf1840fd7c 15 seconds ago 158.5 MB
  23. ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB
  24. php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
  25. nginx latest 6f8d099c3adc 12 days ago 182.7 MB
  26. mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
  27. httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
  28. ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB
  29. hello-world latest 690ed74de00f 6 months ago 960 B
  30. training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
  31. 使用我们的新镜像 runoob/ubuntu 来启动一个容器
  32. runoob@runoob:~$ docker run -t -i runoob/ubuntu:v2 /bin/bash
  33. root@1a9fbdeb5da3:/#
  34. 构建镜像
  35. 我们使用命令 docker build 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。
  36. runoob@runoob:~$ cat Dockerfile
  37. FROM centos:6.7
  38. MAINTAINER Fisher "fisher@sudops.com"
  39. RUN /bin/echo 'root:123456' |chpasswd
  40. RUN useradd runoob
  41. RUN /bin/echo 'runoob:123456' |chpasswd
  42. RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
  43. EXPOSE 22
  44. EXPOSE 80
  45. CMD /usr/sbin/sshd -D
  46. 每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。
  47. 第一条FROM,指定使用哪个镜像源
  48. RUN 指令告诉docker 在镜像内执行命令,安装了什么。。。
  49. 然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像。
  50. runoob@runoob:~$ docker build -t runoob/centos:6.7 .
  51. Sending build context to Docker daemon 17.92 kB
  52. Step 1 : FROM centos:6.7
  53. ---> d95b5ca17cc3
  54. Step 2 : MAINTAINER Fisher "fisher@sudops.com"
  55. ---> Using cache
  56. ---> 0c92299c6f03
  57. Step 3 : RUN /bin/echo 'root:123456' |chpasswd
  58. ---> Using cache
  59. ---> 0397ce2fbd0a
  60. Step 4 : RUN useradd runoob
  61. ......
  62. 参数说明:
  63. -t :指定要创建的目标镜像名
  64. . Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
  65. 使用docker images 查看创建的镜像已经在列表中存在,镜像ID860c279d2fec
  66. runoob@runoob:~$ docker images
  67. REPOSITORY TAG IMAGE ID CREATED SIZE
  68. runoob/centos 6.7 860c279d2fec About a minute ago 190.6 MB
  69. runoob/ubuntu v2 70bf1840fd7c 17 hours ago 158.5 MB
  70. ubuntu 14.04 90d5884b1ee0 6 days ago 188 MB
  71. php 5.6 f40e9e0f10c8 10 days ago 444.8 MB
  72. nginx latest 6f8d099c3adc 12 days ago 182.7 MB
  73. mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
  74. httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
  75. ubuntu 15.10 4e3b13c8a266 5 weeks ago 136.3 MB
  76. hello-world latest 690ed74de00f 6 months ago 960 B
  77. centos 6.7 d95b5ca17cc3 6 months ago 190.6 MB
  78. training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
  79. 我们可以使用新的镜像来创建容器
  80. runoob@runoob:~$ docker run -t -i runoob/centos:6.7 /bin/bash
  81. [root@41c28d18b5fb /]# id runoob
  82. uid=500(runoob) gid=500(runoob) groups=500(runoob)
  83. 从上面看到新镜像已经包含我们创建的用户 runoob

设置镜像 tag

Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

  1. PS C:\Users\DELL> docker tag --help
  2. Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
  3. Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  4. PS C:\Users\DELL> docker tag httpd:latest httpd_1:latest_1
  5. PS C:\Users\DELL> docker images
  6. REPOSITORY TAG IMAGE ID CREATED SIZE
  7. minio/minio latest f55b2545c435 2 days ago 191MB
  8. redis latest bc8d70f9ef6c 5 days ago 105MB
  9. httpd latest f3cffeea581b 6 days ago 138MB
  10. httpd_1 latest_1 f3cffeea581b 6 days ago 138MB
  11. iredmail/mariadb stable e4899534d71b 3 weeks ago 761MB
  12. docker/getting-started latest 3ba8f2ff0727 2 months ago 27.9MB
  13. lejmr/iredmail mysql-latest f719a06cbe14 9 months ago 861MB
  14. PS C:\Users\DELL>