Docker core concepts and basic use

A, Docker Profile

Docker is a building, open platform for publishing and running applications. Docker to the container as the basic unit of resource separation and scheduling, container encapsulates all the environment entire project runtime required by Docker you can applications and infrastructure separation, like management applications, like management infrastructure to quickly complete the project deployment and delivery.

Docker use the Go language development, based on the cgroup, namespace, and Union FS AUFS class technology such as the Linux kernel, the process of encapsulation isolation, belonging to the virtualization operating system level. Initial implementation is based on LXC, from version 0.7 to start removing LXC, instead use libcontainer self-developed, from 1.11 to start, then further evolved into the use runC and containerd.

  • runc : Linux is a command-line tool used according to specifications OCI runtime container to create and run a container.
  • containerd : is a daemon that manages the lifecycle of the container, a container and a minimum set of features performed on a mirrored management node.

FIG reflects the differences Docker traditional virtualization mode: the traditional virtual machine technology is a set of the virtual hardware on which to run a full operating system, then run the desired application process on the system; and Docker containers in the application process is run directly on the host kernel, the kernel does not own the container, and there is no hardware virtualization, and therefore more portable than traditional virtual machine.

Two, Docker architecture and core concepts

Docker using client-server architecture, the client Docker Docker send commands to the daemon, which is responsible for building, operating and distribution Docker containers. Docker and client daemon uses REST API, communicate through a network interface or a UNIX socket. The core concept is as follows:

2.1 Mirror

Docker image (Image) is a special file system, including the time needed to run the program resources and the environment. Image does not contain any dynamic data, its contents will not be changed after the construct.

Because the operating system image contains the complete rootfile system, the volume is often large, so Docker design, take full advantage of technology Union FS (file system jointly), and it is designed as a tiered storage architecture, it is in fact a mirror It is a coalition of a multi-layer file system. When constructing the mirror, will build a layer, the previous layer is the base layer; each layer constructing End will not change, any change in the layer of this layer occurs only in themselves. For example, one file before delete operation, the actual not really delete the previous layer files, but only deleted in the current layer mark for the file. When the final container to run, though not seen this document, but in fact the file will always follow the mirror. Therefore, when building the image, you need to be extra careful, try each layer of the layer containing only thing you need to add any extra things to be cleared away before the end of the layer build.

Characterized in that the hierarchical storage of image multiplexing, the customized becomes easier. Even before you can build a good image as a base layer, and then further adding a new layer to customize the content they need to build a new image.

2.2 container

Mirror image (Image) and the container (Container), like object-oriented programming design and 实例the same image is a static definition of container is a runtime entity mirror, the container can be created, start, stop, delete, suspend, Wait.

The process is the essence of container, but in a different process and direct the implementation of the host, container processes run their own independent namespace in. Therefore, the container can have its own rootfile system, your network configuration, its own process space, or even their own user ID space. Running the process in a container in an isolated environment, to use it as if they were operating in a system independently of the host, this feature makes direct application of the packaged safer operation than in the host.

Recall mirroring is tiered storage container as well. Each container is running, is a mirror for the base layer, create a storage container at its current level, we can call this run for the container prepared to read and write storage layer called the container storage layer . Lifetime of the storage layer and the container as the container, the container die, the container also will die of the memory layer. Therefore, any container stored in the storage layer information will be deleted with the container lost.

Docker best practices as required, the container should not write any data to which the storage layer, the storage layer to keep the container free of state. All file write operations, volume data should be used (Volume), or binding directory host, reading and writing these locations will skip container storage layer to read and write to the host (or network storage) occurs directly, and its performance higher stability. Life cycle data volume is independent of the container, the container demise, the data volume will not die, therefore, the use of data volumes, delete or re-run after the container, data will be lost.

2.3 Warehouse

After the image is built, it can easily run on the current host, but if you need to use this image on another server, you need a centralized storage, distribution mirroring service, which is mirrored warehouse (Registry). Docker Hub is a mirror image of Docker official public warehouse, provides a number of commonly used software mirroring, of course, for security and privacy needs, you can also build your own private warehouse.

2.4 Docker daemon

Docker daemon (dockerd) is responsible for monitoring and managing Docker Docker requests the API objects, such as mirroring, the container, and a network volume, with each other may communicate daemon.

2.5 Docker Client

