python - 在 Docker 中根据更改重新启动 Flask 应用程序

标签 python docker flask flask-script

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

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

在 docker 中我有以下内容:

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

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

docker 文件:

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说明

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

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


如果你想在容器内改变文件,你可以mount a host directory as a volume .也来自文档

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 运行命令可能看起来像这样

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

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


您可能也对这篇文章感兴趣Dockerize a Flask, Celery, and Redis Application with Docker Compose .它更进一步,使用 Docker Compose 编排 Flask 开发环境。

关于python - 在 Docker 中根据更改重新启动 Flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40250199/

相关文章:

python - 测试 Flask 响应是否为 JSON

python - 在 Python 中编写此正则表达式的更好方法

ruby-on-rails - 无法打开与本地主机 :3101 - Rails/Docker 的 TCP 连接

docker - 从 DockerHub 镜像安装 Helm - DockerHub 镜像下载失败

python - git-flask-python : Is it safe to remove pycache and flask session folders

python - Flask-Social Twitter 登录失败并显示消息 - OAuthException : Failed to generate request token

python - 在Python中将元组元素切片并堆叠到矩阵中

Python:如何创建一个包含图像中每个像素信息的数组?

python - 如何在 PyObjC 中正确构建主菜单?

docker - Dockerfile 中的 RUN 命令不会在容器之间持续存在