Docker Compose配置持久化与镜像合并推送至Docker Hub咨询
Hey there! Let's work through your two key goals one by one: making your Drupal configuration persist across container restarts, and getting your setup pushed to Docker Hub so you can spin it up with a simple docker run command.
一、实现Drupal配置与数据持久化
Right now, your Drupal config, modules, uploaded files, and database data are all stored inside the containers themselves—meaning if you stop or delete the containers, all that work is gone. We’ll fix this using Docker volumes to keep that data safe outside the containers.
1. Update your docker-compose.yml with persistent volumes
Here’s a modified version of your docker-compose.yml that adds named volumes for all critical data paths (I’m assuming you’re using Drupal + MySQL, which is the standard setup):
version: '3.8' services: drupal: image: drupal:latest ports: - "8080:80" volumes: # Persist modules, themes, and profiles - drupal-modules:/var/www/html/modules - drupal-profiles:/var/www/html/profiles - drupal-themes:/var/www/html/themes # Persist site config and user-uploaded files - drupal-sites:/var/www/html/sites db: image: mysql:8.0 environment: MYSQL_DATABASE: drupal MYSQL_USER: drupal MYSQL_PASSWORD: your-db-password MYSQL_ROOT_PASSWORD: your-root-password volumes: # Persist MySQL database data - drupal-db:/var/lib/mysql volumes: drupal-modules: drupal-profiles: drupal-themes: drupal-sites: drupal-db:
Named volumes are Docker-managed, so you don’t have to worry about file permissions or specifying exact host paths—Docker handles storing the data securely on your system.
2. Test the persistence
Restart your stack with the new volumes:
# If you want to clean up old volumes first (optional, only if you want a fresh start) docker-compose down -v # Start the stack with persistent volumes docker-compose up -d
Go through your Drupal setup again, then stop and restart the containers. You’ll see all your config, modules, and data are still there—success!
二、Push your setup to Docker Hub and run with a single command
Since you have two containers (Drupal + MySQL), we’ll need to package your custom Drupal config into a dedicated image, then set things up so you can spin up both containers easily.
1. Package your custom Drupal config into an image
First, copy the configured sites directory (which holds your Drupal settings and files) from your running Drupal container to your local machine:
# Replace <drupal-container-name> with the name of your Drupal container (check with docker ps) docker cp <drupal-container-name>:/var/www/html/sites ./sites
Next, create a Dockerfile in the same directory as the sites folder:
FROM drupal:latest # Copy your custom site config and files into the image COPY ./sites /var/www/html/sites # Set correct file permissions for Drupal RUN chown -R www-data:www-data /var/www/html/sites
This builds a custom Drupal image that includes all your setup work.
2. Build and push the image to Docker Hub
First, log in to your Docker Hub account:
docker login
Build your image with a tag that matches your Docker Hub username and repo name:
docker build -t your-dockerhub-username/your-repo-name:latest .
Push the image to Docker Hub:
docker push your-dockerhub-username/your-repo-name:latest
3. Run everything with a single command (or close to it)
Since Drupal needs a MySQL database to run, we can’t do a single docker run for both, but we can make it super simple with two commands (or wrap them in a script if you want). First, create a dedicated network for the containers to communicate:
docker network create drupal-network
Start the MySQL container (with a persistent volume for the database):
docker run -d \ --name drupal-db \ --network drupal-network \ -e MYSQL_DATABASE=drupal \ -e MYSQL_USER=drupal \ -e MYSQL_PASSWORD=your-db-password \ -e MYSQL_ROOT_PASSWORD=your-root-password \ -v drupal-db:/var/lib/mysql \ mysql:8.0
Then start your custom Drupal image:
docker run -d \ --name drupal \ --network drupal-network \ -p 8080:80 \ -e DRUPAL_DB_HOST=drupal-db \ -e DRUPAL_DB_NAME=drupal \ -e DRUPAL_DB_USER=drupal \ -e DRUPAL_DB_PASSWORD=your-db-password \ your-dockerhub-username/your-repo-name:latest
Now you can access your Drupal site at localhost:8080 just like before, and all data is persistent. If you want to make it even simpler, you can create a small shell script that runs both commands, or share a docker-compose.yml file that uses your custom image—users can then run docker-compose up -d after pulling the image.
内容的提问来源于stack exchange,提问作者Craig




