Docker 最常用的镜像命令和容器命令
本文列出了 Docker 使用过程中最常用的镜像命令和容器命令,以及教大家如何操作容器数据卷,实现容器数据的备份。熟练练习这些命令以后,再来一些简单的应用部署练习,大家就可以学习 Docker 的镜像构建、备份恢复迁移、镜像仓库、网络、集群等等更多的内容。
文章目录
一.帮助命令
docker version
: 查看Docker版本信息docker info
: 查看Docker信息docker --help
: 查看帮助信息
二、镜像相关命令及其基本操作
官方文档:https://docs.docker.com/reference/
登入镜像仓库
-
docker login [选项] [镜像仓库URL]
# 这里以阿里云实例 [root@Stupidkid ~]# sudo docker login --username=兴欣工作室 registry.cn-hangzhou.aliyuncs.com/stupid_kid/stupid_kid Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded #登录成功
-
选项
--username=xxx
: 指定阿里云用户名(我用的是阿里云) -
passwd : 是创建仓库时的密码(阿里云账号登录密码)
-
url : 仓库链接
获取镜像
-
格式:docker pull [镜像仓库URL]/[命名空间名称]/[仓库名称]:[镜像版本号]
-
示例 :
docker pull docker.io/library/busybox:latest
-
字段说明
URL 命名空间 仓库名称 版本号 docker.io library busybox latest
[root@Stupidkid ~]# docker pull docker.io/library/busybox:latest
latest: Pulling from library/busybox
ea97eb0eb3ec: Pull complete
Digest: sha256:bde48e1751173b709090c2539fdf12d6ba64e88ec7a4301591227ce925f3c678
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
- 简化:
docker pull busybox:latest
(不指定则默认仓库)
-
可以直接使用
docker run
,如果本地没有镜像会自动去仓库拉取。[root@localhost ~ ]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582838d92970a09f323 Status: Downloaded newer image for hello-world:latest ...
下载的时候,我们可以看到有若干层组成,像 0e03bdcc26d7 这样的字符串是层的唯一 ID(实际上,完整的 ID 包括 256 比特, 64 个十六进制字符组成)。使用 docker pull 命令下载中会获取并输出镜像的各层信息。当不同的镜像包括相同的层的时候,本地仅存一份内容,减小存储空间。
查看镜像基本信息
使用docker images
或 docker images ls
命令可以列举本地主机上已有镜像的基本信息。
- 基本用法:
docker images [选项]
/docker images ls [选项]
[root@Stupidkid ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 219ee5171f80 2 days ago 1.23MB
hello-world latest bf756fb1ae65 11 months ago 13.3kB
这些镜像都是存储在 Docker 宿主机的 /var/lib/docker 目录下。
- 字段说明
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
---|---|---|---|---|
仓库名称 | 版本号(又称镜像标签)(latest:表示最新版本) | 镜像ID | 镜像创建时间到现在 | 镜像文件的体积 |
同一仓库源可以有多个"TAG",代表这个仓库源的不同个版本,我们使用" REPOSITORY:TAG" 来定义不同的镜像
如果你不指定一个镜像的版本标签,例如你只使用"ubuntu","docker"将默认使用"ubuntu:latest"镜像
其中镜像的 ID 信息十分重要,它唯一标识了镜像。在使用镜像 ID 的时候,一般可以使用该 ID 的前若干个字符组成的可区分串来替代完整的 ID。
TAG 信息用于标记来自同一个仓库的不同镜像。TAG 在同一个仓库中是唯一的。
镜像大小信息只是表示了该镜像的逻辑体积大小,实际上由于相同的镜像层本地只会存储一份,物理上占用 的存储空间会小于各镜像逻辑体积之和。
docker images
命令常用选项
-a
: 显示所有的镜像(包括临时镜像文件)
[root@Stupidkid ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 219ee5171f80 2 days ago 1.23MB
hello-world latest bf756fb1ae65 11 months ago 13.3kB
-q : 只显示镜像ID
[root@Stupidkid ~]# docker images -q
219ee5171f80
bc9a0695f571
bf756fb1ae65
--digests
: 显示镜像再要信息
[root@Stupidkid ~]# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
busybox latest sha256:bde48e1751173b709090c2539fdf12d6ba64e88ec7a4301591227ce925f3c678 219ee5171f80 2 days ago 1.23MB
nginx latest sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3 bc9a0695f571 11 days ago 133MB
hello-world latest sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582838d92970a09f323 bf756fb1ae65 11 months ago 13.3kB
–digests=true | false:列出镜像的数字摘要值
[root@Stupidkid ~]# docker images --digests=true
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
busybox latest sha256:bde48e1751173b709090c2539fdf12d6ba64e88ec7a4301591227ce925f3c678 219ee5171f80 2 days ago 1.23MB
nginx latest sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3 bc9a0695f571 11 days ago 133MB
hello-world latest sha256:e7c70bb24b462baa86c102610182e3efcb12a04854e8c582838d92970a09f323 bf756fb1ae65 11 months ago 13.3kB
[root@Stupidkid ~]# docker images --digests=false
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 219ee5171f80 2 days ago 1.23MB
nginx latest bc9a0695f571 11 days ago 133MB
hello-world latest bf756fb1ae65 11 months ago 13.3kB
--no-trunc
: 显示完整的镜像信息
[root@Stupidkid ~]# docker images --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest sha256:219ee5171f8006d1462fa76c12b9b01ab672dbc8b283f186841bf2c3ca8e3c93 2 days ago 1.23MB
nginx latest sha256:bc9a0695f5712dcaaa09a5adc415a3936ccba13fc2587dfd76b1b8aeea3f221c 11 days ago 133MB
hello-world latest sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b 11 months ago 13.3kB
查看镜像详细信息
使用docker inspect
命令获取镜像的详细信息,包括 PID、作者、架构等等。
-
基本格式:
docker inspect [镜像ID] / [镜像名称:版本号]
-
示例:
nginx:latest none [root@Stupidkid ~]# docker inspect nginx:latest [ { "Id": "sha256:bc9a0695f5712dcaaa09a5adc415a3936ccba13fc2587dfd76b1b8aeea3f221c", "RepoTags": [ "nginx:latest" ], "RepoDigests": [ "nginx@sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3" ], "Parent": "", "Comment": "", "Created": "2020-11-25T00:30:19.011398516Z", "Container": "279e6916c4aaaf5d61e468508abd96933f4e48194bd979dc692e0196cde2d59d", ...
docker inspect
命令选项
-f : 可以使用golang的语言模板语法获取所需信息。
[root@Stupidkid ~]# docker inspect -f '{{.Id}}' nginx
sha256:bc9a0695f5712dcaaa09a5adc415a3936ccba13fc2587dfd76b1b8aeea3f221c
为镜像添加tag
为了方便后续工作中使用特定的镜像,还可以使用 docker tag 命令来为本地的镜像添加标签、修改镜像名称和版本号。
-
格式 :
docker tag [原镜像仓库url]/[原镜像命名空间]/[原镜像仓库名称]:[版本号] [新镜像仓库url]/[新镜像命名空间]/[新镜像仓库名称]:[版本号]
或者如下:
docker tag [原镜像仓库url]/[原镜像命名空间]/[原镜像仓库名称]:[版本号] \ [新镜像仓库url]/[新镜像命名空间]/[新镜像仓库名称]:[版本号]
-
示例
[root@Stupidkid ~]# docker tag nginx:latest registry-vpc.cn-hangzhou.aliyuncs.com/stupid_kid/stupid_kid/nginx:v1 # 修改镜像名与TAG [root@Stupidkid ~]# docker tag nginx:latest nginx_1:v1 [root@Stupidkid ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest 219ee5171f80 2 days ago 1.23MB registry.cn-shanghai.aliyuncs.com/python16-shawn/busybox v1 219ee5171f80 2 days ago 1.23MB nginx latest bc9a0695f571 11 days ago 133MB nginx_1 v1 bc9a0695f571 11 days ago 133MB registry-vpc.cn-hangzhou.aliyuncs.com/stupid_kid/stupid_kid/nginx v1 bc9a0695f571 11 days ago 133MB hello-world latest bf756fb1ae65 11 months ago 13.3kB
修改tag后,源镜像还会存在,ID则为同一个,在删除镜像时若指定的是ID则会删除所有匹配到ID的镜像,若指定的是镜像名称:版本
则仅删除指定名称的单个镜像。
查看镜像构建历史
-
格式 :
docker history [镜像名字:镜像版本号] / [镜像ID]
-
示例:
[root@Stupidkid ~]# docker history nginx:latest IMAGE CREATED CREATED BY SIZE COMMENT bc9a0695f571 11 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B <missing> 11 days ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B <missing> 11 days ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 11 days ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B <missing> 11 days ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB <missing> 11 days ago /bin/sh -c #(nop) COPY file:08ae525f517706a5… 1.95kB <missing> 11 days ago /bin/sh -c #(nop) COPY file:e7e183879c35719c… 1.2kB <missing> 11 days ago /bin/sh -c set -x && addgroup --system -… 63.6MB <missing> 11 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B <missing> 11 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.4.4 0B <missing> 11 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.19.5 0B <missing> 2 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B <missing> 2 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 2 weeks ago /bin/sh -c #(nop) ADD file:d2abb0e4e7ac17737… 69.2MB
搜索镜像
在 docker 中搜索镜像主要使用 Search 子命令,默认只搜索 Docker Hub 官方镜像仓库中的镜像。
-
格式 :
docker search [所搜索的镜像名称] [选项]
-
示例:
[root@Stupidkid ~]# docker search python NAME DESCRIPTION STARS OFFICIAL AUTOMATED python Python is an interpreted, interactive, objec… 5661 [OK] django Django is a free web application framework, … 1024 [OK] pypy PyPy is a fast, compliant alternative implem… 256 [OK] nikolaik/python-nodejs Python with Node.js 55 [OK] joyzoursky/python-chromedriver Python with Chromedriver, for running automa… 54 [OK] arm32v7/python Python is an interpreted, interactive, objec… 53 circleci/python Python is an interpreted, interactive, objec… 41 centos/python-35-centos7 Platform for building and running Python 3.5… 38 centos/python-36-centos7 Platform for building and running Python 3.6… 30 hylang Hy is a Lisp dialect that translates express… 28 [OK] arm64v8/python Python is an interpreted, interactive, objec… 24 centos/python-27-centos7 Platform for building and running Python 2.7… 17 bitnami/python Bitnami Python Docker Image 10 [OK] publicisworldwide/python-conda Basic Python environments with Conda. 6 [OK] dockershelf/python Repository for docker images of Python. Test… 5 [OK] clearlinux/python Python programming interpreted language with… 4 d3fk/python_in_bottle Simple python:alpine completed by Bottle+Req… 4 [OK] i386/python Python is an interpreted, interactive, objec… 3 centos/python-34-centos7 Platform for building and running Python 3.4… 2 ppc64le/python Python is an interpreted, interactive, objec… 2 amd64/python Python is an interpreted, interactive, objec… 1 saagie/python Repo for python jobs 0 s390x/python Python is an interpreted, interactive, objec… 0 ccitest/python CircleCI test images for Python 0 [OK] openshift/python-33-centos7 DEPRECATED: A Centos7 based Python v3.3 imag… 0
-
字段说明:
NAME
:镜像名称DESCRIPTION
:镜像描述STARS
:用户评价,反映一个镜像的受欢迎程度(收藏个数)OFFICIAL
:是否为官方构建AUTOMATED
:自动构建,表示该镜像由 Docker Hub 自动构建流程创建的。
docker search
命令常用选项:
-f :过滤
# 搜索被收藏超过 300 个的并且关键词包括 Python 的镜像
docker search -f stars=300 python
# 搜索官方提供的带有 Redis 关键字的镜像
docker search -f is-official=true redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 8792 [OK]
-s
:列出搜藏不小于指定值的镜像
- 示例:
docker search python -s 300
--automated
: 只列出 automated build 类型的镜像
- 示例:
docker search python --automated
--limit
: 限制输出结果
–no-trunc: 不截断输出结果
删除镜像
使用docker rmi命令,相当于docker image rm命令。
- 格式 :
docker rmi [镜像名称:版本号] / [镜像ID]
示例:
[root@Stupidkid ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 219ee5171f80 2 days ago 1.23MB
registry.cn-shanghai.aliyuncs.com/python16-shawn/busybox v1 219ee5171f80 2 days ago 1.23MB
nginx latest bc9a0695f571 11 days ago 133MB
nginx_1 v1 bc9a0695f571 11 days ago 133MB
registry-vpc.cn-hangzhou.aliyuncs.com/stupid_kid/stupid_kid/nginx v1 bc9a0695f571 11 days ago 133MB
hello-world latest bf756fb1ae65 11 months ago 13.3kB
[root@Stupidkid ~]# docker rmi nginx:v1
Error: No such image: nginx:v1
[root@Stupidkid ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 219ee5171f80 2 days ago 1.23MB
registry.cn-shanghai.aliyuncs.com/python16-shawn/busybox v1 219ee5171f80 2 days ago 1.23MB
nginx latest bc9a0695f571 11 days ago 133MB
nginx_1 v1 bc9a0695f571 11 days ago 133MB
registry-vpc.cn-hangzhou.aliyuncs.com/stupid_kid/stupid_kid/nginx v1 bc9a0695f571 11 days ago 133MB
hello-world latest bf756fb1ae65 11 months ago 13.3kB
docker rmi命令选项
-f : 强制删除。镜像已经运行为容器则无法删除,可以使用-f强制删除。
示例:删除单个
[root@localhost ~ ]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9bf5ef44a314 nginx "/docker-entrypoint.…" 51 minutes ago Up 51 minutes 0.0.0.0:32770->80/tcp funny_northcutt
[root@localhost ~ ]# docker rmi nginx:latest
Error response from daemon: conflict: unable to remove repository reference "nginx:latest" (must force) - container b1bc639c450e is using its referenced image bc9a0695f571
[root@localhost ~ ]# docker rmi -f nginx:latest
Untagged: nginx:latest
[root@localhost ~ ]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_2 v1 2c45f814f15b 38 minutes ago 131MB
<none> <none> bc9a0695f571 7 days ago 133MB
删除多个:
docker rmi -f [镜像名1:TAG] [镜像名2:TAG]...
删除全部:
docker rmi -f $(docker images -qa)
清理镜像
使用一段时间之后,docker 会产生很多临时镜像文件,以及一些没有被使用的镜像, 我们可以通过 docker image prune 命令来进行清理。
- 格式:docker image prune [选项]
docker image prune
命令选项
- -a:清理所有没有当前使用的镜像,不仅是临时镜像。
- -f: 强制删除。等同于rmi删除。
推送镜像
push将本地镜像推送到网上的个人的私有仓库中,例如阿里云的私有仓库。
-
格式 :
docker push [镜像仓库URL]/[命名空间名称]/[仓库名称]:[版本号]
-
示例:
-
先登录私有仓库。
docker login --username=“登录名,最好纯英文” 仓库URL [root@Stupidkid ~]# docker login --username="兴欣工作室" registry.cn-hangzhou.aliyuncs.com/stupid_kid/stupid_kid Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
-
将镜像改名
# 私有仓库名最好设置为镜像名,在私有仓库内只能查看版本,而没有镜像名。 docker tag 本地镜像名 [镜像仓库URL]/[命名空间名称]/[仓库名称]:[版本号]
-
推送镜像,push一次只能推送一个镜像。
[root@Centos7 docker]# docker push registry.cn-hangzhou.aliyuncs.com/alvinos/py15-nginx:1.19.2 The push refers to repository [registry.cn-hangzhou.aliyuncs.com/alvinos/py15-nginx] 908cf8238301: Pushed eabfa4cd2d12: Pushed 60c688e8765e: Pushed f431d0917d41: Pushed 07cab4339852: Pushed 1.19.2: digest: sha256:794275d96b4ab96eeb954728a7bf11156570e8372ecd5ed0cbc7280313a27d19 size: 1362
-
构建镜像
构建镜像一般有三种情况,基于容器导入、基于本地模板导入、基于 Dockerfile 创建,本节主讲基于容器保存镜像和本地保存镜像文件导入。
1、保存容器为镜像
- 格式:
docker commit [选项] [容器ID] / [容器名称:版本号]
docker commit
命令选项
- -a : 指定作者。
- -m : 简介。
- -p : 保存镜像时,容器暂停运行。
示例:
[root@localhost ~ ]# docker commit -a 'chirou' -m 'nginx_demo' -p 50cf6c577510
sha256:94738e2585944aa455e0c3e1bb174fba02dc18ea17135811d1b874ea0beaab7e
[root@localhost ~ ]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 94738e258594 4 seconds ago 133MB
# 使用inspect查看详细信息。
[root@localhost ~ ]# docker inspect 94738e258594 | grep chirou
"Author": "chirou",
[root@localhost ~ ]# docker inspect 94738e258594 | grep nginx_demo
"Comment": "nginx_demo",
2、导入与导出容器
某些时候,需要将容器或镜像保存成文件从一个系统迁移到另外一个系统,此时可以使用 Docker 的导入和导出功能,这也是Docker 自身提供的一个重要特性。导出的文件是一个tar包,可以通过压缩命令进行压缩,然后进行传输。
export
和import
比较
export
export将容器导出到标准输出,可以使用输出重定向或-o选项至文件中。
-
格式:
docker export [容器名或ID] > [文件名称]
-
示例:将nginx容器导出
docker export nginx > export_nginx.tar
import
import 是将export导出的文件导入为镜像,可以自定义导入的镜像名称和版本号。
import虽然可以导入save保存的文件但是导入后无法运行。
-
格式:
docker import [文件名称] [自定义镜像名称]:[版本号]
-
示例:将上面导出的文件导入
[root@localhost ~ ]# docker import export_nginx.tar import_nginx:v1 87b71baffd0c8e5b2b98884caee97a9d3abcee444e9bad10c865db0daaafa024 [root@localhost ~ ]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE import_nginx v1 87b71baffd0c 44 seconds ago 131MB nginx latest bc9a0695f571 10 days ago 133MB # 通过export导出的文件,使用import导入后无法直接运行。 [root@localhost mnt ]# docker run -d import_nginx:v1 docker: Error response from daemon: No command specified.
可以看到,通过export导出的容器文件,再使用import导入后,SIZE是要比源镜像小的。这是因为export导出的是容器,并没有源镜像的全部内容,比如会丢失构建历史记录和元数据信息等文件,相当于仅保存容器当时的快照状态,这会导致export导出的文件无法直接通过run命令运行,解决方法:
运行时通过-it选项,给容器分配一个伪终端。
docker run -dit import_nginx:v1 sh
推荐使用commit来保存容器,然后再使用save保存,能避免上述问题。
3、导入和导出镜像
save和load
save能将镜像完整的保存下来,包括镜像ID和构建历史。一样可用输出重定向或-o选项保存至tar包中,并且save支持将多个镜像保存至一个tar包中。
格式:
docker save [镜像名或ID ...] > [压缩包名称]
docker save -o [压缩包名称] [镜像名称或ID ...]
实例:
# 不指定版本号则默认为latest,表示最新版。
[root@localhost ~ ]# docker save busybox nginx > box_nginx.tar
[root@localhost ~ ]# ll
-rw-r--r-- 1 root root 138553344 12月 5 19:30 box_nginx.tar
load是将save保存的镜像文件载入为镜像。save保存时若使用镜像ID保存镜像,导入时则没有镜像名称,load在导入时也不能自定义镜像名称,可以在导入后使用docker tag命令修改。
格式:
docker load < [压缩包名称]
<相当于-i选项,指定导入的文件,默认是STDIN。
实例:
[root@localhost ~ ]# docker save dc3bacd8b5ea bc9a0695f571 > box_nginx.tar
# 将原镜像删除
[root@localhost ~ ]# docker rmi bc9a0695f571 dc3bacd8b5ea
# 载入镜像
[root@localhost ~ ]# docker load < box_nginx.tar
Loaded image ID: sha256:bc9a0695f5712dcaaa09a5adc415a3936ccba13fc2587dfd76b1b8aeea3f221c
Loaded image ID: sha256:dc3bacd8b5ea796cea5d6070c8f145df9076f26a6bc1c8981fd5b176d37de843
# 此时ID还是原来的,但tag信息都为none
[root@localhost ~ ]# docker images
<none> <none> bc9a0695f571 10 days ago 133MB
<none> <none> dc3bacd8b5ea 11 days ago 1.23MB