用Docker容器化Python应用程序的最佳实践

From reading many Python Docker container blogs, we’ve found that the majority of posts provide examples of how to containerize a Python application independent of its framework (Django, Flask, Falcon, etc.). For example, you might see something like this:

通过阅读许多Python Docker容器博客,我们发现大多数帖子都提供了如何将一个Python应用程序容器化的例子,而不是其框架(Django、Flask、Falcon等)。例如,你可能会看到这样的东西。

FROM python
WORKDIR /usr/app
COPY . .
RUN pip install -r requirements.txt
CMD [ "python", "app.py" ]

With this Dockerfile, we can build and run a Python Flask application:

通过这个Docker文件,我们可以构建并运行一个Python Flask应用程序。

docker build -t flask-application .
docker run -p 8080:5000 flask-application

Two simple steps and it works just fine, right?

两个简单的步骤,效果就很好,对吗?

While this example is simple and useful for demos and getting started tutorials, it leaves many important concerns unattended. So with that in mind, in this post, we’ll attend to those concerns and take a look at some 6 best practices when containerizing Python applications with Docker. We’ll explore why you should:

虽然这个例子很简单,对演示和入门教程也很有用,但它没有考虑到许多重要的问题。因此,考虑到这一点,在这篇文章中,我们将关注这些问题,并看看用Docker容器化Python应用程序时的6个最佳实践。我们将探讨为什么你应该。

  1. Use explicit and deterministic Docker base image tags for containerized Python applications.

    为容器化的Python应用程序使用显式和确定性的Docker基础镜像标签。

  2. Separate dependencies from source code.

    将依赖性与源代码分开。

  3. Use Python WSGI for production.

    在生产中使用Python WSGI。

  4. Run containers with least possible privilege (and never as root).

    以尽可能低的权限运行容器(永远不要以root身份)。

  5. Handle unhealthy states of your application.

    处理你的应用程序的不健康状态。

  6. Find and fix security vulnerabilities in your Python Docker application image.

    查找并修复Python Docker应用程序镜像中的安全漏洞。

1. Use explicit and deterministic Docker base image tags for containerized Python applications

1.为容器化的Python应用程序使用明确和确定的Docker基础镜像标签

While it may seem logical to use python as the base image for our Dockerized Python application, it leaves the question of which version of Python is being used.

虽然使用python 作为我们的Docker化Python应用程序的基础镜像似乎是合乎逻辑的,但它留下了使用哪个版本的Python的问题。

At the time of this article be...

开通本站会员,查看完整译文。

首页 - Wiki
Copyright © 2011-2024 iteam. Current version is 2.128.0. UTC+08:00, 2024-06-21 12:00
浙ICP备14020137号-1 $访客地图$