程序猿
微录

Docker 常用命令 【镜像管理】

程序猿微录 发布于: 2020-04-28 04:15 157 0 0 0
首页
文章
专栏
问答
寄语
公告
  • 前往登录

Docker 常用命令 【镜像管理】

程序猿微录 发布于 2020-04-28 04:15 157 0 0 0
所属文册: Docker 文章标签: docker 、image

镜像命令

命令:docker images
功能:罗列本地镜像仓库中的镜像列表,等价于docker image ls
描述:
用法:     docker images [OPTIONS] [REPOSITORY[:TAG]]
Options:
  -a, --all             展示全部镜像,默认不展示中介镜像
      --digests         展示摘要
  -f, --filter filter   过滤结果
      --format string   使用Go模板语法格式化输出结果
      --no-trunc        不截断输出结果
  -q, --quiet           只展示ID信息

示例

[root@tinyice ~]# docker images -a
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
zk                      mine                205b50d027e5        26 hours ago        271MB
zookeeper               latest              a873528df41f        5 days ago          225MB
[root@tinyice ~]# docker images --format '{{.ID}}\t{{.Tag}}'
205b50d027e5    mine
a873528df41f    latest
[root@tinyice ~]# docker images -f reference=*/ka*ka
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
wurstmeister/kafka   latest             cd 988f4a6ca13c        11 days ago         421MB

配置项

镜像过滤条件配置项
dangling
true 或者false ,表示镜像信息是否含有标签
label
镜像的Label信息,格式为label=<key>或者label=<key>=<value>
before
在指定镜像之前拉取的镜像,指定格式:<image-name>[:<tag>]、 <image id> 或者<image@digest>
since
在指定镜像之后拉取的镜像,指定格式:<image-name>[:<tag>]、 <image id> 或者<image@digest>
reference
模糊匹配镜像的某个信息

模板参数

容器输出格式GO模板参数
.ID
镜像ID
.Repository
镜像仓库
.Tag
镜像标签
.Digest
镜像摘要
.CreatedSince
自镜像创建以来经过的时间
.CreatedAt
创建镜像的时间
.Size
镜像文件大小

命令:docker tag
功能:给目标镜像创建一个新标签的镜像
描述:
用法:   docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
示例
[root@tinyice ~]# docker tag zk:mine  zk:me
[root@tinyice ~]# docker images -a
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
zk                      me                  205b50d027e5        26 hours ago        271MB
zk                      mine                205b50d027e5        26 hours ago        271MB

命令:docker pull
功能:从镜像远程仓库拉取镜像,如果已存在则更新
描述:
用法:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Options:
  -a, --all-tags                下载指定镜像仓库的所有标签镜像
      --disable-content-trust   跳过镜像的验证流程,默认true
示例
[root@tinyice ~]# docker pull zookeeper
Using default tag: latest
latest: Pulling from library/zookeeper
...

命令:docker push
功能: 从本地镜像仓库推送镜像至远程镜像仓库,需要先登录
描述:
用法:      docker push [OPTIONS] NAME[:TAG]
Options:
      --disable-content-trust   忽略镜像校验,默认true
示例
[root@tinyice ~]# docker pull zk:v1


命令:docker save
功能: 打包目标镜像为tar结构到指定目录
描述:
用法:      docker save [OPTIONS] IMAGE [IMAGE...]
Options:
  -o, --output string   输出目录,默认当前位置

示例

[root@tinyice ~]# docker save -o zktest.tar zk
[root@tinyice ~]# ls
zktest.tar


命令:docker import
功能:导入并创建指定目录的tar结构镜像
描述:
用法:    docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Options:
  -c, --change list       根据DockerFile创建镜像
  -m, --message string   导入镜像的备注信息

示例

