嗨,我是 Docker 新手,我创建了一个 Django 应用程序,它在本地计算机上运行,很好,但是 当我尝试在 docker 容器中运行时,它运行正常,但是当我发送请求时,发现错误模板不存在
Dockerfile
#syntax=docker/dockerfile:1
#FROM ubuntu:latest
FROM ubuntu:latest
FROM python:3.8-slim-buster
WORKDIR /app
#COPY requirements.txt requirements.txt
#RUN pip3 install -r requirements.txt
#
#CMD python . /manage.py runserver 0.0.0.0:8000"""
# pull the official base image
# set work directory
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
RUN apt-get update&& \
apt-get -y install sudo
RUN apt-get install -y tesseract-ocr
RUN apt-get install -y build-essential libpoppler-cpp-dev pkg-config python-dev mupdf
RUN apt-get install ffmpeg libsm6 libxext6 -y
RUN apt-get install -y default-jre
RUN apt-get install -y default-jdk
RUN apt-get update
RUN apt-get install -y python3-pip
RUN pip3 install --upgrade pip
#RUN pip3 install virtualenv
#RUN python3 -m virtualenv env
#RUN source env/bin/activate
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
# copy project
COPY . /app
EXPOSE 8000
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
#
我的 Docker 文件看起来像这样
docker-cpmpose.yml
version: "3"
services:
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000
Folder Structure
Directory: E:\djangoproject\blogproject
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 22-12-2022 19:39 Blog
d----- 22-12-2022 19:39 Blogproject
d----- 22-12-2022 19:40 venv
-a---- 22-12-2022 19:46 192512 db.sqlite3
-a---- 21-12-2022 18:02 153 docker-compose.yml
-a---- 22-12-2022 15:51 1097 Dockerfile
-a---- 24-12-2021 13:00 689 manage.py
-a---- 20-12-2022 13:09 578 requirements.txt
我的模板文件夹位于博客应用程序 templates/blog/templtes.html 中
当我运行 #docker-compose up 时,我的图像服务器已启动,但随后我从我的 brouser 发送了请求,我收到错误页面
TemplateDoesNotExist at /
blog/pages.html
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 3.2.8
Exception Type: TemplateDoesNotExist
Exception Value:
blog/pages.html
Exception Location: /usr/local/lib/python3.8/site-packages/django/template/backends/django.py, line 84, in reraise
Python Executable: /usr/local/bin/python3
Python Version: 3.8.16
Python Path:
['/app',
'/usr/local/lib/python38.zip',
'/usr/local/lib/python3.8',
'/usr/local/lib/python3.8/lib-dynload',
'/usr/local/lib/python3.8/site-packages']
Server time: Fri, 23 Dec 2022 05:53:42 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
This engine did not provide a list of tried templates.
Error during template rendering
In template /app/Blog/templates/blog/base.html, error at line 0
有人可以帮我修复这个错误
根据您的 Dockerfile,您必须将 django 项目从一个文件夹复制到另一个文件夹,并且您没有设置 workdir。
而不是这个:
COPY . /app #here only you copied but didn't set where to copy
试试这个方法:
COPY ./app /app #here copied Django project from one folder to another.
WORKDIR /app
注意:不要忘记重新构建此 Dockerfile。
就我而言,在使用 Docker 之前,我包含了一个像这样的 html 文件:
{% include 'auth_app\navbar.html' %}
现在因为我使用的是 Docker 容器,它使用 Linux,所以路径应该是
{% include 'auth_app/navbar.html' %}
我希望这有帮助!