Docker practical notes 1-basics

Please indicate the source of the reprint: http://blog.csdn.net/zhaoyanjun6/article/details/130181636
This article comes from [Zhao Yanjun's blog]

official website

https://www.docker.com/

Download and install

Download the software for the corresponding platform and install it

insert image description here

Install

Install the description given by the software, and execute the commands in sequence
insert image description here
insert image description here
insert image description here

Docker image

Image is used in Docker to represent a mirror image. Every container is a mirror image

mirror image

We all know that the operating system is divided into 内核and 用户空间. For Linux, after the kernel starts, the root file system will be mounted to provide user space support. However Docker 镜像(Image), it is equivalent to a root file system. For example, the official ubuntu:18.04image contains a complete Ubuntu 18.04set of the root file system of the minimal system.

A Docker image is a special file system. In addition to providing the programs, libraries, resources, configuration and other files required by the container runtime, it also contains some configuration parameters prepared for runtime (such as anonymous volumes, environment variables, users, etc. ). Images do not contain any dynamic data, and their contents are not changed after they are built.

Hierarchical storage

Because the image contains the complete root file system of the operating system, its volume is often huge, so when designing Docker, it takes full advantage of the technology and designs it as a hierarchical storage architecture. So strictly speaking, an image is not a packaged file like an ISO. An image is just a virtual concept. Its actual embodiment is not composed of a file, but a group of file systems, or in other words, a combination of multi-layer file systems. composition.

When the image is built, it will be built layer by layer, and the previous layer is the basis of the next layer. After each layer is built, it will not change again, and any changes on the next layer only occur on its own layer.

For example, the operation of deleting a file in the previous layer does not actually delete the file in the previous layer, but only marks the file as deleted in the current layer. When the final container is running, although this file will not be seen, in fact, the file will always follow the image. Therefore, when building an image, you need to be extra careful. Each layer should only contain what needs to be added to that layer, and any extra things should be cleaned up before the end of the layer's construction.

The feature of hierarchical storage also makes the reuse and customization of images easier. You can even use the previously built image as the base layer, and then further add new layers to customize what you need and build a new image.

container

The relationship between mirror ( Image) and container ( Container) is like and 实例. Mirror is a static definition, and container is an entity when mirroring is running. Containers can be created, started, stopped, deleted, paused, etc.

The essence of a container is a process, but unlike a process directly executed on the host, a container process runs in its own independent namespace. So a container can have its own root filesystem, its own network configuration, its own process space, and even its own user ID space. The process in the container runs in an isolated environment, and when used, it seems to be operating under a system independent of the host. This feature makes container-encapsulated applications more secure than running directly on the host. Also because of this isolation feature, many people often confuse containers and virtual machines when they first learn Docker.

As mentioned earlier, images use hierarchical storage, and so do containers. When each container runs, the image is used as the base layer, and a storage layer for the current container is created on it. We can call this storage layer prepared for container runtime read and write 容器存储层.

The life cycle of the container storage layer is the same as that of the container. When the container dies, the container storage layer also dies. Therefore, any information stored in the storage layer of the container will be lost when the container is deleted.

According to the best practices of Docker, containers should not write any data into their storage layer, and the container storage layer should remain stateless. All file write operations should use 数据卷(Volume), or 绑定宿主目录read and write in these locations will skip the container storage layer and directly read and write to the host (or network storage), which has higher performance and stability.

The life cycle of the data volume is independent of the container. When the container dies, the data volume will not die. Therefore, after using the data volume, the data will not be lost after the container is deleted or restarted.

Common commands

docker info

View docker information

insert image description here

docker images

insert image description here

docker version

insert image description here

docker ps : View containers

docker ps can only view running containers, not running containers
insert image description here

docker ps -a

View all status containers

See what files are in the root directory of the container

docker exec 容器id ls -a /

insert image description here

View a directory of the container

For example, we want to see what files are usrin the directory

Command format:

docker exec 容器id ls 容器目录

Example: view the files in the container usrdirectory

docker exec 9a5dd57529c9 ls /usr

Upload local files to the container

command format

docker cp 文件 容器id:容器目录

Example: Upload desktop images IMG_0423.jpegto usrthe directory of the container

docker cp /Users/zhaoyj/Desktop/IMG_0423.jpeg 9a5dd57529c9:usr

Delete the files in the container directory

Command format:

docker exec container id rm file path

Example: delete usrthe directoryIMG_0423.jpeg

docker exec 9a5dd57529c9 rm /usr/IMG_0423.jpeg

copy container file to local

command format

docker cp CONTAINER:SRC_PATH DEST_PATH

Example: copy the container usr directory test.pngto the local desktop folder

docker cp 9a5dd57529c9:usr/test.png /Users/smile/Desktop/

delete container

Command format:

docker rm CONTAINER_ID

delete mirror

Command format:

docker rmi IMAGE_ID

Notice:删除镜像之前,需要先把对应的容器删掉,否则会报错

insert image description here

Pull the first image

Docker officially provides us with a hello-world image at https://hub.docker.com/

hello-world address: https://hub.docker.com/_/hello-world

insert image description here
We try to pull down the local

Order

docker pull hello-world

insert image description here

view mirror

Order

docker images

insert image description here
The image can also be seen on the docker client of mac

insert image description here

run the first image

Order

docker run hello-world

insert image description here

docker architecture diagram

insert image description here

Guess you like

Origin blog.csdn.net/zhaoyanjun6/article/details/130181636