使用Conda环境转移Django项目到Docker容器

发布于 2025-02-05 18:26:10 字数 12216 浏览 2 评论 0原文

我是Django的新手,并且正在尝试关注William S. Vincent的专业人士3.1的Django书。在这种情况下,我正在尝试使用Pycharm IDE上的Conda环境在系统(MAC OS)上移动一个简单的Django项目,并将其移动到Docker容器。

该书为项目使用pipenv的问题

,并建议在dockerfile中输入以下代码: ”一书中给出

,由于我正在为项目使用Conda环境,因此我无法在Dockerfile中使用上述代码。

我尝试的

步骤1

是通过输入以下代码来创建Environment.yml文件,其中包含项目使用的所有软件包。

(django_for_professionals_31) My-MacBook-Pro:django_for_professionals_31_ch1 me$ conda env export --no-builds > environment.yml

我的emoverition.yml文件看起来如下*:

name: django_for_professionals_31
channels:
  - defaults
dependencies:
  - asgiref=3.4.1
  - bzip2=1.0.8
  - ca-certificates=2022.4.26
  - certifi=2022.5.18.1
  - django=3.2.5
  - krb5=1.19.2
  - libedit=3.1.20210910
  - libffi=3.3
  - libpq=12.9
  - ncurses=6.3
  - openssl=1.1.1o
  - pip=21.2.4
  - psycopg2=2.8.6
  - python=3.10.4
  - pytz=2021.3
  - readline=8.1.2
  - setuptools=61.2.0
  - sqlite=3.38.3
  - sqlparse=0.4.1
  - tk=8.6.12
  - typing_extensions=4.1.1
  - tzdata=2022a
  - wheel=0.37.1
  - xz=5.2.5
  - zlib=1.2.12
prefix: /opt/anaconda3/envs/django_for_professionals_31

步骤2

,然后,基于这个教程,我试图编写我的dockerfile,如下所示:

# Pull base image
FROM continuumio/miniconda3

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Create the environment:
COPY environment.yml .
RUN conda env create -f environment.yml

# Make RUN commands use the new environment:
SHELL ["conda", "run", "-n", "django_for_professionals_31", "/bin/bash", "-c"]

# Demonstrate the environment is activated:
RUN echo "Make sure django is installed:"
RUN python -c "import django"

# The code to run when container is started:
COPY run.py .
ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "django_for_professionals_31", "python", "run.py"]

# Copy project
COPY . /code/

此后,我运行了命令来构建Docker映像。我得到的命令和输出如下:

(django_for_professionals_31) My-MacBook-Pro:django_for_professionals_31_ch1 me$ docker build .
[+] Building 3.6s (12/13)                                                                                                                               
 => [internal] load build definition from Dockerfile                                                                                               0.0s
 => => transferring dockerfile: 737B                                                                                                               0.0s
 => [internal] load .dockerignore                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                    0.0s
 => [internal] load metadata for docker.io/continuumio/miniconda3:latest                                                                           3.3s
 => [auth] continuumio/miniconda3:pull token for registry-1.docker.io                                                                              0.0s
 => [1/8] FROM docker.io/continuumio/miniconda3@sha256:24103733efebe6d610d868ab16a6f0e5f114c7aad0326a793d946b73af15391d                            0.0s
 => [internal] load build context                                                                                                                  0.0s
 => => transferring context: 5.29kB                                                                                                                0.0s
 => CACHED [2/8] WORKDIR /code                                                                                                                     0.0s
 => CACHED [3/8] COPY environment.yml .                                                                                                            0.0s
 => CACHED [4/8] RUN conda env create -f environment.yml                                                                                           0.0s
 => CACHED [5/8] RUN echo "Make sure django is installed:"                                                                                         0.0s
 => CACHED [6/8] RUN python -c "import django"                                                                                                     0.0s
 => ERROR [7/8] COPY run.py .                                                                                                                      0.0s
