django - How to update django app deployed with docker-compose? Now it seems running old version of code
问题描述
I have django web application based on cookiecutter-django. The stack is build on several containers running: django, redis, celerybeat, celery worker, celery flower, postgres, caddy. When I launched application in production-like environment on VPS I have experienced strange behavior - django seems running old version of code (e.g. using version of form) despite checking out new code from git repository. I have tried few actions to "force" refresh of application code:
docker-compose down
and then, rebuild all containers withdocker-compose build
, and thendocker-compose up
similar rebuild as above but only for the container with django.
When I inspect code inside django container - there is proper version of code.
I did checkup app with Django Debug Toolbar
- and seems that pages are are not loaded from cache (there are no calls to cache from backend, and there is number of queries to database which might indicate that pages are not loaded from cache).
I was expecting that django will automatically detect change of code and restart running new code, additionally interpreter restart could be needed (which should be solved via putting containers down and rebuid). Are there ideas what else to check or try? Removing all containers, images and volumes helped but it is no my preferred way to introduce each update.
I went through solutions from Why does docker-compose build not reflect my django code changes? and After docker-compose build the docker-compose up run old not updated containers
but none was working for me except "nuke everything". Is there a way for "soft reload"?
Here is Dockerfile for django container:
# Dockerfile for django container
FROM python:3.6-alpine
ENV PYTHONUNBUFFERED 1
RUN apk update \
# psycopg2 dependencies
&& apk add --virtual build-deps gcc python3-dev musl-dev \
&& apk add postgresql-dev \
# Pillow dependencies
&& apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
# CFFI dependencies
&& apk add libffi-dev py-cffi
RUN addgroup -S django \
&& adduser -S -G django django
# Requirements are installed here to ensure they will be cached.
COPY ./requirements /requirements
RUN pip install --no-cache-dir -r /requirements/production.txt \
&& rm -rf /requirements
COPY ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r//' /entrypoint
RUN chmod +x /entrypoint
RUN chown django /entrypoint
COPY ./compose/production/django/start /start
RUN sed -i 's/\r//' /start
RUN chmod +x /start
RUN chown django /start
COPY ./compose/production/django/celery/worker/start /start-celeryworker
RUN sed -i 's/\r//' /start-celeryworker
RUN chmod +x /start-celeryworker
RUN chown django /start-celeryworker
COPY ./compose/production/django/celery/beat/start /start-celerybeat
RUN sed -i 's/\r//' /start-celerybeat
RUN chmod +x /start-celerybeat
RUN chown django /start-celerybeat
COPY ./compose/production/django/celery/flower/start /start-flower
RUN sed -i 's/\r//' /start-flower
RUN chmod +x /start-flower
COPY . /app
RUN chown -R django /app
USER django
WORKDIR /app
ENTRYPOINT ["/entrypoint"]
解决方案
万一有人遇到这种情况,答案是每次推送新代码时都必须重建容器。
只需运行
docker-compose -f production.yml build
以更新生产版本
推荐阅读
- eclipse - 如何找到导致修改代码某些部分的 SVN 分支或提交
- simulation - Anylogic 可以与 VR 设备集成吗?
- css - 下一个课后没有播放css动画
- javascript - 为 HH:MM:SS 制作 2 位倒数计时器
- json - 如何使用 v-for 获取本地 json 文件打印 1 个对象
- python - 使用 Django Rest 框架更新 ImageField
- python - 如何随机化列表中的函数?
- xml - 使用 xslt 交换元素或子树的位置
- python - 这个解释过的散列算法如何转化为(伪)代码
- node.js - Mongoose Schema.method() 不起作用,并显示错误消息