1

I use volume between host and container for a flask application. The web part of my docker-compose looks like this:

  web:
    restart: on-failure
    container_name: web
    build:
      context: "."
      dockerfile: "docker/Dockerfile.web"
    ports:
      - 5000:5000
    volumes:
      - database:/var/run/postgresql
      - .:/usr/src/app
volumes:
  database:

Here is my Dockerfile:

FROM python

WORKDIR /usr/src/app

COPY . .

RUN pip install --no-cache-dir -r requirements.txt

CMD ["python", "./sources/app.py"]

I think my volumes are well created because when I make and update in app.py from the host, the app.py from the container is also updated. But the problem is when I reload the page from my browser the content has not been updated.

When I do docker volume list I got this:

DRIVER              VOLUME NAME
local               b26d14737c65ba4d26c3751ae898883e9062c4894a5fd31b191ffd9ded7f1d50
local               web-app_database

What is the volume b26d14 ? I don't think it's one a created with docker-compose. After running docker volume inspect it returns:

[
    {
        "CreatedAt": "2020-03-21T10:41:19Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/b26d14737c65ba4d26c3751ae898883e9062c4894a5fd31b191ffd9ded7f1d50/_data",
        "Name": "b26d14737c65ba4d26c3751ae898883e9062c4894a5fd31b191ffd9ded7f1d50",
        "Options": null,
        "Scope": "local"
    }
]

The only way I have to make it works it is to restart the container.

I found this article: link but it did not fix my issue.

Docker versions:
- Docker version 19.03.8, build afacb8b
- docker-compose version 1.25.4, build 8d51620a

Any ideas ?

Thanks for replies and have a nice day.

Baptiste
  • 115
  • 1
  • 11
  • When your application code changes, are you restarting your container? – David Maze Mar 21 '20 at 12:51
  • When I make a code change in host app.py I got the same change in container app.py. Without restarting anything. But when I reload the page from my browser, I don't see any change. – Baptiste Mar 21 '20 at 13:48
  • 3
    Sounds like a probelm with your app responding to the new content, rather than a docker problem. – Software Engineer Mar 21 '20 at 15:01
  • Maybe. I tried to run the app without cache. Flask has an option to disable it. But I still have the problem. Have you seen the article I linked ? – Baptiste Mar 21 '20 at 15:34
  • Can you provide a reproducible example. – BMitch Mar 21 '20 at 16:31
  • Can you post you Dockerfile, so that we can see how do you run your app? – Stefan Golubović Mar 21 '20 at 16:40
  • I edited the post with my Dockerfile – Baptiste Mar 21 '20 at 17:19
  • How do you refresh your browser? F5 or CTRL + SHIFT + R or clicking in your address bar, removing the fragment and hit ENTER? – qräbnö Mar 21 '20 at 17:23
  • I do command + R. – Baptiste Mar 21 '20 at 17:54
  • 2
    Try setting `FLASK_ENV` in Dockerfile with `ENV FLASK_ENV=development`. That will enable app reloading when something is changed. – Stefan Golubović Mar 21 '20 at 18:17
  • This worked ! So I know it's a Flask problem now. Why does Flask need to reload the application to display the changes ? – Baptiste Mar 21 '20 at 18:21
  • Even in local development, you need to restart the app to see changes. That's why most of the frameworks support some sort of [live or hot reloading](https://stackoverflow.com/a/41429055/4778343) to speed up development. In your case, flask provides this feature when you [set FLASK_ENV](https://flask.palletsprojects.com/en/1.1.x/quickstart/#debug-mode) environment variable. – Stefan Golubović Mar 21 '20 at 18:26

1 Answers1

1

Through comments, we concluded that the issue was how Flask handles live/hot reloading and has nothing to do with Docker.

Flask provides FLASK_ENV environment variable that can be used to enable development features such as debugger and automatic reloader. To enable development features you need to set its value to development.

Docker also provides a neat way to set environment variables with ENV keyword. All you have to do is to add the following line in your Dockerfile:

ENV FLASK_ENV=development
Stefan Golubović
  • 1,225
  • 1
  • 16
  • 21