------
 > [7/8] COPY run.py .:
------
failed to compute cache key: "/run.py" not found: not found

第3步,

我不确定下一步该错误时该怎么办,我的第一个本能是在 dockerfile中的部分。因此,我这样做了,我的dockerfile看起来如下:

# Pull base image
FROM continuumio/miniconda3

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Create the environment:
COPY environment.yml .
RUN conda env create -f environment.yml

# Make RUN commands use the new environment:
SHELL ["conda", "run", "-n", "django_for_professionals_31", "/bin/bash", "-c"]

# Demonstrate the environment is activated:
RUN echo "Make sure django is installed:"
RUN python -c "import django"

# The code to run when container is started:
#COPY run.py .
#ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "django_for_professionals_31", "python", "run.py"]

# Copy project
COPY . /code/

在终端重新运行$ docker build。时,我能够创建Docker Image,我得到的输出的最后一行是=> =>编写图像SHA256:...

步骤4

然后,创建docker-compose.yml文件所需的书。我在书中使用了完全相同的代码,下面给出:

version: '3.8'

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000

之后,我运行了基于创建的图像来构建Docker容器的命令。命令和输出如下所示。

(django_for_professionals_31) My-MacBook-Pro:django_for_professionals_31_ch1 me$ docker-compose up
[+] Building 3.8s (13/13) FINISHED                                                                                                                      
 => [internal] load build definition from Dockerfile                                                                                               0.0s
 => => transferring dockerfile: 734B                                                                                                               0.0s
 => [internal] load .dockerignore                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                    0.0s
 => [internal] load metadata for docker.io/continuumio/miniconda3:latest                                                                           3.4s
 => [auth] continuumio/miniconda3:pull token for registry-1.docker.io                                                                              0.0s
 => [1/7] FROM docker.io/continuumio/miniconda3@sha256:24103733efebe6d610d868ab16a6f0e5f114c7aad0326a793d946b73af15391d                            0.0s
 => [internal] load build context                                                                                                                  0.1s
 => => transferring context: 5.11kB                                                                                                                0.0s
 => CACHED [2/7] WORKDIR /code                                                                                                                     0.0s
 => CACHED [3/7] COPY environment.yml .                                                                                                            0.0s
 => CACHED [4/7] RUN conda env create -f environment.yml                                                                                           0.0s
 => CACHED [5/7] RUN echo "Make sure django is installed:"                                                                                         0.0s
 => CACHED [6/7] RUN python -c "import django"                                                                                                     0.0s
 => [7/7] COPY . /code/                                                                                                                            0.1s
 => exporting to image                                                                                                                             0.1s
 => => exporting layers                                                                                                                            0.1s
 => => writing image sha256:d0166e8db7d10a43f18975955b398d9227ae7c2a217a69a2fe76a9cc869c0917                                                       0.0s
 => => naming to docker.io/library/django_for_professionals_31_ch1_web                                                                             0.0s
[+] Running 2/2
 ⠿ Network django_for_professionals_31_ch1_default  Created                                                                                        0.1s
 ⠿ Container django_for_professionals_31_ch1-web-1  Created                                                                                        0.2s
