开发场景下如何将主机源码同步至Docker容器?
Hey there! Let's work through this source code sync issue you're facing with your Docker4Drupal setup on Ubuntu. Since the sshfs plugin isn't working for you, here are three practical, battle-tested alternatives tailored to your environment:
1. Use Docker Bind Mounts (Native & Straightforward)
Bind mounts are Docker's built-in way to map a host directory directly to a container directory—this is the simplest solution for Ubuntu, given the strong filesystem compatibility between the host and most Drupal containers.
How to update your docker-compose.yml:
Locate the service(s) that need access to your source code (usually php or nginx/apache in Docker4Drupal). Modify their volumes section to replace any existing named volumes with a bind mount pointing to your local source directory:
services: php: volumes: # Replace the existing volume entry with this bind mount - ./path/to/your/local/source:/var/www/html
Critical Permission Fix:
Docker4Drupal containers typically run as the www-data user. To avoid permission errors on your host:
- Check the UID/GID of
www-datainside the container (rundocker exec <php-container-name> id www-data—it's often1000:1000). - Update your local source directory permissions to match:
chown -R 1000:1000 ./path/to/your/local/source
Alternatively, you can add a user directive to the service in your compose file to run the container as your host user:
services: php: user: "${UID}:${GID}"
(You'll need to export your UID/GID in your shell first: export UID=$(id -u) GID=$(id -g) or add this to your .bashrc/.zshrc.)
2. Rsync Auto-Sync (For Bidirectional or Performance-Sensitive Scenarios)
If bind mounts cause performance issues (rare on Ubuntu, but possible with large codebases), rsync-based auto-sync is a great fallback. It watches for file changes on your host and pushes them to the container in real-time.
Setup a Real-Time Sync Script:
- Install
inotify-toolson your Ubuntu host to monitor file changes:
sudo apt update && sudo apt install inotify-tools
- Create a simple bash script (e.g.,
sync-source.sh):
#!/bin/bash SOURCE_DIR="./path/to/your/local/source" CONTAINER_NAME="your-docker4drupal-php-container" TARGET_DIR="/var/www/html" # Monitor for changes and sync while inotifywait -r -e modify,create,delete,move "$SOURCE_DIR"; do rsync -avz --delete "$SOURCE_DIR/" "$CONTAINER_NAME:$TARGET_DIR" done
- Make the script executable and run it:
chmod +x sync-source.sh ./sync-source.sh
You can also run this script in the background or set it up as a systemd service for persistence.
3. Mutagen (High-Performance Bidirectional Sync)
Mutagen is a tool designed specifically to solve file sync challenges between hosts and containers, offering better performance than bind mounts for certain workloads (like large Drupal codebases with many files).
Setup Steps:
- Install Mutagen on your Ubuntu host:
curl -L https://github.com/mutagen-io/mutagen/releases/latest/download/mutagen_linux_amd64.tar.gz | tar -xz sudo mv mutagen /usr/local/bin/
- Pause any existing volume mounts for your source code in
docker-compose.yml(comment out the relevant lines). - Create a Mutagen sync session:
mutagen create \ ./path/to/your/local/source \ docker://<php-container-name>/var/www/html \ --sync-mode two-way-resolved
- Start the sync agent and monitor the session:
mutagen start mutagen monitor
Mutagen will handle bidirectional sync, conflict resolution, and performance optimizations automatically.
内容的提问来源于stack exchange,提问作者Victor Shelepen