Docker client (docker) is the primary means for users to interact with the Docker. When you use docker run commands, the client will send those commands to dockerd, dockerd responsible for its implementation. Docker a client can communicate with multiple dockerd.

Three, Docker commonly used commands

Docker provides a number of commands for managing mirroring, containers and services, unified commands use the format: docker [OPTIONS] COMMANDwhich represents the optional OPTIONS parameter. Note that the implementation of Docker command generally need to obtain root privileges, because Docker command-line tool docker and docker daemon is a single binary file, docker daemon is responsible for receiving and executing commands from the docker, and its operation requires root privileges . All commonly used commands and their usage scenario is as follows:

3.1 basis of command

  • Version docker : docker for viewing version information
  • info docker : docker is used to view the configuration information
  • Help Docker : to view help information

3.2 mirroring command

1. docker search image name

Find the specified name from the official mirror mirror warehouse Docker Hub. Commonly used parameters --no-trunc, on behalf of the full display image information.

2. docker images

Lists information about all the top-level mirroring. Common parameters are as follows:

  • -a : All mirrored, comprising an intermediate image hidden
  • -q : Show only image ID
  • -Digests : display summary information
  • Trunc-the -no : display a complete image information

3. docker pull mirror name [: TAG]

Download image from the official repository, :TAGmirrored version, without the default download the latest version.

4. docker rmi image name or ID [: TAG]

删除指定版本的镜像,不加 :TAG 则默认删除镜像的最新版本。如果有基于该镜像的容器存在,则该镜像无法直接删除,此时可以使用参数 -f,代表强制删除。rmi 命令支持批量删除,多个镜像名之间使用空格分隔。如果想要删除所有镜像,则可以使用命令 docker rmi -f $(docker images -qa)

3.3 容器相关命令

1. docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

run 是 docker 中最为核心的一个命令,用于新建并启动容器,其拥有众多可用参数,可以使用 docker run --help 查看所有可用参数。常用参数如下:

  • -i :表示使用交互模式,始终保持输入流开放;
  • -t :表示分配一个伪终端,通常和 -i 结合使用,表示使用伪终端与容器进行交互;
  • -d :以后台方式运行容器;
  • –name :指定容器启动容器的名字,如果不指定,则由 docker 随机分配;
  • -c :用于给运行在容器中的所有进程分配 CPU 的 shares 值,这是一个相对权重,实际的处理速度与宿主机的 CPU 相关;
  • -m :用于限制为容器中所有进程分配的内存总量,以 B、K、M、G 为单位;
  • -v :挂载数据卷 volume,可以用多个 -v 参数同时挂载多个 volume。volume 的格式为:[host-dir]:[container-dir]:[rw:ro][rw:ro] 用于指定数据卷的模式,rw 代表读写模式,ro 代表只读模式。
  • -p :用于将容器的端口暴露给宿主机的端口,格式为:hostPort:containerPort ,通过端口的暴露,可以让外部主机能够访问容器内的应用。

2. docker ps [OPTIONS]

列出当前所有正在运行的容器。常用参数如下:

  • -a :列出所有容器,包括运行的和已经停止的所有容器
  • -n :显示最近创建的 n 个容器
  • -q :只显示容器编号
  • –no-trunc :不要截断输出信息

3. 启动\重启\停止\强制停止容器

与容器启动、停止相关的命令为:docker start|restart|stop|kill 容器名或ID ,start 命令用于启动已有的容器,restart 用于重启正在运行的容器,stop 用于停止正在运行的容器,kill 用于强制停止容器。

4. 进入正在运行的容器

想要进入正在运行的容器,与容器主进程交互,有以下两种常用方法:

  • docker attach 容器名或ID
  • docker exec -it 容器名或ID /bin/bash

5. 退出容器

想要退出正在运行的容器,有以下两种常用方法:

  • exit :退出并停止容器;
  • ctrl+P+Q :退出。

6. docker rm 容器名或ID

删除已停止的容器,常用参数为-f,表示强制删除容器,即便容器还在运行。想要删除所有容器,可以使用 docker rm -f $(docker ps -aq) 命令。

7. 查看容器信息

可以使用 docker inspect [OPTIONS] NAME|ID [NAME|ID...] 查看容器或者镜像的详细信息,想要查看指定的信息,可以使用 -- format 参数来指定输出的模板格式,示例如下:

