E
E
ErikHabr2022-03-13 23:01:45
Django
ErikHabr, 2022-03-13 23:01:45

How to force docker to update django code?

Tell me how to make it so that when I make changes in the code, the docker automatically updates the container and these changes are applied? It turns out very inconvenient to work now. I have mac os (if that matters).

Docker example

FROM python:3.8

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN mkdir /efood_backend

WORKDIR /efood_backend

COPY requirements.txt /efood_backend/

RUN pip install --upgrade pip && pip install -r requirements.txt

ADD . /efood_backend/


docker-compose.yaml example
version: '3'

services:
  backend:
    build: .
    container_name: efood_backend
    #hostname: efood_backend
    restart: always
    command: python manage.py runserver 0.0.0.0:8000  # python backend/manage.py ?
    volumes:
      - .:/efood-backend
    ports:
      - "0.0.0.0:8000:8000"  # 8000:8000 ?
    env_file:
      - .env
    depends_on:
      - pgdb
      - celery
      - redis

  celery:
    build: .
    container_name: efood_celery
    command: celery -A project worker -l INFO
    volumes:
      - .:/efood-backend
    env_file:
      - .env
    depends_on:
      - redis
      - pgdb

  celery_beat:
    build: .
    container_name: efood_celery_beat
    command: celery -A project beat -l info
    env_file:
      - .env
    depends_on:
      - redis
      - pgdb
      - celery
    volumes:
      - .:/efood-backend

  pgdb:
    image: postgres
    container_name: efood_pgdb
    env_file:
      - .env
    volumes:
      - efood-pgdata:/var/lib/postgresql/data/

  pgadmin:
    container_name: efood_pgadmin
    image: dpage/pgadmin4
    depends_on:
      - pgdb
    restart: unless-stopped
    env_file:
      - .env
    ports:
      - 8080:80
    volumes:
      - efood-pgadmin-data:/var/lib/pgadmin
    links:
      - "pgdb:pgsql-server"

  redis:
    image: "redis:alpine"
    container_name: efood_redis

volumes:
  efood-pgdata:
  efood-pgadmin-data:

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Karabanov, 2022-03-13
@karabanov

RUN mkdir /efood_backendThere is no need to pre-call - WORKDIR /efood_backendit will create the directory itself.
You can try to run the application not directly python manage.py runserver 0.0.0.0:8000, but with the help of gunicorn - it has the ability to autoload in case of a file change, you need to specify which file you need to monitor something like this:

--preload --reload --reload-extra-file /path/to/file/reload.txt

E
ErikHabr, 2022-03-14
@ErikHabr

Strangely, after hours of torment and searching, I decided to shorten the name of the efood-backend folder to just efood and everything works as it should. Containers automatically update themselves.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question