python web容器_容器化python Web应用程序

python web容器

In this article, we will try to build a ‘hello world’ web application and containerize the web application using docker. Why containerize? I am pretty sure there would be hundreds of articles explaining the why part in detail, but let me also give a few lines on the same. They allow us to build a software application that can be run independently of the computing environment. Thus allowing us to concentrate more on the software and less on the underlying infrastructure. The container can be thought like a box that has the software solution, its dependencies, and libraries, binaries, and the corresponding configuration files which can be run as a single unit. This is containerization in a nutshell, but you could browse through the web to understand the underlying essence of it in detail. In this article, we would learn to build a basic container and deploy our ‘Hello world’ python app on the container.

在本文中,我们将尝试构建一个“ hello world” Web应用程序,并使用Docker容器化该Web应用程序。 为什么要集装箱化? 我敢肯定,会有成百上千的文章详细解释了为什么要分开的内容,但让我在相同的地方写几行。 它们使我们能够构建可以独立于计算环境运行的软件应用程序。 因此,我们可以将更多的精力集中在软件上,而不必将精力集中在基础架构上。 可以将容器视为具有软件解决方案,其依赖项以及库,二进制文件和相应配置文件(可作为一个单元运行)的盒子。 简而言之,这是容器化,但是您可以浏览网络以详细了解其基础。 在本文中,我们将学习构建一个基本容器,并在该容器上部署“ Hello world” Python应用程序。

Before you go any further, make sure you install docker and Python3.

你再往前走之前,请确保您安装码头工人和Python3。

Let’s start building something awesome.

让我们开始构建一些很棒的东西。

Start by creating a directory, sub-directories, and files similar to below structure

首先创建类似于以下结构的目录,子目录和文件

