命令:docker image 镜像管理命令
Usage: docker image COMMAND
PS C:\Users\DELL> docker image --help
Usage: docker image COMMAND
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.
例子:管理命令举例
Usage: docker image ls [OPTIONS] [REPOSITORY[:TAG]]
PS C:\Users\DELL> docker image ls --help
Usage: docker image ls [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
ls, list
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
PS C:\Users\DELL> docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
minio/minio latest f55b2545c435 2 days ago 191MB
iredmail/mariadb stable e4899534d71b 3 weeks ago 761MB
docker/getting-started latest 3ba8f2ff0727 2 months ago 27.9MB
lejmr/iredmail mysql-latest f719a06cbe14 9 months ago 861MB
PS C:\Users\DELL>
命令使用
查看镜像列表 docker images
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
PS C:\Users\DELL> docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
PS C:\Users\DELL> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
minio/minio latest f55b2545c435 2 days ago 191MB
iredmail/mariadb stable e4899534d71b 3 weeks ago 761MB
docker/getting-started latest 3ba8f2ff0727 2 months ago 27.9MB
lejmr/iredmail mysql-latest f719a06cbe14 9 months ago 861MB
PS C:\Users\DELL>
拉取镜像 docker pull
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
PS C:\Users\DELL> docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform
capable
-q, --quiet Suppress verbose output
PS C:\Users\DELL> docker image pull --help
Usage: docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform
capable
-q, --quiet Suppress verbose output
PS C:\Users\DELL>
例子 docker pull
PS C:\Users\DELL> docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
69692152171a: Pull complete
7284b4e0cc7b: Pull complete
3678b2d55ccd: Pull complete
ab492cf0b2a4: Pull complete
991f7f97a9d8: Pull complete
Digest: sha256:e4c2b93c04762468a6cce6d507d94def02ef4dc285278d0d926e09827f4857db
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
PS C:\Users\DELL>
查询镜像 docker search
Usage: docker search [OPTIONS] TERM
PS C:\Users\DELL> docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
PS C:\Users\DELL> docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 9446 [OK]
bitnami/redis Bitnami Redis Docker Image 180 [OK]
sameersbn/redis 83 [OK]
grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2 78
rediscommander/redis-commander Alpine image for redis-commander - Redis man… 58 [OK]
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 34
redislabs/redisinsight RedisInsight - The GUI for Redis 30
redislabs/redis Clustered in-memory database engine compatib… 30
oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Supp… 25
redislabs/rejson RedisJSON - Enhanced JSON data type processi… 23
arm32v7/redis Redis is an open source key-value store that… 23
bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 22 [OK]
redislabs/redisgraph A graph database module for Redis 15 [OK]
arm64v8/redis Redis is an open source key-value store that… 12
redislabs/redismod An automated build of redismod - latest Redi… 12 [OK]
webhippie/redis Docker images for Redis 11 [OK]
insready/redis-stat Docker image for the real-time Redis monitor… 10 [OK]
s7anley/redis-sentinel-docker Redis Sentinel 10 [OK]
goodsmileduck/redis-cli redis-cli on alpine 9 [OK]
circleci/redis CircleCI images for Redis 7 [OK]
centos/redis-32-centos7 Redis in-memory data structure store, used a… 5
clearlinux/redis Redis key-value data structure server with t… 3
wodby/redis Redis container image with orchestration 1 [OK]
tiredofit/redis Redis Server w/ Zabbix monitoring and S6 Ove… 1 [OK]
xetamus/redis-resource forked redis-resource 0 [OK]
PS C:\Users\DELL>
NAME: 镜像仓库源的名称
DESCRIPTION: 镜像的描述
OFFICIAL: 是否 docker 官方发布
stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。
AUTOMATED: 自动构建。
删除镜像 docker rmi
Usage: docker rmi [OPTIONS] IMAGE [IMAGE…]
PS C:\Users\DELL> docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
PS C:\Users\DELL> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
minio/minio latest f55b2545c435 2 days ago 191MB
redis latest bc8d70f9ef6c 5 days ago 105MB
httpd latest f3cffeea581b 5 days ago 138MB
iredmail/mariadb stable e4899534d71b 3 weeks ago 761MB
docker/getting-started latest 3ba8f2ff0727 2 months ago 27.9MB
lejmr/iredmail mysql-latest f719a06cbe14 9 months ago 861MB
PS C:\Users\DELL> docker rmi httpd:latest
Untagged: httpd:latest
Untagged: httpd@sha256:e4c2b93c04762468a6cce6d507d94def02ef4dc285278d0d926e09827f4857db
Deleted: sha256:f3cffeea581b3306a13d80b25a437f73f767b8f27afdb29393b3764b9dfaea69
Deleted: sha256:887d0217b7251e4d5ad46486c19c5e2c82ae8aa6d53c3ffb729f032028a5f1c9
Deleted: sha256:e04f6a8ae4f2939d3b73fd06085169c625486a81d3823fba3fdad029b2017347
Deleted: sha256:85643304b5484a30ffe669196e71d6b763661800e12beb7ff131615040906adb
Deleted: sha256:7524155083fa19d15b8902105916a8da484d1ff5d362d3530d871511c1f13f2a
PS C:\Users\DELL>
更新 update、构建 build、提交 commit、发布 push 镜像
参考:https://www.runoob.com/docker/docker-image-usage.html
创建镜像
当我们从 docker 镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。
1、从已经创建的容器中更新镜像,并且提交这个镜像
2、使用 Dockerfile 指令来创建一个新的镜像
更新镜像
更新镜像之前,我们需要使用镜像来创建一个容器。
runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@e218edb10161:/#
在运行的容器内使用 apt-get update 命令进行更新。
在完成操作之后,输入 exit 命令来退出这个容器。
此时 ID 为 e218edb10161 的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit 来提交容器副本。
runoob@runoob:~$ docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2
sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8
各个参数说明:
-m: 提交的描述信息
-a: 指定镜像作者
e218edb10161:容器 ID
runoob/ubuntu:v2: 指定要创建的目标镜像名
我们可以使用 docker images 命令来查看我们的新镜像 runoob/ubuntu:v2:
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/ubuntu v2 70bf1840fd7c 15 seconds ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB
php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
使用我们的新镜像 runoob/ubuntu 来启动一个容器
runoob@runoob:~$ docker run -t -i runoob/ubuntu:v2 /bin/bash
root@1a9fbdeb5da3:/#
构建镜像
我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。
runoob@runoob:~$ cat Dockerfile
FROM centos:6.7
MAINTAINER Fisher "fisher@sudops.com"
RUN /bin/echo 'root:123456' |chpasswd
RUN useradd runoob
RUN /bin/echo 'runoob:123456' |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D
每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。
第一条FROM,指定使用哪个镜像源
RUN 指令告诉docker 在镜像内执行命令,安装了什么。。。
然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像。
runoob@runoob:~$ docker build -t runoob/centos:6.7 .
Sending build context to Docker daemon 17.92 kB
Step 1 : FROM centos:6.7
---> d95b5ca17cc3
Step 2 : MAINTAINER Fisher "fisher@sudops.com"
---> Using cache
---> 0c92299c6f03
Step 3 : RUN /bin/echo 'root:123456' |chpasswd
---> Using cache
---> 0397ce2fbd0a
Step 4 : RUN useradd runoob
......
参数说明:
-t :指定要创建的目标镜像名
. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
使用docker images 查看创建的镜像已经在列表中存在,镜像ID为860c279d2fec
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/centos 6.7 860c279d2fec About a minute ago 190.6 MB
runoob/ubuntu v2 70bf1840fd7c 17 hours ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 6 days ago 188 MB
php 5.6 f40e9e0f10c8 10 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 5 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
centos 6.7 d95b5ca17cc3 6 months ago 190.6 MB
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
我们可以使用新的镜像来创建容器
runoob@runoob:~$ docker run -t -i runoob/centos:6.7 /bin/bash
[root@41c28d18b5fb /]# id runoob
uid=500(runoob) gid=500(runoob) groups=500(runoob)
从上面看到新镜像已经包含我们创建的用户 runoob。
设置镜像 tag
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
PS C:\Users\DELL> docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
PS C:\Users\DELL> docker tag httpd:latest httpd_1:latest_1
PS C:\Users\DELL> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
minio/minio latest f55b2545c435 2 days ago 191MB
redis latest bc8d70f9ef6c 5 days ago 105MB
httpd latest f3cffeea581b 6 days ago 138MB
httpd_1 latest_1 f3cffeea581b 6 days ago 138MB
iredmail/mariadb stable e4899534d71b 3 weeks ago 761MB
docker/getting-started latest 3ba8f2ff0727 2 months ago 27.9MB
lejmr/iredmail mysql-latest f719a06cbe14 9 months ago 861MB
PS C:\Users\DELL>