简体   繁体   English

在开发 Django 项目时,Docker 是“virtualenv”的替代方案吗?

[英]Is Docker an alternative for 'virtualenv' while develping Django project?

A virtual environment is required when creating an application and now I am using 'virtualenv' for creating a virtual environment while developing my Django application.创建应用程序时需要虚拟环境,现在我在开发 Django 应用程序时使用“virtualenv”创建虚拟环境。 I heard of Docker about its virtual environment.我听说Docker关于它的虚拟环境。 Am I able to use Docker as an alternative to virtualenv?我可以使用 Docker 作为 virtualenv 的替代品吗?

Managing Python versions and dependencies with virtualenv (or pipenv ) will likely be your method of choice for your app running on localhost .使用virtualenv (或pipenv )管理 Python 版本和依赖项可能是您在localhost运行的应用程序的首选方法。

You can of course run your app exclusively in a Docker container, where you won't have the need for any separate environments and will manage dependencies via the Dockerfile (and perhaps pip ).您当然可以在 Docker 容器中专门运行您的应用程序,在那里您不需要任何单独的环境,并且将通过Dockerfile (可能还有pip )管理依赖项。

I'd suggest the following approach: run an instance of your app on localhost and simultaneously a production version in a Docker container.我建议采用以下方法:在localhost上运行应用程序实例,同时在 Docker 容器中运行生产版本。 Also, run your database in a Docker container alongside (both managed via docker-compose .)此外,在旁边的 Docker 容器中运行您的数据库(均通过docker-compose管理。)

For a detailed walk-through on how to set this up (shameless plug), see this post or this GitHub repo有关如何设置(无耻插件)的详细演练,请参阅此帖子或此GitHub 存储库

While in practice, you could , you really shouldn't.在实践中,你可以,你真的不应该。 Docker should in most cases be a method of distribution, rather than a way of isolating your development environment.在大多数情况下,Docker 应该是一种分发方式,而不是一种隔离开发环境的方式。 That is to say that your final product -- the Django project you produce -- could be distributed as a docker image so it's easily deployed (and scaled via Kubernetes), but distributing an image just to handle the development environment is definitely nonstandard and I wouldn't recommend it.也就是说,你的最终产品——你生产的 Django 项目——可以作为 docker 镜像分发,所以它很容易部署(并通过 Kubernetes 扩展),但是分发一个镜像只是为了处理开发环境绝对是不标准的,我不会推荐它。

Instead, consider Pipenv which neatly combines pip and venv into a human-digestible package.相反,考虑一下Pipenv ,它将pipvenv巧妙地组合成一个人类可消化的包。 Store all that in your code repository, and setting up a development environment is as simple as将所有内容存储在您的代码存储库中,设置开发环境就像

git clone
pipenv install --dev

Yes docker can be used as an alternative.是的,docker 可以用作替代方案。

virtualenv just sets a fresh python installation in path which can be used to install dependencies particular to your project without polluting global python installation and thus managing versioning as well. virtualenv 只是在路径中设置了一个全新的 python 安装,它可用于安装特定于您的项目的依赖项,而不会污染全局 python 安装,从而也可以管理版本控制。

Docker is a step further and gives a complete OS - linux image and OS level isolation. Docker 更进了一步,提供了完整的操作系统 - linux 映像和操作系统级别隔离。 Not just one can have a isolated python installation but can also have other os specific dependencies installed like ffmpeg, lapack, arpack etc. A new developer can spin up a container from docker image having all dependencies (python and OS) installed.不仅可以安装独立的 Python,还可以安装其他特定于操作系统的依赖项,如 ffmpeg、lapack、arpack 等。新开发人员可以从安装了所有依赖项(python 和 OS)的 docker 镜像启动一个容器。

The usage of virtualenv or docker depends on your requirement or use case. virtualenv 或 docker 的使用取决于您的要求或用例。

Docker container encapsulates an entire OS and provides isolation of OS whereas a virtualenv only encapsulates Python dependencies which enables switching between Python versions and dependencies, but you are dependent with your host OS. Docker 容器封装了整个操作系统并提供了操作系统的隔离,而 virtualenv 只封装了 Python 依赖项,可以在 Python 版本和依赖项之间切换,但您依赖于您的主机操作系统。

Virtualenv can be used in case if you have two different projects on your machine that require two different versions of the same package with python.如果您的机器上有两个不同的项目需要使用 python 的同一个包的两个不同版本,则可以使用 Virtualenv。 Typically, the virtualenv provides abstraction from OS's use of python.通常,virtualenv 提供了操作系统对 python 使用的抽象。

Docker enables portability from one machine to another whereas with virtualenv you will have to do the installation in another place. Docker 支持从一台机器到另一台机器的可移植性,而使用 virtualenv,您将不得不在另一个地方进行安装。 Docker isolates all of the additional system dependencies too and provides a completely isolated environment. Docker 也隔离了所有额外的系统依赖项,并提供了一个完全隔离的环境。 It is containerization platform which is used to package your application and all its dependencies together in the form of containers so to make sure that your application works seamlessly in any environment which can be development or test or production.它是一个容器化平台,用于将您的应用程序及其所有依赖项以容器的形式打包在一起,以确保您的应用程序在任何开发、测试或生产环境中都能无缝运行。 It helps to ensure that the Dev and Production environment are completely same.它有助于确保开发和生产环境完全相同。

DOCKERFILE FROM python:3.9 ENV PYTHONUNBUFFERED=1 WORKDIR /code COPY requirements.txt /code/ RUN pip install -r requirements.txt COPY. DOCKERFILE FROM python:3.9 ENV PYTHONUNBUFFERED=1 WORKDIR /code COPY requirements.txt /code/ RUN pip install -r requirements.txt COPY。 /code/ /代码/

DOCKER-COMPOSE DOCKER-COMPOSE

version: '3.8' services: backend: build: .版本:'3.8' 服务:后端:构建:。 command: python manage.py runserver volumes: -.:/code ports: - "8000:8000"命令:python manage.py runserver volumes: -.:/code ports: - "8000:8000"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM