Docker卷未挂载问题:本地Docker Compose开发文件同步配置求助
Hey there! Let's get your local views and static files syncing with your Docker container properly. Here's a step-by-step breakdown of what to check and fix:
1. Make Sure Your Docker Compose File Has the Correct Volume Mounts
First, you'll need a docker-compose.yml file in your MY_PROJECT root directory that explicitly mounts your local directories to the corresponding paths inside the container. Based on your Dockerfile, your app files live at /go/src/MY_PROJECT (since the default GOPATH in most Go Docker images is /go).
Here's a working example compose config:
version: '3.8' services: your-app-name: build: . volumes: # Mount local views dir to container's views dir - ./views:/go/src/MY_PROJECT/views # Mount local static dir to container's static dir - ./static:/go/src/MY_PROJECT/static # Add other configs like ports, command to run your app, etc. ports: - "8080:8080" command: go run /go/src/MY_PROJECT/main.go
Double-check that the container-side paths match exactly where your app expects to find these files. If your GOPATH is custom, replace /go with your actual GOPATH value.
2. Understand How ADD . $APP_DIR Interacts with Volumes
Your Dockerfile uses ADD . $APP_DIR to copy all local files into the image. When you mount a volume, it overrides the container's directory with your local files—this is exactly what you want for live syncing. Just ensure that the mount path in the compose file matches the directory structure the image was built with.
3. Verify the Mount is Working
After starting your container with docker-compose up -d, run these commands to confirm sync is working:
- List files in the container's views directory to match local files:
docker exec -it <your-container-name> ls /go/src/MY_PROJECT/views - Modify a local HTML/CSS/JS file, then check the container's file content:
docker exec -it <your-container-name> cat /go/src/MY_PROJECT/views/index.html
If the content doesn't update, double-check that your local views and static directories are in the same folder as your docker-compose.yml (the MY_PROJECT root).
4. Troubleshoot Common Issues
- Permission Problems: If the container can't read the mounted files, add a permission fix to your Dockerfile:
For development, you can also run the container as root (not recommended for production) by addingRUN chmod -R 755 $APP_DIR/views $APP_DIR/staticuser: rootto your compose service config. - App Caching: If your Go app caches static files, you might need to restart the app or disable caching during development to see changes live.
内容的提问来源于stack exchange,提问作者Ryo

