为什么 docker-compose build 没有反映我的 Django 代码更改? [英] Why does docker-compose build not reflect my django code changes?

查看:57
本文介绍了为什么 docker-compose build 没有反映我的 Django 代码更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个使用 docker-compose 部署的 Django 项目.我的 profile_form.html 模板中有一个简单的错误,其中突出显示了第 3 行.

{% 包含 "header.html" %}{% 加载 i18n %}{% 从未来加载 %}'future' 不是注册的标签库

所以我简单地删除了load url行,保存profile_form.html,然后尝试构建一个反映代码更改的新容器.

docker-compose 构建docker-compose 开始

这并没有解决问题,我得到了同样的错误.我通过运行进入容器

docker-compose exec -i -t /bin/bash

并检查了 profile_form.html 并确定第 3 行仍然存在.

除非我遗漏了一些完全明显的东西,否则这告诉我我对 docker-compose build 的理解是不正确的.因为我认为 docker-compose build 能够确定是的,'web' 目录中的代码发生了变化",然后重建容器.

解决方案

您无需在每次更改代码时重建用于开发的 docker 映像.这将花费太多时间.但是由于您正在开发 django 并且我假设您使用的是 django 附带的开发服务器(python manage.py runserver),您可以直接将新代码更改发送到您的容器并让dev-server 热加载代码,就像您在本地机器上开发 django 应用程序时所习惯的那样.您需要将一个卷从您的主机映射到您的容器,该容器接收最新的代码并使用它来更新您的应用程序.

由于您没有提供任何代码示例,我只能猜测您在做什么.看一下docker开发者直接提供的例子:https://docs.docker.com/compose/django/

他们的 Dockerfile:

FROM python:2.7环境 PYTHONUNBUFFERED 1运行 mkdir/code工作目录/代码添加requirements.txt/code/运行 pip install -r requirements.txt添加 ./代码/

<代码>添加./code/ 最初将您的本地代码复制到容器中.这是您第一次构建映像时应用程序的初始状态.但是我们不想在每次代码更改时构建映像,因为这需要几分钟时间.您正在使用 docker-compose,您可以将卷映射到您的容器以进行热代码重新加载,正如 docker 开发人员在教程中所见:

版本:'2'服务:D b:图片:postgres网络:建造: .命令:python manage.py runserver 0.0.0.0:8000卷:- .:/code # <-- 此行启用热代码重新加载!端口:- 8000:8000"依赖于取决于:- D b

我标记了代码重新加载的重要行.此行确保本地计算机上的每个更改也反映在容器内.dev-server runserver 识别本地代码更改并重新启动 web 服务器,只需几秒钟.

这是在 docker 容器中使用 django 应用程序的方法.

为什么运行 docker-compose start 后没有变化?

docker-compose start 使用代码中的错误行启动旧图像的旧容器.您需要使用新映像创建新容器.对于这种情况,命令 docker-compose up.来自 docker-compose startdocs:><块引用>

用法:启动[SERVICE...]
启动服务的现有容器.

可以肯定的是,您可以使用 docker-compose rm 删除旧容器.这只会删除您的容器,而不是图像.

So I have a Django project I am deploying with docker-compose. There is a simple error in my profile_form.html template with line 3 highlighted.

{% include "header.html" %}
{% load i18n %}
{% load url from future %}

'future' is not a registered tag library

So I simple delete the load url line, save profile_form.html, and then try to build a new container reflecting the code change.

docker-compose build
docker-compose start

This did not resolve the issue and I get the same error. I went into the container by running

docker-compose exec -i -t <containerid> /bin/bash

and examined the profile_form.html and sure enough line 3 is still there.

Unless I am missing something completely obvious this tells me that my understanding of docker-compose build is incorrect. As I thought docker-compose build would be able to determine "yes there is a code change in the 'web' directory" and then rebuild the container.

解决方案

You do not need to rebuild a docker image, which is used for development, on each change of your code. This would take too much time. But since you are developing django and I assume, that you are using the dev-server shipped with django (python manage.py runserver), you can directly send your new code changes to your container and let the dev-server hot load the code as you are used to when you are developing a django application on your local machine. You need to map a volume from your host to your container, which receives the latest code and uses it to update your application.

Since you did not provide any code samples, I can only guess what you are doing. Take a look at the example directly provided by the docker developers: https://docs.docker.com/compose/django/

Their Dockerfile:

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

ADD . /code/ initially copys your local code to the container. This is the initial state of your application when you build the image for the first time. But we do not want to build the image on each code change, since this takes some minutes. You are using docker-compose and you can map volumes to your container for hot code reloading as it can be seen in the tutorial by the docker devs:

version: '2'
services:
  db:
    image: postgres
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code  # <--  THIS line enables hot code reloading!
    ports:
      - "8000:8000"
    depends_on:
      - db

I marked the important line for code reloading. This line ensures, that each change on your local machine is also reflected inside the container. The dev-server runserver recognizes the local code change and restarts the webserver, which takes only few seconds.

This is the way to go with a django application inside a docker container.

Why are there no changes after running docker-compose start?

docker-compose start starts the old containers of your old image with the wrong line in your code. You need to create new containers with the new image. For this case is the command docker-compose up. From the docs of docker-compose start:

Usage: start [SERVICE...]
Starts existing containers for a service.

To be sure, you can remove your old containers with docker-compose rm. This removes only your containers, not the image.

这篇关于为什么 docker-compose build 没有反映我的 Django 代码更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