Невозможно запустить мой проект Django с Gunicorn внутри Docker
Я новичок в Docker.
В Visual Studio Code есть расширение под названием Remote - Containers
, и я использую его для докеризации проекта Django.
Для первого шага расширение создает Dockerfile:
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.10.5
EXPOSE 8000
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
# File wsgi.py was not found. Please enter the Python path to wsgi file.
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "project.wsgi"]
Затем он добавляет файл конфигурации контейнера разработки:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.238.0/containers/docker-existing-dockerfile
{
"name": "django-4.0.5",
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerFile": "../Dockerfile"
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Uncomment the next line to run commands after the container is created - for example installing curl.
// "postCreateCommand": "apt-get update && apt-get install -y curl",
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}
Наконец, я выполняю команду Rebuild and Reopen in Container.
Через несколько секунд контейнер запускается, и я вижу командную строку . Учитывая, что в конце Dockerfile есть такая строка:
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "project.wsgi"]
... приложение Django должно быть запущено, но это не так, и http://127.0.0.1:8000
отказывается подключаться.
Но если я выполню ту же команду в командной строке:
gunicorn --bind 0.0.0.0:8000 project.wsgi
Все работает нормально?
Почему? Все файлы создаются расширением VSCode. Те же инструкции размещены на официальном сайте VSCode, но они не работают.
P.S: Когда контейнер загрузился в первый раз, я создал проект под названием 'project', поэтому структура папок выглядит следующим образом: