Docker(三)[核心技术-镜像管理]

1.简介

  • 镜像是一个Docker的可执行文件,其中包括运行应用程序所需的所有代码内容、依赖库、环境变量和配置文件等。
  • 通过镜像可以创建一个或多个容器。
  • 当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。

2.搜索

搜索镜像仓库(Docker Hub)上的镜像资源

1.获取帮助

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

search相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/search/

2.命令格式

docker search [镜像名称]

例:

docker search ubuntu

在这里插入图片描述

字段标识 说明 备注
Name 名称 -
DESCRIPTION 基本功能描述 -
STARS 星级(关注程度) -
OFFICIAL 是否官方创建 是 [OK]
AUTOMATED 是否自动创建 是 [OK]

3.过滤条件

1.filter

只显示官方创建的Ubuntu

docker search --filter is-official=true ubuntu

在这里插入图片描述

只显示自动创建的

docker search --filter is-automated=true ubuntu

在这里插入图片描述

只显示星级500及以上的

docker search --filter stars=500 ubuntu
# stars=500即stars>=500

在这里插入图片描述

只显示:1.官方创建;2.星级500及以上

docker search --filter is-official=true --filter stars=500 ubuntu

在这里插入图片描述

2.format

格式化输出

占位符 描述
.Name 镜像名称
.Description 镜像描述
.StarCount 镜像星级
.IsOfficial 是否为官方创建
.IsAutomated 是否自动创建

只显示镜像名称是否自动创建是否为官方创建表格样式输出

docker search --format "table {{.Name}}\t{{.IsAutomated}}\t{{.IsOfficial}}" ubuntu

在这里插入图片描述

3.limit

搜索镜像Ubuntu,并显示前3条搜索结果

docker search --limit=3 ubuntu

在这里插入图片描述

4.no-trunc

显示完整的镜像描述

docker search --no-trunc ubuntu

在这里插入图片描述

3.获取

下载镜像仓库(Docker Hub)上的镜像资源

1.获取帮助

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)
  -q, --quiet                   Suppress verbose output

pull相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/pull/

2.命令格式

docker pull [镜像名称]

例:

docker pull ubuntu

在这里插入图片描述

1.信息查看

sudo cat /var/lib/docker/image/overlay2/repositories.json | python3 -m json.tool

如果没有Python3,建议安装,否则无法使用json.tool工具对json信息进行格式化打印:

sudo apt install python3

在这里插入图片描述

2.存储位置查看

sudo ls -la /var/lib/docker/image/overlay2/imagedb/content/sha256

在这里插入图片描述

4.查看

列出本地镜像资源

1.获取帮助

docker image --help
Usage:  docker image COMMAND

Manage images

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.

docker image ls 和docker images 等价

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

List images

Aliases:
  ls, images, 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 numeric IDs
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 numeric IDs

image相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/image/

images相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/images/

2.命令格式

1.image ls

docker image ls [镜像名称]

例:

docker image ls

在这里插入图片描述
列出所有的本地的images(包括已删除的镜像记录)

docker image ls -a

在这里插入图片描述

2.images

docker images [镜像名称]

例:

docker images

在这里插入图片描述
列出所有的本地的images(包括已删除的镜像记录)

docker images -a 

在这里插入图片描述

字段标识 说明 备注
REPOSITORY 镜像的仓库源 -
TAG 版本标签 区分不同发行版本,如果不指定具体标记,默认使用latest标记信息
IMAGE ID 镜像ID D唯一标识了镜像,如果ID相同,说明是同一镜像
CREATED 创建时间 -
SIZE 大小 -

5.重命名

对本地镜像的NAME、TAG进行重命名,并新产生一个命名后镜像

1.获取帮助

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

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

tag相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/tag/

2.命令格式

docker tag [老镜像名称]:[老镜像版本] [新镜像名称]:[新镜像版本]

例:

docker tag ubuntu:latest wyf-ubuntu:v1.0

在这里插入图片描述

只是多一个名词为wyf-ubuntu标签为v1.0,镜像ID并未改变,还是和原镜像一样。

6.删除

将本地的一个或多个镜像删除

1.获取帮助

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

rmi相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/rmi/

docker rmi 和docker image rm 等价

docker image rm --help
Usage:  docker image rm [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Aliases:
  rm, rmi, remove

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents

image rm相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/image_rm/

2.命令格式

docker rmi [命令参数][镜像ID]
docker rmi [命令参数][镜像名称]:[镜像版本]
docker image rm [命令参数][镜像]

如果一个image_id存在多个名称【使用tag重命名过】,那么应该使用 [REPOSITORY[:TAG]] 的格式删除镜像
例:

docker rmi wyf-ubuntu:v1.0

在这里插入图片描述

删除了仅仅是对一个的 [REPOSITORY[:TAG]] 的镜像

docker rmi 4e5021d210f6

删除同一个镜像ID的,如果没有使用tag进行重命名过会被删除,否则使用tag重命名过的所有原镜像和重命名后的镜像都无法删除,解决方案如下:

  • 1.使用命令参数 -f
docker rmi -f 4e5021d210f6

在这里插入图片描述

  • 2.使用 [REPOSITORY[:TAG]]列出进行删除
docker rmi ubuntu:latest wyf-ubuntu:v1.0 wyf-ubuntu:v1.1 wyf-ubuntu:v1.2

在这里插入图片描述

7.导出

将已经下载好的镜像,导出到本地。
将本地的一个或多个镜像打包保存成本地tar文件。

1.获取帮助

docker save --help
Usage:  docker save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

Options:
  -o, --output string   Write to a file, instead of STDOUT

save相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/save/

2.命令格式

docker save [命令参数][导出镜像名称][本地镜像名称]
docker save [命令参数][导出镜像名称][本地镜像名称:TAG标签]
docker save [本地镜像名称]>[导出镜像名称]
docker save [本地镜像名称:TAG标签]>[导出镜像名称]
# 参数
-o, --output string   		指定写入的文件名和路径

例:

docker save -o ubuntu.tar ubuntu 

在这里插入图片描述

8.导入

将save命令【导出】打包的镜像导入本地镜像库中

1.获取帮助

docker load --help
Usage:  docker load [OPTIONS]

Load an image from a tar archive or STDIN

Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output

load相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/load/

2.命令格式

docker load [命令参数][被导入镜像压缩文件的名称]
docker load < [被导入镜像压缩文件的名称]
docker load --input [被导入镜像压缩文件的名称]

例:

docker load < ubuntu.tar

如果发现导入的时候没有权限需要使用chmod命令修改镜像文件的权限
在这里插入图片描述

9.历史

查看本地一个镜像的历史(历史分层)信息

1.获取帮助

docker history --help
Usage:  docker history [OPTIONS] IMAGE

Show the history of an image

Options:
      --format string   Pretty-print images using a Go template
  -H, --human           Print sizes and dates in human readable format (default true)
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs

history相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/history/

2.命令格式

docker history [镜像名称]
docker history [镜像名称]:[镜像版本]
docker history [镜像ID]

例:

docker history ubuntu
docker history ubuntu:latest
docker history 4e5021d210f6

在这里插入图片描述

10.详细信息

查看本地一个或多个镜像的详细信息

1.获取帮助

docker image inspect --help
Usage:  docker image inspect [OPTIONS] IMAGE [IMAGE...]

Display detailed information on one or more images

Options:
  -f, --format string   Format the output using the given Go template

image inspect相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/image_inspect/
docker inspect --help
Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type

inspect相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/inspect/

2.命令格式

docker image inspect [命令参数] [镜像名称]:[镜像版本]
docker inspect [命令参数] [镜像ID]

例:

docker inspect ubuntu
[
    {
        "Id": "sha256:4e5021d210f65ebe915670c7089120120bc0a303b90208592851708c1b8c04bd",
        "RepoTags": [
            "ubuntu:latest"
        ],
        "RepoDigests": [],
        "Parent": "",
        "Comment": "",
        "Created": "2020-03-20T19:20:22.835345724Z",
        "Container": "f35b3af588999f5c47b8132845d7e6c3a220cedac21a8dada926f41de36eef55",
        "ContainerConfig": {
            "Hostname": "f35b3af58899",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:257f42741ca08b6d23bb7cc030bb3ce32278879eb87d1f7c8195ddf9057807cb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "18.09.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:257f42741ca08b6d23bb7cc030bb3ce32278879eb87d1f7c8195ddf9057807cb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 64205678,
        "VirtualSize": 64205678,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/c2588adf5a51a6bde1e98b995d5eb86781506685b1302784ffeadf1961700083/diff:/var/lib/docker/overlay2/a15aa45e3c04df6be36329bc47a451f9ca72f9dde755144fa2bf987152dc0e76/diff:/var/lib/docker/overlay2/c9c055c58ff29f15297254a286c03977cacc2a0164af89fb7053bc85c310f650/diff",
                "MergedDir": "/var/lib/docker/overlay2/6e87dd509a157e5f68c8e9ebae692b428be23041787c696667196889c9cddc91/merged",
                "UpperDir": "/var/lib/docker/overlay2/6e87dd509a157e5f68c8e9ebae692b428be23041787c696667196889c9cddc91/diff",
                "WorkDir": "/var/lib/docker/overlay2/6e87dd509a157e5f68c8e9ebae692b428be23041787c696667196889c9cddc91/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:c8be1b8f4d60d99c281fc2db75e0f56df42a83ad2f0b091621ce19357e19d853",
                "sha256:977183d4e9995d9cd5ffdfc0f29e911ec9de777bcb0f507895daa1068477f76f",
                "sha256:6597da2e2e52f4d438ad49a14ca79324f130a9ea08745505aa174a8db51cb79d",
                "sha256:16542a8fc3be1bfaff6ed1daa7922e7c3b47b6c3a8d98b7fca58b9517bb99b75"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

在这里插入图片描述

11.创建

根据模板创建镜像

进入系统模板镜像网站

https://download.openvz.org/template/precreated/

在这里插入图片描述
找到一个镜像模板进行下载,比如说ubuntu-16.04-x86_64.tar.gz,地址为:

https://download.openvz.org/template/precreated/ubuntu-16.04-x86_64.tar.gz

在这里插入图片描述

1.获取帮助

docker import --help
Usage:  docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Import the contents from a tarball to create a filesystem image

Options:
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Set commit message for imported image

search相关参数具体说明详见:

https://docs.docker.com/engine/reference/commandline/search/

2.命令格式

cat 模板文件名.tar | docker import - [自定义镜像名]

例:
1.下载镜像模板

wget https://download.openvz.org/template/precreated/ubuntu-16.04-x86_64.tar.gz

在这里插入图片描述
2.根据模板创建镜像

cat ubuntu-16.04-x86_64.tar.gz | docker import - ubuntu-mini

在这里插入图片描述
更多请参考:

https://docs.docker.com/engine/reference/commandline/docker/

或:

https://docker-doc.readthedocs.io/zh_CN/latest/reference/commandline/cli.html

在这里插入图片描述

发布了48 篇原创文章 · 获赞 14 · 访问量 4153

猜你喜欢

转载自blog.csdn.net/weixin_42366378/article/details/105362406