You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发场景下如何将主机源码同步至Docker容器?

Reliable Source Code Sync Solutions for Docker4Drupal on Ubuntu (Alternative to sshfs)

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:

  1. Check the UID/GID of www-data inside the container (run docker exec <php-container-name> id www-data—it's often 1000:1000).
  2. 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:

  1. Install inotify-tools on your Ubuntu host to monitor file changes:
sudo apt update && sudo apt install inotify-tools
  1. 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
  1. 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:

  1. 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/
  1. Pause any existing volume mounts for your source code in docker-compose.yml (comment out the relevant lines).
  2. Create a Mutagen sync session:
mutagen create \
    ./path/to/your/local/source \
    docker://<php-container-name>/var/www/html \
    --sync-mode two-way-resolved
  1. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:03:49