Home >Operation and Maintenance >Docker >What are the core components of docker
Docker has three core components: 1. Image, a Linux file system; 2. Container, a runtime instance of the image; 3. Repository, a centralized storage place for images .
The operating environment of this tutorial: linux5.9.8 system, docker-1.13.1 version, Dell G3 computer.
The three core components of Docker
Image
Container
Repository
What is a Docker image?
To simply understand, the Docker image is a Linux file system (Root FileSystem). This file system contains programs that can run in the Linux kernel and corresponding data.
Speaking of this, we may need to add some knowledge related to the Linux operating system:
Generally speaking, Linux is divided into two parts: Linux Kernel (Linux Kernel) and user space , and the real Linux operating system refers to the Linux kernel. Our commonly used operating systems such as Ubuntu and CentOS are actually distribution versions (Linux Distribution) formed by different manufacturers adding their own software and tools (tools) based on the Linux kernel.
Therefore, we can also think of the image as the user space mentioned above. When Docker creates a container through the image, the user space defined by the image is run on the host as an independent and isolated process. On top of the Linux kernel.
Here we want to emphasize two characteristics of mirroring:
The mirror is layered (Layer): that is, a mirror can be composed of multiple intermediate layers, multiple mirrors The same middle layer can be shared, or we can generate a new image by adding one more layer to the image.
The mirror is read-only: after the mirror is built, it cannot be modified. What we said above is to add a layer to build a new mirror. In the middle, a temporary container is actually created, and files are added or deleted on the container to form a new image, because the container can be changed dynamically.
Through the following diagram, I can better understand the relationship between Docker images and Linux:
Commands for operating images
The commands related to image operations in Docker are all under the docker image subcommand. Through the docker image --help command, you can see the docker image subcommand. The detailed documentation of the command is as follows:
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(给镜像打标签)
Get the image
After installing Docker, we do not have any local image. Of course we can build it ourselves, but it is more convenient to pull the official or third-party built image from Docker Hub, the official warehouse service provided by Docker.
You can use docker image pull to pull the image. The format is as follows:
docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
Of course, docker image pull has a more concise usage: such as:
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
To pull the image, you need to specify the URL and port number of the Docker Registry. The default is Docker Hub. You also need to specify the warehouse name and label. The warehouse name and label uniquely determine an image, and the label may be omitted. If omitted, it will be used by default. latest is used as the tag name, and the warehouse name is composed of the author name and software name.
So, after omitting the parameters, for example, if we want to pull the centos image, we can use the following simple command to pull it from Docker Hub:
$ docker pull centos
View local mirrors
Through the above method, we pulled the mirror to the local, so how to check what mirrors are there locally? We can view all local images through the following command:
$ docker image ls
Of course, Docker provides a more concise way of writing, as follows:
$ docker images
Virtual Mirror
We know that the Docker image name consists of the warehouse name and label, but sometimes we will see an image whose warehouse name and label are both
The dangling image is generally a new image generated when we use docker pull to pull the latest image, so the warehouse name and label are given to the new image. , the old image warehouse and tags are canceled and become virtual images.
We can use the following statement to print all the hanging images:
$ docker image ls -f dangling=true
General hanging images have no effect anymore, so they can be cleaned up. The following command can clear all The virtual image:
$ docker image prune
However, if we want to keep some useful virtual images, we can use the docker tag command to rename the warehouse name and tag for the image:
$ docker tag 621d57f27e93 "test:1.0"
Image export and import
如果想与别人共享某个镜像,除了从镜像服务仓库中pull镜像和把镜像push到仓库上去之外,其实我们还可以将本地构建好的镜像直接导出并保存为文件发送给别人,如下:
$ docker image save /tmp/test_image.tar.gz
而当你拿到别人导出的镜像文件,你可以使用docker load命令把镜像加载到本地的Docker镜像列表中,如下:
$ docker load < /tmp/test_image.tar.gz
删除本地镜像
要删除一个或多个本地的镜像,可以使用下面的命令:
docker image rm [option] IMAGE1,IMAGE2,...IMAGEn
也可以使用更简洁的方式,如:
docker rmi [option] IMAGE1,IMAGE2,...IMAGEn
可以使用镜像的长ID、镜像短ID、镜像摘要以及镜像名称来删除镜像,如下:
$ docker rmi f7302e4ab3a8
一般更常用镜像的短ID,如:
$ docker rmi f7302
使用镜像的摘要也可以删除镜像,镜像的摘要可以使用下面的命令查询:
$ docker image ls --digests
当然我们想要清除本地全部镜像时,可以使用下面的命令,不过一般不建议使用。
$ docker rmi $(docker images -qa)
另外,一般如果镜像已经被使用来创建容器,使用上面的命令删除会报下面的错误,告诉我们该镜像已经被使用,不允许删除。
Error response from daemon: conflict: unable to remove repository reference "mysql:5.7" (must force) - container ccd406c07a78 is using its referenced image e1e1680ac726
对于已经被用于创建容器的镜像,删除方法有两种,一种是先把容器删除,再删除镜像,另一种则只需要在删除镜像的命令中跟一个-f参数便可,如:
$ docker rim -f f7302
使用docker commit构建镜像
上面的例子都是直接使用官方提供的镜像,其实,除了从官方仓库或其他镜像仓库拉取别人构建好的镜像外,我们也可以构建自己的镜像,一般有以下两种构建方式。
使用docker commit命令,我们可以将修改过的容器重新提交为一个镜像,如:
$ docker commit conntaner_id my-hello:1.0
使用这种方式构建的镜像,我们称为黑箱镜像,就是一个黑箱子一样,别人并不知道我们对容器做了哪些修改和操作,所以会对其安全性有所质疑。
所以不推荐使用这种方式构建镜像,下面我们介绍一种更加通用且方便的方式。
使用Dockerfile构建镜像
一般推荐编写Dockerfile来构建一种镜像,Docker Hub上的镜像都是采用这种方式构建的,采用这种方式的好处就是,我们不用把镜像分发给别人,而只是把Dockerfile和相应需要写入镜像的资料发给别人,别人也能自己构建镜像,安全透明。
编写一个简单的Got程序
package main import "fmt" func main(){ fmt.Println("Hello Go") }
将Go程序编译为可执行程序,如:
$ go build hello.go
编写Dockerfile文件
下面我们编写一个简单的Dockerfile文件,构建自己的第一个镜像,如下:
# 从一个空白的镜像开始 FROM stratch ADD hello / # 执行 CMD /hello
开始构建镜像
编写好Dockerfile文件后,需要使用docker build命令进行构建,docker build命令的格式如下:
$ docker build [OPTIONS] PATH | URL | -
# 注意最后的点(.)表示当前目录,即Dockerfile所在的目录 $ docker build -t "hello-go:1.0" .
上面只是简单演示了使用Dockerfile文件如何构建镜像,关于Dockerfile,还有许多更加深入地用法,我们之后有机再谈。
容器与镜像的关系,就如同面向编程中对象与类之间的关系。
因为容器是通过镜像来创建的,所以必须先有镜像才能创建容器,而生成的容器是一个独立于宿主机的隔离进程,并且有属于容器自己的网络和命名空间。
我们前面介绍过,镜像由多个中间层(layer)组成,生成的镜像是只读的,但容器却是可读可写的,这是因为容器是在镜像上面添一层读写层(writer/read layer)来实现的,如下图所示:
Usage: docker container COMMAND Manage containers Commands: attach Attach local standard input, output, and error streams to a runnin g container commit Create a new image from a container's changes(把容器保存为镜像) cp Copy files/folders between a container and the local filesystem create Create a new container(创建一个新的容器) diff Inspect changes to files or directories on a container's filesyste m exec Run a command in a running container(在一个运行的容器中执行命令) export Export a container's filesystem as a tar archive inspect Display detailed information on one or more containers kill Kill one or more running containers(杀死一个或多个正在运行的容器) logs Fetch the logs of a container ls List containers(显示本地容器列表) pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container prune Remove all stopped containers rename Rename a container(重命名容器) restart Restart one or more containers(重启一个或多个容器) rm Remove one or more containers(删除一个或多个容器) run Run a command in a new container(运行一个新的容器) start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers(停止一个或多个容器) top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers wait Block until one or more containers stop, then print their exit codes
启动容器
启动容器有几种不同的方式,最常用的方法是使用docker run命令可以通过镜像创建一个容器,如:
# /bin/bash表示运行容器后要执行的命令 $ docker run -it centos /bin/bash
docker run命令有一些比较常用的参数,比如容器是一种提供服务的守护进程,那么通常需要开放端口供外部访问,如:
$ docker run -p 80:80 nginx
也可以为容器指定一个名称,如:
$ docker run -p 80:80 --name webserver nginx
另外一种则是使用docker start命令重新启动已经停止运行的容器,如:
# container_id表示容器的id $ docker start container_id
而对于正在运行的容器,也可以通过docker restart命令重新启动,如:
# container_id表示容器的id $ docker restart container_id
查看本地容器列表
运行容器后,我们可以通过下面的命令查看本地所有容器:
$ docker container ls
不过docker container ls也简洁的写法:
$ docker ps
上面命令执行结果如下:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4f184f5ffb9 redis:latest "docker-entrypoint.s…" 6 seconds ago Up 4 seconds 0.0.0.0:6379->6379/tcp myredis f7d970e7d4ce mysql:5.7 "docker-entrypoint.s…" 7 seconds ago Up 5 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp docker-mysql
上面的命令只会显示正在运行的容器,如果要显示全部容器,包含退出执行的,可以加参数-a,如:
$ docker ps -a
有时候,我们只想查到容器的ID,可以用下面的命令:
$ docker ps -aq
执行结果:
f4f184f5ffb9 f7d970e7d4ce
停止容器
对于已经不需要的容器,可以使用docker stop命令停止其运行,如:
$ docker stop container_id1,container_id2...
批量停止容器,如:
$ docker stop $(docker ps -qa)
容器的三种运行模式
概括而言,Docker容器大体上有三种运行模式,如下:
运行后退出
下面语句创建的容器,在运行后会退出。
$ docker run centos echo "hellowrold"
常驻内存,就是守护进程的模式
如果容器中运行一个守护进程,则容器会一直处于运行状态,如:
$ docker run -d -p 80:80 nginx
交互式
我们也可以在运行容器时,直接与容器交互。
$ docker run -it centos /bin/bash
删除容器
$ docker container rm container_id
删除容器的命令也有简洁的写法,如下:
$ docker rm container_id
也可以像上面批量停止容器一样,我们也可以批量删除容器,如:
$ docker rm $(docker ps -qa)
进入容器
对于正在运行的容器,我们也可以通过docker exec命令再次进入容器,如:
$ docker exec -it f4f184f5ffb9 /bin/bash
需要指定容器的id或name,上面的命令我们用的是ID。
导出容器为镜像
$ docker export -o ./image.tar.gz f4f184f5ffb9
将容器导出后,我们可以另外一台有安装Docker的电脑中将文件包导入成为镜像,如:
$ docker import image.tar.gz
上面讲的是容器的概念和一些常用的命令,关于容器,还可以设置数据卷和网络空间,这些我们有机会后面再谈。
仓库(Repository)是集中存储镜像的地方,这里有个概念要区分一下,那就是仓库与仓库服务器(Registry)是两回事,像我们上面说的Docker Hub,就是Docker官方提供的一个仓库服务器,不过其实有时候我们不太需要太过区分这两个概念。
公共仓库
公共仓库一般是指Docker Hub,前面我们已经多次介绍如何从Docker Hub获取镜像,除了获取镜像外,我们也可以将自己构建的镜像存放到Docker Hub,这样,别人也可以使用我们构建的镜像。
不过要将镜像上传到Docker Hub,必须先在Docker的官方网站上注册一个账号,注册界面如下,按要求填写必要的信息就可以注册了,很简单的。
注册好了之后,可以在本地使用命令登录到Dokcer Hub了,过程如下:
# 在命令行中输入 $ docker login
在输入账号密码登录到Docker Hub之后,便可以使用docker push命令把镜像推送到Docker Hub。
$ docker push test:1.0
私有仓库
有时候自己部门内部有一些镜像要共享时,如果直接导出镜像拿给别人又比较麻烦,使用像Docker Hub这样的公共仓库又不是很方便,这时候我们可以自己搭建属于自己的私有仓库服务,用于存储和分布我们的镜像。
Docker官方提供了registry这个镜像,可以用于搭建私有仓库服务,我们把镜像拉到本地之后,用下面命令创建该镜像的容器便可以搭建一个仓库服务,如下:
$ docker run -d -p 5000:5000 --restart=always --name registry registry
假设我们把一台IP为192.168.0.100的服务器作为仓库服务,并运行上面的语句,那么我们可以下面的语句重新构建上面的镜像,如:
$ docker build -t "192.168.0.100/hello-go:1.0" .
然后使用下面的语句推送到自己的私有仓库服务器:
$ docker push 192.168.0.100/hello-word:1.0
镜像是静态的概念,构建完成之后便不能再修改,而容器则是一个动态的概念,使用Docker可以简单轻松地创建或删除容器,镜像与容器的关系,就如同面向对象编程中的类与对象的关系,而仓库则是存储和分发镜像的地方。
推荐学习:《docker视频教程》
The above is the detailed content of What are the core components of docker. For more information, please follow other related articles on the PHP Chinese website!