更改后在Docker中重新启动Flask应用 [英] Restart flask app in docker on changes

查看:130
本文介绍了更改后在Docker中重新启动Flask应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用flask-script运行我的应用程序:

I am using flask-script to run my app:

if __name__ == "__main__":
    manager.run()

在docker中,我有以下内容:

In docker I have the following:

CMD [ "python", "manage.py", "runserver", "-h", "0.0.0.0", "-p", "5000"]

现在,当我构建并运行容器时,应用程序运行良好。但是,即使我的环境设置了DEBUG = True变量,如果我更改代码并保存应用程序也不会重新启动。我在这里缺少什么吗?

Now when I build and run my container the app runs fine. However, if I make changes to my code and save the app does not restart despite my env having a DEBUG=True variable set. Am I missing something here?

Dockerfile:

Dockerfile:

FROM python:3.4-slim

RUN apt-get update -y && \
    apt-get install -y \
        python-pip \
        python-dev \
        pkg-config \
        libpq-dev \
        libfreetype6-dev

COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip3 install -r requirements.txt

COPY . /app

CMD [ "python", "manage.py", "runserver"]


推荐答案

COPY 指令


从< src>复制新文件或目录。并将它们添加到路径< dest>处的容器的文件系统中。

copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

这意味着映像具有文件的快照就像建立图片时一样。当从该映像启动容器时,它将在其文件系统中看到文件的副本。修改原始文件不会对容器内的副本产生任何影响,应用程序不会看到这些更改并且不会重新启动。

That means the image has a snapshot of the files as they were when the image was built. When you start a container from that image, it will see the copy of your files in its file system. Modifying the original files will not have any effect on the copies inside the container, the app doesn't see those changes and doesn't restart.

如果要在容器内更改文件,可以将主机目录安装为卷。同样从文档中

If you want the files to change inside the container, you can mount a host directory as a volume. Also from the docs


此命令将主机目录/ src / webapp挂载到/ webapp的容器中。如果路径/ webapp已存在于容器的映像中,则/ src / webapp安装会覆盖,但不会删除先前存在的内容。一旦挂载被删除,内容就可以再次访问。

This command mounts the host directory, /src/webapp, into the container at /webapp. If the path /webapp already exists inside the container’s image, the /src/webapp mount overlays but does not remove the pre-existing content. Once the mount is removed, the content is accessible again.

Docker run命令可能看起来像这样

The Docker run command will probably look something like this

docker run -d -v /absolute/path/to/src:/app <image-name>

然后,文件更改应反映在容器内的文件中(因为它们将是相同的文件),一切都应按预期重新启动。

Then your file changes should be reflected in the files inside the container (because they will be the same files) and everything should restart as expected.

您可能对此帖子也有兴趣使用Docker Compose对Flask,Celery和Redis应用程序进行Docker 。它向前迈进了一步,并使用Docker Compose来协调Flask开发环境。

You may also be interested in this post Dockerize a Flask, Celery, and Redis Application with Docker Compose. It takes it one step further and uses Docker Compose to orchestrate a Flask development environment.

这篇关于更改后在Docker中重新启动Flask应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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