我需要在使用Docker时使用虚拟环境吗?

3

我不知道Docker相比虚拟环境的优势在哪里。是否需要同时使用虚拟环境和Docker。

今天之前,基本上我都是使用虚拟环境来创建Django项目。但是今天我的朋友向我推荐了使用Docker。我很困惑该如何选择?

我使用以下命令创建虚拟环境:python3 -m venv virtual_environment_name。这是创建虚拟环境的最佳方式吗?还是应该使用其他方法来创建虚拟环境?

1个回答

3
我更倾向于在本地开发环境中使用pipenv替代virtualenv,而在生产中则使用不带虚拟环境的docker。以下是我的Dockerfile(使用gunicorn运行django):
FROM python:3.6

ENV PYTHONUNBUFFERED 1

# switch system download source
RUN python -c "s='mirrors.163.com';import re;from pathlib import Path;p=Path('/etc/apt/sources.list');p.write_text(re.sub(r'(deb|security)\.debian\.org', s, p.read_text()))"
RUN apt-get update

# aliyun source for pip
RUN python -c "s='mirrors.aliyun.com';from pathlib import Path;p=Path.home()/'.pip';p.mkdir();(p/'pip.conf').write_text(f'[global]\nindex-url=https://{s}/pypi/simple\n[install]\ntrusted-host={s}\n')"

# Optional: install and conf vim, install ipython
RUN apt-get install -y vim
RUN wget https://raw.githubusercontent.com/waketzheng/carstino/master/.vimrc
RUN pip install ipython

# copy source code to docker image
WORKDIR /carrot
ADD . .

# required packages for carrot
RUN apt-get install -y ruby-sass

# install gunicorn and Pipfile
RUN pip install pipenv gunicorn
RUN pipenv install --system
RUN python manage.py collectstatic --noinput

# database name and rpc server ip
ENV POSTGRES_HOST=db
ENV RPC_SERVER_IP=172.21.0.2

EXPOSE 9000

# the PASSWORD env should be replace to a real one
CMD ["gunicorn", "--bind", ":9000", "--env", "PASSWORD=123456", "--error-logfile", "gunicorn.error", "--log-file", "gunicorn.log", "carrot.wsgi:application"]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接