DevOpsDocker - 包含多个容器的程序配置
六翼天使494 发表于:2022-12-3 07:31:53 复制链接 看图 发表新帖
阅读数:400
Overview of a Multi-Container Application Setup

之前,我们介绍了Docker和容器化。还给大家举例Python和JavaScript做演示,演示程序如何容器化,镜像是如何构建的。现在,我们现在已经准备好运行一个超越这一点的应用程序。

在Dockerfile中,每一行描述一层。Docker的union文件系统允许以透明的模式覆盖不同的目录,组成一个相关的系统。基础层(第一行的from)总是一个镜像。每一行使用一个命令,比如,RUN,CMD等等每一行命令,增加一层。层次的优势是,只要层不被修改,就不用再建立镜像的那一部分。另外,一个镜像从Docker镜像仓库中拉回来,是按照层次进行拉取的,因此,可以缓解咋拉取过程中出现的断线问题。

很多应用程序都适用常用的架构:前端、后端、数据库。让我们把这个分解,理解我们如何部署这些。

The Frontend

当你打开一个web页面,这个页面就是前端的一部分。有些时候,前端有控制器(the logical end)以及查看层(the dumb end)。布局和内容的样式(读,HTML和CSS)是展示层。这里的内容由控制器控制。(Django的MTV么,还控制器,说着容器讲到CSS,不可思议!!^_^)

The controller influences what is presented in the view layer based on the user's action and/or database changes. 举个例子,像头条:一些人关注了你,你的数据会变化。控制器会捕获到这个变化,并且更新展示层,展示关注的人+1。

The Backend

也许大伙之前听过Model-view-control(MVC)。这个模型会在程序的后端部分。还是之前头条的例子,model不会关心HTML或者HTML层的问题。它处理程序的状态:关注者的数量以及多少人在关注头条、图片、视频等等。

NOTE

后断层大致包含这些内容。后端主要处理程序的逻辑。包括代码处理数据库数据;所有来自后端的请求。However,来自前端的请求,会在用户在web页面点击button等等。

我们也听过API这个词。API是Application Program Interface的缩写。同样也是部署在后端。API暴露了程序的内部工作。

API可以是一个程序的后端,或者叫做逻辑层。

还是以头条来说的更清楚一些。在头条上发布一条,以及在头条上搜索,可以轻松地使用API办到,API如果公开的话,可以被任何前端程序调用。

NOTE

Docker和docker-compose这些终端命令也是真实的API调用,比如使用Docker Hub在和外部资源或者你饿哦让交互的时候。

The Database

数据库包含了组织好的数据(信息)可以轻松被访问,管理和更新。我们有基于文件的数据库以及基于服务器的数据库(啥叫文件的数据库、服务器的数据库?)。

基于服务器的数据库,引入了服务进程,运行、接收请求、读取、写入数据库。例如,数据库有可能放在云端。

NOTE

基于服务器的数据库在虚拟机里面,大多基于云平台,像GCP(谷歌的云服务),AWS(亚马逊的云服务)。具体产品AWS的RDS,以及GoogleCloudSQL(基于PostgreSQL)。

Obtain server-based databases from the following links:
    https://aws.amazon.com/rds/postgresql/https://cloud.google.com/sql/docs/postgres

简而言之,开发总会引入构建app应用程序层,考虑到云平台的价格以及开发和运营, 航运一直是个麻烦,所以DevOPS被引入了。

Docker和docker-compose帮助我们管理我们所有程序的不同容器,作为一个单独的捆绑,高效,快速,方便。docker-compose帮助我们将程序所有层次变成一个单独的文件,该文件配置非常简单。

在我们结束本概述时, 重要的是要知道, 随着时间的推移, 开发人员已经创建了不同的堆栈变体来总结其应用的前端、后端和数据库结构。下面是它们的列表及其含义 (在本课程中, 我们不会进一步深入讨论):
    PREN - PostgresDB, React, Express, Node.jsMEAN - MongoDB, Express, Angular, Node.jsVPEN - VueJS, PostgresDB, Express, Node.jsLAMP - Linux, Apache, MySQL, PHP

NOTE

重要的是要知道, 应用程序是以这种方式构建的, 以管理关注点的分离。

有了应用结构的知识, 我们就可以到达由docker组成的 CLI, 并将这些知识发挥作用。

Using docker-compose

Using docker-compose requires three steps:
    使用Dockerfile文件,创建一个作为程序的镜像环境。使用docker-compose.yml文件定义你的程序需要运行的服务。使用docker-compose启动app。

NOTE

docker-compose is a command-line interface (CLI) just like the Docker CLI. Running docker-compose gives a list of commands and how to use each.

我们在上一课中查看了图像, 因此检查了步骤1。

某些 docker 撰写版本与某些 Docker 版本不兼容。

我们将在第2步停留一段时间。

下面是docker撰写文件:
    One that runs the two images we created in our previous lesson:

DevOpsDocker - 包含多个容器的程序配置-1.jpg

NOTE

Refer the complete code placed at Code/Lesson-2/example-docker-compose.yml.

Go to https://goo.gl/11rwXV to access the code.

DOCKER-COMPOSE FIRST RUN

    创建一个新的目录,命名为py-js。在新目录中创建一个新的文件,叫作docker-compose.yml。拷贝上面镜像的内容或者example-docker-compose.yml到该文件中。直接在该目录中运行docker-compose up。

注意,输出中同时包括了js-docker和python-docker。当然,在上一节中,我们把js和python两个镜像文件创建在了本地。

如果本地没有镜像,执行docker-compose up命令会导致错误,或者会自动尝试从Docker Hub下载。

DevOpsDocker - 包含多个容器的程序配置-2.jpg

    A docker-compose.yml that runs WordPress. WordPress is a free and open source content management system(CMS) based on PHP and MySQL.

Activity 1 — Running WordPress Using docker-compose

让您熟悉运行文档撰写命令。

你被领导要求使用docker-compose创建一个WordPress站点。
    创建一个新的目录,叫作sandbox。创建一个文件,叫作docker-compose.yml。将下面的内容加入到wordpress-docker-compose.yml。

DevOpsDocker - 包含多个容器的程序配置-3.jpg



NOTE

Refer the complete code placed at Code/Lesson-2/wordpress-docker-compose.yml.

Go to https://goo.gl/t7UGvy to access the code.

NOTE

注意文件中的缩进。建议在缩进线条时使用相同数量的选项卡和空格。

Run docker-compose up in the sandbox directory:

DevOpsDocker - 包含多个容器的程序配置-4.jpg

NOTE

你会注意到,基于一个文件,我们让一个程序运行了起来。这个例子是docker撰写力量的完美展示。

Run docker ps. You'll see the containers running:

DevOpsDocker - 包含多个容器的程序配置-5.jpg

Open your browser and go to the address at: http://0.0.0.0:8000/. We'll have the WordPress website set up ready.

继续设置, 瞬间, 你就有了一个 WordPress 网站, 准备好了。

THE DOCKER-COMPOSE FILE: DOCKER-COMPOSE.YML

docker-compose.yml is a YAML file. It defines services, networks, and volumes.

NOTE

服务指的是容器程序定义,包括程序相关的所有部分,例如,DB,前端,后端。当创建服务的时候,哪个部分最重要,网络,卷,还是环境变量。

docker-compose.yml文件的第一行定义了docker-compose文件格式的版本。

使用docker -v,可以知道Docker的版本,因此可以知道放在第一行的版本时多少(兼容docker版本的)。

在docker-compose文件格式1.0中,第一行有没有无所谓。每个docker-compose文件引入了新的配置或不推荐使用较早的配置。

我们使用版本3.3,与3.0以及上面的版本都兼容。

确保所有人使用版本3,至少有一个版本时1.13.0+。

接下来是服务。让我们使用这个简化的骨架:

DevOpsDocker - 包含多个容器的程序配置-6.jpg

NOTE

在上面的示例中, 我们有两个服务, 即 db 和 web。这两个只缩进一次。

定义服务后的下一行定义了要从中生成映像的映像或 Dockerfile。

第4行将指定 db 服务容器将从中运行的镜像。我们早些时候提到了一些堆栈;db 映像可以是任何基于服务器的数据库。

NOTE

To confirm whether a stack you want to use exists, run the following:docker search <image or name of your preferred stack> (for example, docker search mongo or docker search postgres).

Line 6 explains that the web services image will be built from the Dockerfile in the location (.) relative to the docker-compose.yml.

We can also define the name of the Dockerfile in line 6. docker-compose will search for the file with a name as listed, for example, in the docker-compose.yml:
Line 5| web:build: Dockerfilevolumes:
Lines 7 to 10 gives more definition to the web services.

As evidenced in the docker-compose.yml we used to build and run WordPress, there are two services: db and wordpress. In the output of docker ps, these are the container names: sandbox_wordpress_1 and sandbox_db_1.

The first word before the underscore signifies the name of the directory holding the docker-compose.yml. The second word in that container name is the service name, as defined in docker-compose.yml.

We'll go into more context in the following topic.

THE DOCKER-COMPOSE CLI

As soon as docker-compose is installed, I mentioned that you expect a list of options when you run docker-compose. Run docker-compose –v.

NOTE

These two commands, docker-compose and docker-compose -v, are the only ones that can be ran from whichever working directory is open on your terminal command line or Git bash.

Otherwise, the other options in docker-compose are only run in the presence of the docker-compose.yml file.

Let's dig deep into the common commands: docker-compose build.

This command builds images referenced in the docker-compose line: (build: .) in the template docker-compose.ym.

Building images can also be achieved through the command docker-compose up. Take note that this doesn't happen unless the image has not been built yet, or there has been a recent change that affects the container to be run.

NOTE

This command will also work for the WordPress example, even though both services run from images in the Docker registry and not Dockerfiles within the directory. This would be pulling an image and not building because we build from a Dockerfile.

This command lists the services as configured in the docker-compose.yml:
    docker-compose config --services

This command lists the images used by the created containers:
    docker-compose images

This command lists the logs from the services:
    docker-compose logs

docker-compose logs <service> lists the logs of a specific service, for example, docker-compose logs db.

This command lists the containers running based on the docker-compose:
    docker-compose ps

Note that in most cases, there is a difference between the results of docker-compose ps and docker ps. Containers that were not running in the context of docker-compose will not be displayed by the command docker-compose ps.

This command builds, creates, recreates, and runs services:
    docker-compose up

NOTE

When running docker-compose up, if one service exits, the entire command exits.

Running docker-compose up -d is running docker-compose up in detached mode. That is, the command will be running in the background.

Activity 2 — Analyzing the docker- compose CLI

To get you conversant with the docker-compose CLI.

You have been asked to demonstrate the difference in changes resulting from running two containers.

While still within the directory with the WordPress docker-compose.yml-- in my case, sandbox -- run the commands of Activity B-1, then the following commands:
docker-compose up -ddocker-compose stopdocker-compose rmdocker-compose startdocker-compose up -ddocker-compose stopdocker-compose start
返回列表 使用道具 举报
条评论
您需要登录后才可以回帖 登录 | 立即注册
高级
相关推荐