[root@tinyice ~]# docker import zktest.tar zk:test
sha256:a6c764aafe72b384cb7d752ae6908a474ecbea51d91923fb726c05da7aaa2535
[root@tinyice ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
zk                      test                a6c764aafe72        6 seconds ago       277MB
zk                      me                  205b50d027e5        27 hours ago        271MB
zk   


命令:docker load
功能:从tar结构存档或者标准输入终端加载并创建镜像
说明:只能load通过save命令生成的tar,且会保留镜像中的历史信息,不能重命名镜像和标签,相当于备份的恢复
描述:
用法:     docker load [OPTIONS]
Options:
  -i, --input string   从tar结构存档加入,而不是标准输入终端
  -q, --quiet          禁止加载过程中输出内容
示例
[root@tinyice ~]# docker save -o zk.tar zookeeper
[root@tinyice ~]# docker load -i zk.tar
Loaded image: zookeeper:latest
[root@tinyice ~]# docker load < zk.tar
Loaded image: zookeeper:latest


命令:docker rmi
功能: 删除镜像
描述:
用法:  docker rmi [OPTIONS] IMAGE [IMAGE...]
Options:
  -f, --force      默认删除非运行中容器使用的镜像,这里强制删除镜像
      --no-prune   不删除该镜像的中介镜像,默认删除

示例

[root@tinyice ~]# docker rmi zk
Error: No such image: zk
[root@tinyice ~]# docker rmi zk:test
Untagged: zk:test
Deleted: sha256:a6c764aafe72b384cb7d752ae6908a474ecbea51d91923fb726c05da7aaa2535

[root@tinyice ~]# docker rmi -f 205b50d027e5
Untagged: zk:me
Untagged: zk:mine
Deleted: sha256:205b50d027e534a315a90317e841e5272fec713fd2cfc229ce67b9693d685360
Deleted: sha256:781fec91a5e86fab71a604be26d649655727e9d48295565b67227008abddb85f


命令:docker image prune
功能:删除所有未被使用的镜像,版本要求v1.25+
描述:
用法:    docker image prune [OPTIONS]
Options:
  -a, --all             删除所有未使用的镜像,默认删除无标签(dangling)的镜像
      --filter filter   过滤条件 (e.g. 'until=<timestamp>')
  -f, --force           删除时不需要它确认提示

示例

[root@tinyice ~]# docker image prune --filter dangling=true
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

[root@tinyice ~]# docker image prune -a -f
Total reclaimed space: 0B


命令:docker build
功能:构建新的镜像
描述:
用法:    docker build [OPTIONS] PATH | URL | -
Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          构建时的变量
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   跳过内容校验,默认开启
  -f, --file string             DockerFile文件位置(这里文件名称不固定),默认在PATH|URL下查找固定名称为DockerFile的文件
      --force-rm                删除中间容器
      --iidfile string          Write the image ID to the file
      --isolation string        容器隔离策略
      --label list              镜像的Label元信息
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build (default "default")
      --no-cache                Do not use cache when building the image
      --pull                    尝试拉取最新版本的镜像
  -q, --quiet                   禁止构建过程中的输出,构建完毕后只输出镜像ID
      --rm                      Remove intermediate containers after a successful build (default true)
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
  -t, --tag list                标签列表,格式为:<name>:<tag>
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])

示例

[root@tinyice ~]# docker build -t my-image:latest .
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /root/Dockerfile: no such file or directory


命令:inspect
功能:展示Docker对象(镜像、容器)的详细信息
描述:
用法:    docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Options:
  -f, --format string   使用Go模板格式化输出
  -s, --size            展示容器文件大小
      --type string     当容器名称冲突时指定类型:image/container

示例

[root@tinyice ~]# docker inspect zookeeper:latest
[
    {
        "Id": "sha256:a873528df41f67308900374218cbcb334bf764074f9daa76482d86e53f058589",
        "RepoTags": [
            "zookeeper:latest"
        ],
        "RepoDigests": [
            "zookeeper@sha256:4a70c5b011d94298f62c62d7341a374b6a3a5d15a246f76d4cba3584ae60b8f6"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-07-18T02:36:33.558188597Z",
        "Container": "7cc264481a625e12c5e2dfd9ea899ec54234fe3c089b28860fef783535e2d430",
...


命令:search
功能:从镜像仓库查询镜像列表
描述:
Usage:    docker search [OPTIONS] TERM
用法:    docker search [首选项] 镜像名称[:标签|签名] 
作用:    从镜像仓库查询镜像
首选项【options】:
  -f, --filter filter        根据过滤条件过滤输出
      --format string   使用Go模板格式化输出
      --limit int            展示的条数,默认25
      --no-trunc          显示完整的镜像信息

示例

[root@localhost l]# docker search mysql:5.7 【docker search 镜像名称:镜像标签 ===>从镜像仓库查询镜像】
NAME  【镜像名称】      DESCRIPTION  【镜像描述】                        STARS 【镜像状态】   OFFICIAL        AUTOMATED
mysql                   MySQL is a widely used, open-source relation…      5490                [OK]                
mariadb                 MariaDB is a community-developed fork of MyS…      1696                [OK]                
...