Docker Basics: Images

content

Images are one of the three core concepts of Docker. Before Docker can run the container, the corresponding image needs to exist locally. If there is no corresponding image locally, Docker will try to download it from the default image repository. Of course, users can also use custom image warehouses through configuration.
This article will introduce the specific operations of images , including using the pull command to pull (download) public images from Docker Hub's image repository; view information about existing images locally; use the search command to search for images; delete image labels and image files; create users Customize the image and upload it to the Docker Hub registry.
The operations related to the image are defined in the docker image subcommand. Although the format without image is still compatible, bringing the image will make the command easier to understand and have better auto-completion effects.

get mirror

A local image is a prerequisite for running a container, so we need to use the docker image pull command to pull the image from the image repository on the network to the local before running the container. The format of the command is:
docker image pull [OPTIONS] NAME[:TAG|@DIGEST]

If only the name of the image is specified, the image marked with the latest tag will be selected by default. For example, we want to pull the latest ubuntu image:

$ docker image pull ubuntu

This command actually pulls the ubuntu:latest image, and the latest version is 16.04. As you can see from the above figure, the docker image is actually divided into many layers, and each layer saves some specific files. The above command is actually equivalent to:

$ docker image pull registry.hub.docker.com/ubuntu:latest

That is to pull the latest image in the ubuntu warehouse from the default data warehouse server registry.hub.docker.com. If we feel that pulling images from Docker Hub is too slow, we can choose to pull from other data warehouse servers, such as the servers deployed by Docker Hub in China:

$ docker image pull registry.docker-cn.com/library/ubuntu:latest

After the image is downloaded locally, the container can be run, for example:

$ docker run --rm ubuntu echo hello docker

View mirror information

Use the docker image ls (or docker images) command to list locally stored images:

$ docker image ls

The output information includes:
REPOSITORY : Indicates which repository the image comes from, such as ubuntu or registry.docker-cn.com/library/ubuntu.
TAG : The tag information of the image, such as 14.04 or latest.
IMAGE ID : ID number that identifies the image.
CREATED : The time the image was created.
SIZE : Image size.
The ID information of the mirror is very important, it uniquely identifies the mirror.
TAG information is used to tag different images from the same repository (eg ubuntu). For example, there are multiple mirrors in the ubuntu repository, which can be distinguished by TAG information. TAG 13.04, 14.04 and 16.04 all represent different releases.

Using the docker image tag command to add a new tag to the local image can also facilitate our use, such as adding the following tag to the ubuntu:14.04 image:

$ docker image tag ubuntu:14.04 oldubuntu

Then you can use oldubuntu to reference the ubuntu:14.04 image. In fact, we can see from the output of docker image ls that they refer to the same image ID:

The new label we add is like giving the image an alias.

Use the docker image inspect command to get the detailed information of the image, such as viewing the information of the ubuntu:latest image:

$ docker image inspect ubuntu:latest

这个命令的输出很长,上图只是截取了一小部分的信息。它输出的是一个 JSON 格式的信息,一般情况下我们会有的放矢的通过 -f 选项取其中的某一部分。比如只获取镜像的 Architecture 信息:

$ docker image inspect -f {{".Architecture"}} ubuntu:latest

搜索镜像

除了直接在 Docker Hub 的官方网站上搜索镜像资源,还可以通 docker search 命令以命令行的方式进行搜索,比如搜索 mysql 镜像:

$ docker search mysql

该图只截取了一部分结果。个人感觉这个命令的价值有限,因为我们在选择镜像时还是需要慎重的。往往要在 Docker Hub 的官方网站上查看镜像相关的详细信息,然后才会决定是否使用,而 docker search 命令提供的信息过于有限。

删除镜像

对于不再需要的镜像我们可以使用 docker image rm 命令进行删除,以释放镜像占用的磁盘空间。我们可以为 docker image rm 命令传递镜像的标签或 ID,这两种方式略微有些区别,下面我们将分别介绍。

使用进行的标签删除镜像
比如删除标签为 mysql:5.6 的镜像:

$ docker image rm mysql:5.6

镜像的所有内容一下就被删除了(很多镜像层被删除掉),再来删除 ubuntu:14.04 试试:

$ docker image rm ubuntu:14.04

