Как заставить django реагировать на изменения, которые я вношу в свой код (в докере)?
Я опубликую способ Я запускаю свой сервер в Docker.
- Работаю в wsl (Linux подсистемы Windows)
- Базовый файл docker для производства< /li>
Еще один для разработчиков, использующих его в качестве базы
Наблюдения
- При обслуживании рабочего сервера любое изменение в любом файле (а именно urls.py иviews.py) игнорируется. Добавление синтаксических ошибок или любые изменения не отражаются.
- Например, при обслуживании сервера с синтаксическими ошибками в urls.py и когда сервер все еще работает, удаление синтаксической ошибки , изменение работает, но любые последующие изменения не работают.
- Я проверил, что файлы меняются внутри контейнера докеров, но Django их игнорирует.
Dockerfile (базовый)
Код: Выделить всё
# Use Python 3.11.3 as the base image
FROM python:3.11.3-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV DJANGO_SETTINGS_MODULE=config.settings
# Set work directory
WORKDIR /app
# Install system dependencies and debugging tools
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
curl \
netcat-openbsd \
&& rm -rf /var/lib/apt/lists/*
# Install Python dependencies
COPY requirements.txt /app/
RUN pip install --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# Copy project
COPY . /app/
# Create a directory for static files
RUN mkdir -p /app/staticfiles && chmod 755 /app/staticfiles
# Create log directory and file
RUN mkdir -p /var/log && \
touch /var/log/app.log && \
chmod 666 /var/log/app.log
# Add these lines to print some debug info
RUN echo "Python version:" && python --version
RUN echo "Pip packages:" && pip list
RUN echo "Contents of /app:" && ls -la /app
RUN python -c "import django; print(f'Django version: {django.__version__}')"
RUN python -c "import sys; print(f'Python path: {sys.path}')"
# Expose port
EXPOSE 8080
# This is important for the version endpoint
RUN mkdir /build_artifacts; TZ=America/Argentina/Buenos_Aires date +"%Y-%m-%d %H:%M:%S" > /build_artifacts/build_time.txt
COPY docker_entrypoint_common.sh /app/docker_entrypoint_common.sh
COPY docker_entrypoint.sh /app/docker_entrypoint.sh
RUN chmod +x /app/docker_entrypoint.sh /app/docker_entrypoint_common.sh
ENTRYPOINT ["/app/docker_entrypoint.sh"]
Код: Выделить всё
# Dockerfile.dev
# Use the production image as the base
FROM personal-website-backend
# Install debugpy for debugging
RUN pip install debugpy
# Expose the debugging port
EXPOSE 5678
# Copy the common and development entrypoint scripts
COPY docker_entrypoint_common.sh /app/docker_entrypoint_common.sh
COPY docker_entrypoint_dev.sh /app/docker_entrypoint.sh
RUN chmod +x /app/docker_entrypoint.sh /app/docker_entrypoint_common.sh
# Mount your source code for hot-reloading (optional, see note below)
# This step is typically done at run time with a volume mount, not in the Dockerfile
# Set the entrypoint
ENTRYPOINT ["/app/docker_entrypoint.sh"]
Код: Выделить всё
#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status
# Common functionality for both production and development
# Set build time environment variable if needed
if [ -f "/build_artifacts/build_time.txt" ]; then
export BUILD_TIME=$(cat /build_artifacts/build_time.txt)
echo "Build time set to: $BUILD_TIME"
else
echo "ERROR: build_time.txt file not found."
exit 1
fi
Код: Выделить всё
#!/bin/bash
# Source the common entrypoint script
source /app/docker_entrypoint_common.sh
# Development-specific functionality
# Start Gunicorn with reload and debugpy for debugging
echo "Starting Gunicorn with reload and debugpy..."
# Source the common entrypoint script
source /app/docker_entrypoint_common.sh
# Development-specific functionality
# Start Django development server with debugpy for debugging
echo "Starting Django development server with debugpy..."
exec python -m debugpy --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8080 --reload
Код: Выделить всё
docker build -t personal-website-backend .
dockebuild -f Dockerfile.dev -t personal-website-backend_dev .
Код: Выделить всё
dockerun -it -p 8080:8080 -p 5678:5678 -v $(pwd):/app -e GCP_PROJECT_ID=my_id --env-file /home/noams/src/personal_website/backend/.secrets_backend --env-file /home/noams/src/personal_website/backend/.env personal-website-backend_dev /bin/bash
Как заставить django реагировать на изменения, которые я вношу в свой код?
Подробнее здесь: https://stackoverflow.com/questions/791 ... -in-docker
Мобильная версия