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

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

问题描述

因此,我有一个正在使用docker-compose进行部署的Django项目。我的profile_form.html模板中有一个简单的错误,突出显示了第3行。

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

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

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

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

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

除非我遗漏了一些完全显而易见的内容,否则这表明我对docker-compose构建的理解不正确。正如我认为的docker-compose build能够确定是的,'web'目录中存在代码更改,然后重新构建容器。

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.

推荐答案

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

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.

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

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/

其Dockerfile:

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 / 首先将您的本地代码复制到容器中。首次构建映像时,这是应用程序的 initial 状态。但是我们不希望在每次代码更改时都建立映像,因为这需要几分钟。您正在使用docker-compose,并且可以将卷映射到容器以进行热代码重载,如docker devs在教程中所见:

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

我标记了代码重新加载的重要行。此行确保本地计算机上的每个更改也都反映在容器内。开发服务器 runserver 识别本地代码更改并重新启动Web服务器,仅需几秒钟。

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.

此操作是在Docker容器中使用Django应用程序的方式。

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

docker-compose start 使用代码中的错误行启动旧映像的旧容器。您需要使用新映像创建新容器。对于这种情况,命令是 docker-compose up 。从 docker-compose start 文档 $ c>:

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:


用法:开始[SERVICE ...]

开始现有服务容器。

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

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

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

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