I have a Dockerfile which I am using to create a container for a Flask application. It is in a subdirectory, since I have multiple Dockerfiles and docker-compose files and I didn't like them cluttering up the root of the project.
When developing locally, I want use a Docker bind mount to mount the local directory into the container, overlaying an existing directory. This will allow Flask to notice file changes and reload the development server without me needing to restart the Docker containers.
I am running the container with this command:
docker-compose -p myproject -f docker/docker-compose.dev.yml up
The bind mount was working with this setup:
docker/Dockerfile.dev
# setup...
WORKDIR /app
COPY . /app # To allow the container to start successfully, before the bind mount is mounted
docker/docker-compose.dev.yml
services:
api:
build:
context: ..
dockerfile: ./docker/Dockerfile.dev
volumes:
- ..:/app
// And a bunch of other setup stuff and services
I followed this helpful tutorial to help slim down my container, and adopt Pipenv. It recommended a slightly different setup, where my container creates a user instead of running everything as root. So I now have this which is failing:
docker/Dockerfile.dev
# setup...
RUN useradd --create-home appuser
WORKDIR /home/appuser
USER appuser
COPY . .
docker/docker-compose.dev.yml
services:
api:
build:
context: ..
dockerfile: ./docker/Dockerfile.dev
volumes:
- ..:/home/appuser
// And a bunch of other setup stuff and services
While the app launches successfully because I've copied everything into place with the COPY
command, the server doesn't reload when I change files in the host directory. When I open a shell using this command:
docker run --env-file .env --entrypoint bash --network flasknetwork -v $(PWD):/home/appuser -ti api
Then of course the bind mount appears as expected, shadowing appuser
's home directory in the container.
I don't understand what's different between these two setups. How can I get the bind mount working again when the container is launched using docker-compose
?
CodePudding user response:
So the problem wasn't the bind mount after all. I used docker inspect api
and discovered that the mount was working as expected:
"Mounts": [
{
"Type": "bind",
"Source": "/Users/myuser/src/project",
"Destination": "/home/appuser",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
So I looked up Flask autoreloading, and remembered that it only works when FLASK_ENV
is set to development
. This setting had gotten removed as I was moving things around, and I hadn't added it back. So the app was running in production mode, and thus was not autoreloading.