如何修改Drupal 8中公共文件的存储路径?
Got it, let's walk through how to reconfigure your Drupal (I’m assuming this is Drupal given the familiar sites/*/files path structure) file system to use a separate Docker volume instead of the default directories. Here’s a step-by-step breakdown that works for both single and multi-site setups:
First, make a persistent volume to store your files—this keeps your data safe even if you rebuild or restart containers:
docker volume create drupal-shared-files
You need to tell Drupal to use the new path inside the container. Pick one of these options based on your setup:
Option A: Admin Interface (Single Site, Quick & Simple)
- Log into your Drupal backend, navigate to Configuration > Media > File system (path varies slightly by Drupal version, but this is standard for 9/10).
- In the Public file system path field, enter a container-internal path like
/var/www/html/shared-files(this is where we’ll mount the volume later). - Save the configuration—Drupal will try to create the directory automatically (we’ll fix permissions next if it fails).
Option B: Edit settings.php (Multi-Site or Automation)
For multi-site setups or if you want to automate this, edit the settings.php file for each site:
- For single site:
sites/default/settings.php - For multi-site:
sites/<your-site-name>/settings.php
Add or modify these lines:
// Set public file system path to the container-internal directory $settings['file_public_path'] = '/var/www/html/shared-files'; // Optional: If using private files, set that path too // $settings['file_private_path'] = '/var/www/html/private-shared-files';
Update your Docker configuration to link the volume you created to the path you set in Drupal.
Using docker-compose.yml (Recommended)
Add the volume mount to your Drupal service block:
services: drupal: image: drupal:latest volumes: - drupal-shared-files:/var/www/html/shared-files # Keep any existing volume mounts for modules/themes/settings here # - ./modules:/var/www/html/modules ports: - "8080:80" volumes: drupal-shared-files: external: true # Omit this if you want Docker to create the volume automatically
Using docker run command
If you’re using direct docker run commands:
docker run -d -p 8080:80 \ -v drupal-shared-files:/var/www/html/shared-files \ drupal:latest
Drupal needs the web server user (usually www-data for Debian/Ubuntu-based images) to have write access to the mounted volume. Do one of these:
- Quick fix via container exec:
docker exec -it <your-drupal-container-name> chown -R www-data:www-data /var/www/html/shared-files
- Automate with docker-compose:
Add a command to ensure permissions are set on startup:
services: drupal: # ... existing config ... command: > sh -c "chown -R www-data:www-data /var/www/html/shared-files && apache2-foreground"
If you have files in the old sites/*/files directories, copy them to the new volume:
- Pull files from the container to your local machine:
docker cp <your-drupal-container-name>:/var/www/html/sites/default/files ./temp-drupal-files
- Push the files to the Docker volume:
docker run --rm -v drupal-shared-files:/target -v ./temp-drupal-files:/source busybox cp -r /source/* /target/
- Re-apply permissions (from Step 4) to the migrated files.
内容的提问来源于stack exchange,提问作者Onkeltem

