Docker-compose how to update celery without rebuild?

Viewed 75

I am working on my django + celery + docker-compose project.

Problem

  1. I changed django code
  2. Update is working only after docker-compose up --build

How can I enable code update without rebuild?

I found this answer Developing with celery and docker but didn't understand how to apply it

docker-compose.yml

version: '3.9'

services:
  django:
    build: ./project # path to Dockerfile
    command: sh -c " 
      gunicorn --bind 0.0.0.0:8000 core_app.wsgi"
    volumes:
      - ./project:/project
      - ./project/static:/project/static
      - media-volume:/project/media
    expose:
      - 8000
  
  celery:
    build: ./project
    command: celery -A documents_app worker  --loglevel=info
    volumes:
      - ./project:/usr/src/app
      - media-volume:/project/media

    depends_on:
      - django
      - redis

.........
volumes:
    pg_data:
    static:
    media-volume:
1 Answers

Code update without rebuild is achievable and best practice when working with containers otherwise it takes too much time and effort creating a new image every time you change the code.

The most popular way of doing this is to mount your code directory into the container using one of the two methods below.

In your docker-compose.yml

services:
  web:
    volumes:
      - ./codedir:/app/codedir   # while 'codedir' is your code directory

In CLI starting a new container

$ docker run -it --mount "type=bind,source=$(pwd)/codedir,target=/app/codedir" celery bash

So you're effectively mounting the directory that your code lives in on your computer inside of the /opt/ dir of the Celery container. Now you can change your code and...

the local directory overwrites the one from the image when the container is started. You only need to build the image once and use it until the installed dependencies or OS-level package versions need to be changed. Not every time your code is modified. - Quoted from this awesome article

Related