docker inspect --format='{{.NetworkSettings}}'  32cb3ace3279

8. 查看容器运行日志

可以使用 docker logs [OPTIONS] CONTAINER 查看容器中进程的运行日志,常用参数如下:

  • –details :显示日志详情
  • -f :跟随日志输出显示
  • –tail :从末尾开始显示指定行的数据
  • -t :显示时间戳
  • –since :开始时间
  • –until : 结束时间

四、DockerFile

dockerfile 是 Docker 用来构建镜像的文本文件,包含自定义的指令和格式,可以通过 build 命令从 dockerfile 中构建镜像,命令格式为:docker build [OPTIONS] PATH | URL | -

dockerfile 描述了组装镜像的步骤,其中每条指令都是单独执行的。除了 FROM 指令,其他的每一条指令都会在上一条指令所生成镜像的基础上执行,执行完后会生成一个新的镜像层,新镜像层覆盖在原来的镜像之上从而形成新的镜像。为了提高镜像构建的速度, Docker Daemon 会缓存构建过程中的中间镜像。在构建镜像时,它会将 dockerfile 中下一条指令和基础镜像的所有子镜像做比较,如果有一个子镜像是由相同的指令生成的,则命中缓存,直接使用该镜像,而不用再生成一个新的镜像。常用指令如下:

1. FROM

FROM 指令用于指定基础镜像,因此所有的 dockerfile 都必须使用 FROM 指令开头。FROM 指令可以出现多次,这样会构建多个镜像,每个镜像创建完成后,Docker 命令行界面会输出该镜像的 ID。常用指令格式为:FROM <image>[:<tag>] [AS <name>]

2. MAINTAINER

MAINTAINER 指令可以用来设置作者名称和邮箱,目前 MAINTAINER 指令被标识为废弃,官方推荐使用 LABEL 代替。

3. LABEL

LABEL 指令可以用于指定镜像相关的元数据信息。格式为:LABEL <key>=<value> <key>=<value> <key>=<value> ...

4. ENV

ENV 指令用于声明环境变量,声明好的环境变量可以在后面的指令中引用,引用格式为 $variable_name${variable_name} 。常用格式有以下两种:

  • ENV <key> <value> :用于设置单个环境变量;
  • ENV <key>=<value> ... :用于一次设置多个环境变量。

5. EXPOSE

EXPOSE 用于指明容器对外暴露的端口号,格式为:EXPOSE <port> [<port>/<protocol>...] ,您可以指定端口是侦听 TCP 还是 UDP,如果未指定协议,则默认为 TCP。

6. WORKDIR

WORKDIR 用于指明工作目录,它可以多次使用。如果指明的是相对路径,则它将相对于上一个WORKDIR指令的路径。示例如下:

WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd # 此时pwd为:/a/b/c

7. COPY

COPY 指令的常用格式为:COPY <src>... <dest>,用于将指定路径中的文件添加到新的镜像中,拷贝的目标路径可以不存在,程序会自动创建。

8. ADD

ADD 指令的常用格式为:COPY <src>... <dest>,作用与 COPY 指令类似,但功能更为强大,例如 Src 支持文件的网络地址,且如果 Src 指向的是压缩文件,ADD 在复制完成后还会自动进行解压。

9. RUN

RUN 指令会在前一条命令创建出的镜像基础上再创建一个容器,并在容器中运行命令,在命令结束后提交该容器为新的镜像。它支持以下两种格式:

  • RUN <command>shell 格式)
  • RUN ["executable", "param1", "param2"] (exec 格式)

使用 shell 格式时候,命令通过 /bin/sh -c 运行,而当使用 exec 格式时,命令是直接运行的,容器不调用 shell 程序,这意味着不会发生正常的 shell 处理。例如,RUN ["echo","$HOME"] 不会对 $HOME 执行变量替换,此时正确的格式应为:RUN ["sh","-c","echo $HOME"]。下面的 CMD 指令也存在同样的问题。

10. CMD

  • CMD ["executable","param1","param2"] (exec 格式, 首选)
  • CMD ["param1","param2"] (作为 ENTRYPOINT 的默认参数)
  • CMD command param1 param2 (shell 格式)

CMD 指令提供容器运行时的默认值,这些默认值可以是一条指令,也可以是一些参数。一个 dockerfile 中可以有多条 CMD 指令,但只有最后一条 CMD 指令有效。CMD 指令与 RUN 指令的命令格式相同,但作用不同:RUN 指令是在镜像的构建阶段用于产生新的镜像;而 CMD 指令则是在容器的启动阶段默认将 CMD 指令作为第一条执行的命令,如果用户在 docker run 时指定了新的命令参数,则会覆盖 CMD 指令中的命令。

11. ENTRYPOINT

ENTRYPOINT 指令 支持以下两种格式:

  • ENTRYPOINT ["executable", "param1", "param2"] (exec 格式,首先)
  • ENTRYPOINT command param1 param2 (shell 格式)

ENTRYPOINT 指令 和 CMD 指令类似,都可以让容器在每次启动时执行相同的命令。但不同的是 CMD 后面可以是参数也可以是命令,而 ENTRYPOINT 只能是命令;另外 docker run 命令提供的运行参数可以覆盖 CMD,但不能覆盖 ENTRYPOINT ,这意味着 ENTRYPOINT 指令上的命令一定会被执行。如下 dockerfile 片段:

ENTRYPOINT ["/bin/echo", "Hello"]
CMD ["world"]

当执行 docker run -it image 后,此时输出为 hello world,而当你执行 docker run -it image spring ,此时 CMD 中的参数会被覆盖,此时输出为hello spring

五、案例

5.1 基于 Centos 镜像部署 Spring Boot 项目

生产环境中的大多数项目通常都部署在 Linux 服务器上,这里我们从基础的 Linux 镜像开始,并将我们的项目(这里以 Spring Boot 项目为例)一起打包构建成为一个完整的可执行的镜像。首先需要创建Dockerfile文件,其内容如下:

# 以官方仓库的centos镜像为基础开始创建
FROM centos
# 作者信息
MAINTAINER  [email protected]

# 把JDK安装包拷贝到容器中并自动进行解压
ADD jdk-8u211-linux-x64.tar.gz /usr/java/
# 拷贝项目Jar包到容器中
COPY spring-boot-base.jar  /usr/app/
# 配置Java环境变量
ENV JAVA_HOME /usr/java/jdk1.8.0_211
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH ${JAVA_HOME}/bin:$PATH
# 项目启动命令
ENTRYPOINT ["java", "-jar", "/usr/app/spring-boot-base.jar"]

将 JDK 安装包,Spring Boot 项目的 Jar 包以及 Dockerfile 文件放在同一个目录,然后执行下面镜像构建命令:

docker build -t spring-boot-base-java:latest .

镜像构建完成后,可以使用以下命令进行启动:

docker run -it  -p 8080:8080 spring-boot-base-java

这里为了观察到启动效果,所以使用交互的方式启动,实际部署时可以使用-d参数来后台启动,输出如下:

5.2 基于 JDK 镜像部署 Spring Boot 项目

上面的项目我们是基于最基础的 Centos 镜像开始构建,但由于 Docker Hub 上已经提供了 JDK 的镜像,我们也可以选择从 JDK 镜像开始构建,此时构建过程更加简单。构建步骤和上面的完全一致,只是 Dockerfile 的内容有所不同,如下:

# 由于只需要运行环境,这里我们直接以官方仓库的jre镜像为基础开始创建
FROM openjdk:8u212-jre
# 作者信息
MAINTAINER  [email protected]

# 拷贝项目Jar包到容器中
COPY spring-boot-base.jar  /usr/app/
# 项目启动命令
ENTRYPOINT ["java", "-jar", "/usr/app/spring-boot-base.jar"]

参考资料

  1. Docker 官方简介:https://docs.docker.com/engine/docker-overview/
  2. Docker CLI 和 Dockerfile 官方文档: https://docs.docker.com/reference/
  3. 浙江大学SEL实验室 . Docker 容器与容器云(第2版). 人民邮电出版社 . 2016-10
  4. Docker 从入门到实践:https://yeasy.gitbooks.io/docker_practice/

更多文章,欢迎访问 [全栈工程师手册] ,GitHub 地址:https://github.com/heibaiying/Full-Stack-Notes

发布了163 篇原创文章 · 获赞 88 · 访问量 6万+

Guess you like

Origin blog.csdn.net/m0_37809146/article/details/104017981