为什么这次只删除了一点点东西?再去看看进行列表,oldubuntu 还在,并且引用着 ID 为 3b853789146f 的镜像:

原来,对于被多个标签引用的镜像 ID,删除标签时只是把那个标签删掉了,并会真正删除镜像文件。现在再删除一次 oldubuntu 试试:

由于 oldubuntu 是最后一个引用该镜像的标签,所以删除该标签会同时删除该镜像的所有文件。

使用镜像 ID 删除镜像
对于镜像 ID 为 c9d990395902 的镜像来说,也有两个标签引用着它,分别是 ubuntu:latest 和 newubuntu:

现在让我们尝试通过镜像 ID 删除它:

$ docker image rm c9d990395902

此时 docker 检测到该镜像 ID 被引用了多次就机智的报错了,并且终止了删除操作。同样如果由其它的镜像引用了该 ID 的镜像, docker 同样会报错并终止删除操作。所以,只有当一个镜像不被多个标签引用,也没其它镜像引用它时,才可以被通过镜像 ID 删除。

创建镜像

我们可以通过不同的方式创建镜像,比如基于已有容器进行创建和基于 Dockerfile 文件进行创建。笔者在《Docker 基础 : Dockerfile》一文中介绍过通过 docker build 命令利用 Dockerfile 文件创建镜像,所以这里只介绍如何通过 docker container commit 命令基于已有容器创建镜像。我们先启动一个以 ubuntu:latest 为镜像的容器,然后在当前目录下创建一个名为 nickfile 的文件:

$ docker run -it ubuntu:latest bash

在文件创建后退出容器,但要记住该容器的 ID 为:7e26732e14e6。然后执行下面的命令创建镜像:

$ docker container commit -m "add file nickfile." 7e26732e14e6 nickimage

镜像创建成功后,你可以在镜像列表中看到名称为 nickimage 的镜像:

下面运行一个基于 nickimage 的容器,看看 nickfile 是否存在:

$ docker run --rm nickimage ls

验明正身!我们在容器中创建的文件 nickfile 已经被成功的添加到 nickimage 镜像中了。

导出和导入镜像

当碰到没有网络的环境时,如何获取镜像呢?答案是在能够获得镜像的环境中把镜像导出为文件,然后通过 U 盘等设备拷贝到目标环境中,再进行导入。

导出镜像
通过 docker image save 命令可以把镜像导出为本地文件,比如导出 ubuntu:latest 镜像为 ubuntu1604.tar:

$ docker image save -o ubuntu1604.tar ubuntu:latest

一般我们还会再压缩一下,这样最终的文件会小不少:

$ tar -czf ubuntu1604.tar.gz ubuntu1604.tar

文件大小从 112M 压缩到了 41M,效果还是很明显的。

导入镜像
把 ubuntu1604.tar.gz 文件拷贝到目标系统上后先要解压出 ubuntu1604.tar 文件:

$ tar -xf ubuntu1604.tar.gz

然后通过 docker image load 命令执行镜像的导入操作:

$ docker image load -i ubuntu1604.tar

这样就 OK 了,用 docker image ls 命令看看,是不是已经可以看到 ubuntu:latest 镜像了!

上传镜像

可以使用 docker image push 命令把镜像上传到镜像仓库服务器,默认是上传到 Docker Hub 的镜像仓库,此时事先需要注册用户并进行登录。上传镜像的命令格式为:
docker image push NAME[:TAG]
比如笔者在 Docker Hub 注册了账号 ljfpower,并通过 docker login 命令完成了登录操作(需要输入用户名和密码进行验证)。接下来就可把本地的镜像上传到镜像仓库服务器了。在上传前需要给镜像打上合法的标签(用户账号/仓库名称:TAG),比如:

$ docker image tag azcli:1.0 ljfpower/azcli:latest

最后上传这个标签就行了:

$ docker image push ljfpower/azcli:latest 

上传后你就可以在 Docker Hub 上看到这个镜像了:

总结

镜像是使用 docker 的前提,所以本文比较详细的介绍了 docker 镜像相关的操作,包括获取、查看、搜索、创建、删除、导出、导入等。希望对大家了解、学习 docker 有所帮助。

参考:
《Docker 技术入门与实战》

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324760014&siteId=291194637