Docker部署Flask应用:代码修改后无法自动同步问题求助
Hey there! I totally get this frustration—when you're just starting out with Docker, it's easy to hit this "code changes not updating" wall. Let's get your Flask Hello World app syncing automatically in no time.
Why Your Changes Aren't Taking Effect
By default, when you build a Docker image, it copies your code into the image at build time. After that, any changes you make to your local app.py don't get reflected in the running container because the container is using the copied version from the image. To fix this, we need to use Docker volumes to mount your local code directory directly into the container, plus enable Flask's auto-reload feature.
Step 1: Update Your Dockerfile
First, make sure your Dockerfile is set up to support development mode. Here's a solid example:
# Use an official Python runtime as the base image FROM python:3.9-slim # Set the working directory inside the container WORKDIR /app # Copy only requirements first to leverage Docker cache COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Set Flask environment variables for auto-reload ENV FLASK_APP=app.py ENV FLASK_ENV=development # Expose the port Flask runs on EXPOSE 5000 # Start the Flask app CMD ["flask", "run", "--host=0.0.0.0"]
Note: We don't need to copy the entire codebase here anymore (though you can if you want)—the volume we'll add next will override this with your local files.
Step 2: Modify docker-compose.yml to Add a Volume
This is the key part. Add a volumes entry to your service to mount your local directory into the container. Here's how your docker-compose.yml should look:
version: '3.8' services: web: build: . ports: - "5000:5000" # Mount local current directory to /app in the container volumes: - ./:/app # Ensure development mode is enabled (redundant with Dockerfile, but safe to include) environment: - FLASK_ENV=development
The ./:/app line tells Docker to sync everything in your local project folder with the /app directory inside the container. Any changes you make locally will instantly show up in the container.
Step 3: Restart Your Containers
First, stop any running containers to clear the old setup:
docker-compose down
Then start everything up again (the --build flag ensures we rebuild the image with our updated Dockerfile):
docker-compose up --build
Now Test It!
Edit your app.py (change the Hello World message, for example) and save the file. You'll see output in your terminal from Flask saying it detected a file change and reloading the app. Refresh your browser, and you'll see your updated code—no need to rebuild the image or restart containers manually!
Common Pitfalls to Watch For
- Flask Development Mode: If
FLASK_ENVisn't set todevelopment, Flask won't auto-reload. Double-check this in both your Dockerfile and docker-compose.yml. - Docker File Sharing (Windows/Mac): If you're on Windows or Mac, make sure your project directory is in Docker's allowed file sharing list. You can check this in Docker Desktop > Settings > Resources > File Sharing.
- Cache Issues: If you made changes to your Dockerfile before adding the volume, using
docker-compose up --buildensures you're using the latest image configuration.
内容的提问来源于stack exchange,提问作者Peter Cui