Attaching to django_for_professionals_31_ch1-web-1
django_for_professionals_31_ch1-web-1  | Traceback (most recent call last):
django_for_professionals_31_ch1-web-1  |   File "/code/manage.py", line 11, in main
django_for_professionals_31_ch1-web-1  |     from django.core.management import execute_from_command_line
django_for_professionals_31_ch1-web-1  | ModuleNotFoundError: No module named 'django'
django_for_professionals_31_ch1-web-1  | 
django_for_professionals_31_ch1-web-1  | The above exception was the direct cause of the following exception:
django_for_professionals_31_ch1-web-1  | 
django_for_professionals_31_ch1-web-1  | Traceback (most recent call last):
django_for_professionals_31_ch1-web-1  |   File "/code/manage.py", line 22, in <module>
django_for_professionals_31_ch1-web-1  |     main()
django_for_professionals_31_ch1-web-1  |   File "/code/manage.py", line 13, in main
django_for_professionals_31_ch1-web-1  |     raise ImportError(
django_for_professionals_31_ch1-web-1  | ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
django_for_professionals_31_ch1-web-1 exited with code 1

从以上看来,在我看来(我可能错了)在评论了dockerfile中的两行代码后,conda环境不会在docker容器中激活,但包括这两行代码的导致无法计算高速缓存密钥:“/run.py”找不到:找不到错误,因为无法创建图像。

如何解决上述问题并创建Docker映像并使用它来为我的项目构建容器?任何帮助都将不胜感激。谢谢。


* 注释 :我从emoverition.yml文件中删除了libcxx = 12.0.0软件包,因为使用它,在运行$ docker build。阅读

> [4/7] RUN conda env create -f environment.yml:                                                                                                       
#9 1.036 Collecting package metadata (repodata.json): ...working... done                                                                                
#9 8.859 Solving environment: ...working... failed                                                                                                      
#9 8.874 
#9 8.874 ResolvePackageNotFound: 
#9 8.874   - libcxx=12.0.0
#9 8.874 

I am new to Django and am trying to follow the book Django for Professionals 3.1 by William S. Vincent. In this context, I am trying to move a simple Django project currently on my system (Mac OS) using conda environment on the PyCharm IDE to a Docker container.

The Problem

The book uses pipenv for the project and suggests to enter the following code in the Dockerfile:
Dockerfile code given in the book

However, since I am using a Conda environment for the project, I cannot use the above code in Dockerfile.

What I Tried

Step 1

I started by entering the following code to create the environment.yml file containing all packages that the project uses.

(django_for_professionals_31) My-MacBook-Pro:django_for_professionals_31_ch1 me$ conda env export --no-builds > environment.yml

My environment.yml file looks like the following*:

name: django_for_professionals_31
channels:
  - defaults
dependencies:
  - asgiref=3.4.1
  - bzip2=1.0.8
  - ca-certificates=2022.4.26
  - certifi=2022.5.18.1
  - django=3.2.5
  - krb5=1.19.2
  - libedit=3.1.20210910
  - libffi=3.3
  - libpq=12.9
  - ncurses=6.3
  - openssl=1.1.1o
  - pip=21.2.4
  - psycopg2=2.8.6
  - python=3.10.4
  - pytz=2021.3
  - readline=8.1.2
  - setuptools=61.2.0
  - sqlite=3.38.3
  - sqlparse=0.4.1
  - tk=8.6.12
  - typing_extensions=4.1.1
  - tzdata=2022a
  - wheel=0.37.1
  - xz=5.2.5
  - zlib=1.2.12
prefix: /opt/anaconda3/envs/django_for_professionals_31

Step 2

Then, based on this tutorial, I tried to write my Dockerfile, as shown below:

# Pull base image
FROM continuumio/miniconda3

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Create the environment:
COPY environment.yml .
RUN conda env create -f environment.yml

# Make RUN commands use the new environment:
SHELL ["conda", "run", "-n", "django_for_professionals_31", "/bin/bash", "-c"]

# Demonstrate the environment is activated:
RUN echo "Make sure django is installed:"
RUN python -c "import django"

# The code to run when container is started:
COPY run.py .
ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "django_for_professionals_31", "python", "run.py"]

# Copy project
COPY . /code/

Thereafter, I ran the command to build a docker image. The command and output I got are given below:

(django_for_professionals_31) My-MacBook-Pro:django_for_professionals_31_ch1 me$ docker build .
[+] Building 3.6s (12/13)                                                                                                                               
 => [internal] load build definition from Dockerfile                                                                                               0.0s
 => => transferring dockerfile: 737B                                                                                                               0.0s
 => [internal] load .dockerignore                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                    0.0s
 => [internal] load metadata for docker.io/continuumio/miniconda3:latest                                                                           3.3s
 => [auth] continuumio/miniconda3:pull token for registry-1.docker.io                                                                              0.0s
 => [1/8] FROM docker.io/continuumio/miniconda3@sha256:24103733efebe6d610d868ab16a6f0e5f114c7aad0326a793d946b73af15391d                            0.0s
 => [internal] load build context                                                                                                                  0.0s
 => => transferring context: 5.29kB                                                                                                                0.0s
 => CACHED [2/8] WORKDIR /code                                                                                                                     0.0s
 => CACHED [3/8] COPY environment.yml .                                                                                                            0.0s
 => CACHED [4/8] RUN conda env create -f environment.yml                                                                                           0.0s
 => CACHED [5/8] RUN echo "Make sure django is installed:"                                                                                         0.0s
 => CACHED [6/8] RUN python -c "import django"                                                                                                     0.0s
 => ERROR [7/8] COPY run.py .                                                                                                                      0.0s
------
 > [7/8] COPY run.py .:
------
failed to compute cache key: "/run.py" not found: not found

Step 3

I was not sure what to do next when I got this error, and my first instinct was to comment out the code under the # The code to run when container is started: section in Dockerfile. So, I did that and my Dockerfile looked like the following:

# Pull base image
FROM continuumio/miniconda3

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Create the environment:
COPY environment.yml .
RUN conda env create -f environment.yml

# Make RUN commands use the new environment:
SHELL ["conda", "run", "-n", "django_for_professionals_31", "/bin/bash", "-c"]

# Demonstrate the environment is activated:
RUN echo "Make sure django is installed:"
RUN python -c "import django"

# The code to run when container is started:
#COPY run.py .
#ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "django_for_professionals_31", "python", "run.py"]

# Copy project
COPY . /code/

Upon re-running the $ docker build . on the terminal, I was able to create the Docker image, as the last line of the output I got was => => writing image sha256:....

Step 4

Then, the book required to create the docker-compose.yml file. I am using the exact same code in the book, given below:

version: '3.8'

services:
  web:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - 8000:8000

After that, I ran the command to build a Docker container based on the created image. The command and output are shown below.

(django_for_professionals_31) My-MacBook-Pro:django_for_professionals_31_ch1 me$ docker-compose up
[+] Building 3.8s (13/13) FINISHED                                                                                                                      
 => [internal] load build definition from Dockerfile                                                                                               0.0s
 => => transferring dockerfile: 734B                                                                                                               0.0s
 => [internal] load .dockerignore                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                    0.0s
 => [internal] load metadata for docker.io/continuumio/miniconda3:latest                                                                           3.4s
 => [auth] continuumio/miniconda3:pull token for registry-1.docker.io                                                                              0.0s
 => [1/7] FROM docker.io/continuumio/miniconda3@sha256:24103733efebe6d610d868ab16a6f0e5f114c7aad0326a793d946b73af15391d                            0.0s
 => [internal] load build context                                                                                                                  0.1s
 => => transferring context: 5.11kB                                                                                                                0.0s
 => CACHED [2/7] WORKDIR /code                                                                                                                     0.0s
 => CACHED [3/7] COPY environment.yml .                                                                                                            0.0s
 => CACHED [4/7] RUN conda env create -f environment.yml                                                                                           0.0s
 => CACHED [5/7] RUN echo "Make sure django is installed:"                                                                                         0.0s
 => CACHED [6/7] RUN python -c "import django"                                                                                                     0.0s
 => [7/7] COPY . /code/                                                                                                                            0.1s
 => exporting to image                                                                                                                             0.1s
 => => exporting layers                                                                                                                            0.1s
 => => writing image sha256:d0166e8db7d10a43f18975955b398d9227ae7c2a217a69a2fe76a9cc869c0917                                                       0.0s
 => => naming to docker.io/library/django_for_professionals_31_ch1_web                                                                             0.0s
