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

如何修改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:

Step 1: Create a dedicated Docker volume

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
Step 2: Update Drupal's file system path

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';
Step 3: Mount the volume to your Drupal container

Update your Docker configuration to link the volume you created to the path you set in Drupal.

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
Step 4: Fix file permissions (Critical!)

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"
Bonus: Migrate existing files

If you have files in the old sites/*/files directories, copy them to the new volume:

  1. Pull files from the container to your local machine:
docker cp <your-drupal-container-name>:/var/www/html/sites/default/files ./temp-drupal-files
  1. 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/
  1. Re-apply permissions (from Step 4) to the migrated files.

内容的提问来源于stack exchange,提问作者Onkeltem

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:38:53