$ mkdir learner_project$ mkdir learner_project/app$ touch learner_project/app/app.py$ touch learner_project/app/requirements.txt$ touch Dockerfile|-- Dockerfile
|-- app
| |-- app.py
| |-- requirements.txt
|-- static
`-- templates

The folder structure I have here is a typical python flask application structure. You could have it any way you like, but I like to keep things organized as per standards. You could ignore the directories static and templates for now.

我在这里拥有的文件夹结构是一个典型的python flask应用程序结构。 您可以按照自己喜欢的任何方式进行操作,但是我喜欢按照标准组织事情。 您现在可以忽略static目录和templates

I think with this you have set up a blueprint for your application and all you need now is to build the solution. Please note that we would not be writing more code in this article. The goal is to learn to containerize our application.

我认为您已经为应用程序建立了一个蓝图,现在您所要做的就是构建解决方案。 请注意,我们不会在本文中编写更多代码。 目的是学习容器化我们的应用程序。

The contents of app.py look the following:

app.py的内容如下所示:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
return 'Hello World! How are you doing?'


if __name__ == '__main__':
app.run(host='0.0.0.0', port=8008, debug=True)

The contents of the file requirements.txt are :

文件requirements.txt的内容为:

flask

That’s it and you are ready to run your python web application. When I run the application I see the following(I installed the flask library prior to execution):

就是这样,您就可以准备运行python Web应用程序了。 运行应用程序时,我看到以下内容(我在执行前安装了flask库):

$ python app/app.py 
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:8008/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 961-117-870

What does this mean? Your web application is running and listening on localhost:8008 . When you hit the URL in the browser you should see the following message

这是什么意思? 您的Web应用程序正在localhost:8008上运行并正在侦听。 当您在浏览器中单击URL时,应该看到以下消息

Hello World! How are you doing?

If you have come so far that means you can pat your back. You have built your web application. So far we were building the application on our machine and all the application modules exist in our machine only. Now, if anyone wants to run this application you would have to give all the steps that you had done so far, in order for them to successfully execute the app.

如果您已经走了那么远,那意味着您可以轻拍一下。 您已经构建了Web应用程序。 到目前为止,我们正在计算机上构建应用程序,而所有应用程序模块仅存在于计算机中。 现在,如果有人想运行该应用程序,则必须给出到目前为止已经完成的所有步骤,以使他们成功执行该应用程序。

Instead when we containerize our application we would be able to just give the container image and anyone could run a container from this image with a simple command and voila the application would be running as it were on our machine.

取而代之的是,当我们对应用程序进行容器化时,我们将只能够提供容器映像,任何人都可以使用简单的命令从该映像运行容器,瞧,该应用程序将像在计算机上一样运行。

For this, we would have to define the Dockerfile which would be having a list of instructions to build an image from which a container could be brought up and then the application can be run on anyone's machine using this image.

为此,我们将必须定义Dockerfile ,该文件将具有指令列表以构建可从中调出容器的映像,然后可以使用该映像在任何人的计算机上运行该应用程序。

FROM python:3.8-alpine

MAINTAINER Srinaveen Desu

COPY ./app/requirements.txt /app/requirements.txt

WORKDIR /app

RUN apk add --update \
&& pip install --upgrade pip \
&& pip install -r requirements.txt \
&& rm -rf /var/cache/apk/*

COPY ./app /app

CMD python app.py

The instruction FROM tells the image on which our application would be running and in this case, it is python followed by version tag 3.8-alpine.

FROM指令会告诉我们应用程序将在其上运行的图像,在这种情况下,它是python后跟版本标记3.8-alpine

The instruction MAINTAINER (optional) tells who is currently moderating this Dockerfile.

指令MAINTAINER (可选)告诉当前谁正在审核此Dockerfile。

The instruction COPY tells the file that needs to be copied from your local machine to the Docker container. (source to destination)

指令COPY告诉需要从本地计算机复制到Docker容器的文件。 (来源到目的地)

The instruction RUN executes the commands which are responsible for setting up the required environment for our application to run and these get added into the image we would be building.

RUN指令执行命令,这些命令负责设置应用程序运行所需的环境,并将这些命令添加到我们将要构建的映像中。

The instruction CMD is a command which is run once our container starts. A typical use case would be starting the web application server.

指令CMD是在容器启动后运行的命令。 典型的用例是启动Web应用程序服务器。

Now that our Dockerfile is ready, we are ready to build the image and run the container. The command is run in the directory where Dockerfile exists. It builds the image flask-demo-container which is versioned 1.0

现在我们的Dockerfile已经准备好了,我们准备构建映像并运行容器。 该命令在Dockerfile所在的目录中运行。 它构建了版本为1.0的图像flask-demo-container

$ pwd              
/Users/desu/learner_project$ docker build --tag flask-demo-container:1.0 .Sending build context to Docker daemon 9.047MBStep 1/7 : FROM python:3.8-alpine---> 44fceb565b2aStep 2/7 : MAINTAINER Srinaveen Desu---> Using cache---> 81c543f544caStep 3/7 : COPY ./app/requirements.txt /app/requirements.txt---> Using cache---> 7446b6ed7a69Step 4/7 : WORKDIR /app---> Using cache---> 76a94dbf8407Step 5/7 : RUN apk add --update && pip install --upgrade pip && pip install -r requirements.txt && rm -rf /var/cache/apk/*---> Using cache---> bda14c8083c5Step 6/7 : COPY ./app /app---> a5f384727c69Step 7/7 : CMD python app.py---> Running in dc00695397f9Removing intermediate container dc00695397f9---> 853572c38b75Successfully built 853572c38b75Successfully tagged flask-demo-container:1.0

If you see the two successful messages you are good to go to the next step which is running the container.

如果看到两条成功消息,则可以继续执行运行容器的下一步。

$ docker run --publish 8000:8008 --name flask-demo-app flask-demo-container:1.0* Serving Flask app "app" (lazy loading)* Environment: productionWARNING: This is a development server. Do not use it in a production deployment.Use a production WSGI server instead.* Debug mode: on* Running on http://0.0.0.0:8008/ (Press CTRL+C to quit)* Restarting with stat* Debugger is active!* Debugger PIN: 185-072-509172.17.0.1 - - [01/Sep/2020 06:03:47] "GET / HTTP/1.1" 200 -

If we hit the URL localhost:8000 you would see the hello world message you had seen earlier.

如果我们访问URL localhost:8000,您将看到您之前看到的hello world消息。

--publish forwards traffic hit at the port 8000of the local machine to the port 8008 of the container.

--publish将在本地计算机的端口8000命中的流量转发到容器的端口8008

--name is the unique name that you give to the container.

--name是您赋予容器的唯一名称。

And the last part of the command is the image followed by the version that we want the container to be run from.

命令的最后一部分是映像,后面是我们要从中运行容器的版本。

If we want to run the container in the detached mode(running the container in the background) you would have to use the following command:

如果我们要以分离模式运行容器(在后台运行容器),则必须使用以下命令:

$ docker run --detach  -p 8004:8008 --name flask-demo-app flask-demo-container:1.1

Note1: I have changed the image version to 1.1

注意1:我将图像版本更改为1.1

Note2: In case you want to stop the container (in the case of detached mode) you would have to run the following command

注意2:如果要停止容器(在分离模式下),则必须运行以下命令

docker stop <container-name>

In case you want to run the container again, you would have to remove the container before issuing the run command given above.

如果要再次运行该容器,则必须在发出上述运行命令之前先删除该容器。

docker rm <container-name>

That was all for this article. Hope you had some fun learning cool stuff :).

这就是本文的全部内容。 希望您在学习有趣的东西时有一些乐趣:)。

References: Docker Doc

参考: Docker Doc

翻译自: https://medium.com/swlh/containerizing-python-web-application-1052e61cf201

python web容器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值