Restart flask app in docker on changes
Asked Answered
S

1

6

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"]
Swec answered 25/10, 2016 at 21:40 Comment(4)
Are you editing the files inside the container or outside?Enthetic
Outside the containerSwec
Would you mind including the rest of your Dockerfile?Enthetic
Just added dockerfileSwec
E
12

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.

Enthetic answered 27/10, 2016 at 4:42 Comment(1)
Thanks, exactly what I needed.Swec

© 2022 - 2024 — McMap. All rights reserved.