Restart flask app in docker on changes

前提是你 提交于 2019-12-05 07:22:43

问题


I am using flask-script to run my app:

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

In docker I have the following:

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

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:

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"]

回答1:


The COPY instruction

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

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.

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.


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.



来源:https://stackoverflow.com/questions/40250199/restart-flask-app-in-docker-on-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!