Docker Compose容器与主机文件无法同步问题排查与解决
Hey there! Let's walk through what was causing your users.json sync issue and how you resolved it, plus some extra tips to avoid this headache in the future.
Core Issues Identified
You hit two key misconfigurations/operational mistakes that led to the file sync failure:
- Conflicting Image Definitions
YourDockerfilebuilds an image frompython:3, but yourdocker-compose.ymlexplicitly setimage: webapp-flask. This caused Docker Compose to prioritize any existingwebapp-flaskimage over rebuilding from your updated Dockerfile—so changes to your app or setup weren't being applied. - Stale Cache & Unrebuilt Containers
By default,docker-compose upreuses existing containers and cached images. Even if you modifiedapp.pyor adjusted your mount settings, Docker would stick with the old container state, which meant yourusers.jsonmount wasn't working as expected because the container was still running off outdated configuration.
Step-by-Step Fix (As You Discovered)
Here's the breakdown of what fixed it, with extra context:
- Clean Up Old Resources
First, stop and remove all existing containers and associated volumes to wipe any stale state:
Then delete the conflictingdocker-compose down -vwebapp-flaskimage to ensure Docker builds fresh:docker rmi webapp-flask - Remove Redundant Image Configuration
In yourdocker-compose.yml, delete theimage: webapp-flaskline under theflaskservice. This tells Docker Compose to automatically use the image built from your Dockerfile, eliminating the conflict. - Force a Fresh Build & Start
Use the--buildflag to ensure Docker rebuilds the image from scratch and spins up new containers:docker-compose up --build
Extra Tips for Docker Toolbox on Windows
Since you're using Docker Toolbox (which relies on VirtualBox), keep these in mind to avoid future mount issues:
- Verify Shared Directories
Docker Toolbox maps your Windows files to a VirtualBox shared folder (usuallyC:\Usersis shared by default). Make sure your project directory is inside this shared path—if it's outside, the mount won't work properly. - File Permissions Adjustment
Windows and Linux containers handle file permissions differently. If you run into write issues again, add a permission fix to your Dockerfile:
This ensures the container has write access to the mounted file.RUN chmod 666 /app/users.json
内容的提问来源于stack exchange,提问作者Contrevien
相关产品推荐
相关产品推荐

