Search code examples
pythondockerflaskdocker-composedocker-volume

Docker volume updated on container after changes, but nothing happen in host


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.


Solution

  • 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