[+] Running 2/2
 ⠿ Network django_for_professionals_31_ch1_default  Created                                                                                        0.1s
 ⠿ Container django_for_professionals_31_ch1-web-1  Created                                                                                        0.2s
Attaching to django_for_professionals_31_ch1-web-1
django_for_professionals_31_ch1-web-1  | Traceback (most recent call last):
django_for_professionals_31_ch1-web-1  |   File "/code/manage.py", line 11, in main
django_for_professionals_31_ch1-web-1  |     from django.core.management import execute_from_command_line
django_for_professionals_31_ch1-web-1  | ModuleNotFoundError: No module named 'django'
django_for_professionals_31_ch1-web-1  | 
django_for_professionals_31_ch1-web-1  | The above exception was the direct cause of the following exception:
django_for_professionals_31_ch1-web-1  | 
django_for_professionals_31_ch1-web-1  | Traceback (most recent call last):
django_for_professionals_31_ch1-web-1  |   File "/code/manage.py", line 22, in <module>
django_for_professionals_31_ch1-web-1  |     main()
django_for_professionals_31_ch1-web-1  |   File "/code/manage.py", line 13, in main
django_for_professionals_31_ch1-web-1  |     raise ImportError(
django_for_professionals_31_ch1-web-1  | ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
django_for_professionals_31_ch1-web-1 exited with code 1

From the above, it seems to me that (I could be wrong though) after commenting out the two lines of code in Dockerfile, the conda environment does not get activated in the Docker container but including those two lines of code leads to the failed to compute cache key: "/run.py" not found: not found error because of which the image cannot be created.

How can I resolve the above issue and create a docker image and use that to build a container for my project? Any help would be much appreciated. Thanks.


*Note: I removed the libcxx=12.0.0 package from the environment.yml file because with it, I got the following error upon running $ docker build . on the terminal. I took the advice to remove this package after reading this

> [4/7] RUN conda env create -f environment.yml:                                                                                                       
#9 1.036 Collecting package metadata (repodata.json): ...working... done                                                                                
#9 8.859 Solving environment: ...working... failed                                                                                                      
#9 8.874 
#9 8.874 ResolvePackageNotFound: 
#9 8.874   - libcxx=12.0.0
#9 8.874 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2025-02-12 18:26:11

尝试以下操作:

emoverition.yml

name: myenv
channels:
  - conda-forge
dependencies:
  - python=3.8
  - asgiref==3.3.1
  - Django==3.1.7
  - pytz==2021.1
  - sqlparse==0.4.1

dockerfile

FROM continuumio/miniconda3


WORKDIR /app


COPY environment.yml .
RUN conda env create -f environment.yml


SHELL ["conda","run","-n","myenv","/bin/bash","-c"]



COPY . .
#start server inside container
CMD ["conda", "run", "--no-capture-output","-n", "myenv", "python3","manage.py", "runserver","0.0.0.0:8000"]

在当前的dir,构建图像:

docker build --tag python-django .

运行图像:

docker run --publish 8000:8000 python-django

Try this :

environment.yml

name: myenv
channels:
  - conda-forge
dependencies:
  - python=3.8
  - asgiref==3.3.1
  - Django==3.1.7
  - pytz==2021.1
  - sqlparse==0.4.1

Dockerfile

FROM continuumio/miniconda3


WORKDIR /app


COPY environment.yml .
RUN conda env create -f environment.yml


SHELL ["conda","run","-n","myenv","/bin/bash","-c"]



COPY . .
#start server inside container
CMD ["conda", "run", "--no-capture-output","-n", "myenv", "python3","manage.py", "runserver","0.0.0.0:8000"]

At the current dir, Build image :

docker build --tag python-django .

Run the image :

docker run --publish 8000:8000 python-django
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文