Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart flask app in docker on changes

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"]
like image 945
user1686342 Avatar asked Feb 05 '23 20:02

user1686342


1 Answers

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.

like image 153
Roman Avatar answered Feb 08 '23 11:02

Roman