
Editorial illustration generated for this article.
Installing PHP, Apache, Composer, and MySQL directly on your laptop works fine for your very first project—and then becomes a headache the moment you maintain two sites on different PHP versions. One legacy client site wants PHP 8.1 and MySQL 5.7, a newer build wants PHP 8.3 and MySQL 8.0, and a routine macOS or Linux package upgrade quietly breaks your local Apache config.
Packaging Drupal and MySQL inside Docker Compose solves that cleanly. Each project gets its own isolated runtime, database, and virtual network without polluting your host machine. Below is a practical, transparent setup that gives you a working local Drupal 10 site backed by MySQL 8.0, persistent named volumes so your data survives container restarts, and a clear mental model of how container-to-container networking works. Once it is running, you can pair it with a disciplined Drupal configuration management deployment workflow and Drupal cache troubleshooting steps.
Before you start: plain Compose vs. DDEV or Lando
Make sure Docker Desktop (macOS or Windows) or Docker Engine with the Compose v2 plugin (Linux) is installed and running:
docker --version
docker compose version
It is worth being honest about when a plain docker-compose.yml is the right tool and when it is not. A minimal two-service Compose file is ideal when you want to understand how Drupal and MySQL talk to each other, spin up a clean sandbox to test core features like building listings with the Drupal Views module, or evaluate a contrib module without installing extra wrapper CLIs.
On the other hand, if you are building a production client project with Composer-managed modules, running drush commands all day, and collaborating across a team, a dedicated wrapper like DDEV or a custom Dockerfile that bind-mounts your host Composer directory into the container is usually a better long-term fit. Keeping /var/www/html inside a Docker named volume is great for quick sandboxes, whereas bind-mounting your Git repository is better when you want to edit code in VS Code and commit changes directly.
Creating the project workspace and Compose file
Create a clean directory for the environment and make sure port 8080 is free on your laptop (if another local service already uses 8080, change "8080:80" to "8088:80" below):
mkdir drupal-docker-local
cd drupal-docker-local
Inside drupal-docker-local, create docker-compose.yml with the following configuration:
services:
database:
image: mysql:8.0
container_name: drupal_mysql
restart: unless-stopped
environment:
MYSQL_DATABASE: drupal_db
MYSQL_USER: drupal_user
MYSQL_PASSWORD: drupal_password
MYSQL_ROOT_PASSWORD: root_secret_password
volumes:
- db_data:/var/lib/mysql
networks:
- drupal_network
drupal:
image: drupal:10-apache
container_name: drupal_web
restart: unless-stopped
depends_on:
- database
ports:
- "127.0.0.1:8080:80"
volumes:
- drupal_data:/var/www/html
networks:
- drupal_network
volumes:
drupal_data:
db_data:
networks:
drupal_network:
driver: bridge
A few details here are intentional:
- Binding to
127.0.0.1:8080:80: Publishing the port on127.0.0.1instead of all interfaces keeps your local Drupal sandbox reachable only from your own laptop—a good habit if you ever boot containers while working on cafe or hotel Wi-Fi. - Named volumes (
db_dataanddrupal_data): Containers are meant to be disposable, so storing MySQL tables and Drupal’ssites/default/filesandsettings.phpin named volumes ensures adocker compose downdoes not wipe your work. - Custom bridge network (
drupal_network): Putting both containers on a user-defined bridge network enables Docker’s embedded DNS resolver, which is how the Drupal container finds MySQL by name.
Keep these simple passwords strictly for local sandboxes. Never reuse them on staging or production, and never commit real credentials to Git.
Booting the containers and waiting on MySQL
Start both services in the background and check their status:
docker compose up -d
docker compose ps
Both drupal_mysql and drupal_web should report as running. However, depends_on only waits for the database container process to start—it does not wait for MySQL to finish initializing its system tables on the very first boot. If you rush straight to the browser in the first five seconds, the installer may fail to connect. Watch the database logs for a moment instead:
docker compose logs -f database
Wait until you see mysqld: ready for connections on port 3306, then press Ctrl + C to detach from the log stream (the container keeps running in the background). To keep terminal workflows consistent across machines, you can also check our notes on writing reproducible terminal commands.
Why localhost fails inside Docker
The number-one tripwire when installing Drupal in Docker happens on the database configuration screen. On a traditional MAMP, XAMPP, or single-VPS setup, PHP and MySQL share the same host, so entering localhost or 127.0.0.1 works.
Inside Docker, each container has its own isolated network namespace. If you tell the drupal container to connect to localhost, PHP looks for a MySQL socket inside drupal_web itself—where MySQL does not exist—and throws SQLSTATE[HY000] [2002] Connection refused or No such file or directory.
Because both containers sit on drupal_network, Docker’s internal DNS resolves each container by its Compose service name. Inside the Drupal installer, use:
Host: database
Port: 3306
Remember the split: container-to-container traffic uses the internal service name (database) and internal port (3306), while your browser on the host laptop uses the published port (http://localhost:8080).
Completing the web installer
Open http://localhost:8080 in your browser and walk through the setup wizard:
- Pick your language and click Save and continue.
- Choose Standard (or Demo: Umami if you want pre-built content types and sample media to test with).
- On Database configuration, select MySQL, MariaDB, Percona Server, or equivalent.
- Enter the database details matching
docker-compose.yml:- Database name:
drupal_db - Database username:
drupal_user - Database password:
drupal_password
- Database name:
- Expand Advanced Options and change the host from
localhosttodatabase, keeping the port at3306. - Click Save and continue and let Drupal install core modules, then configure your site name and admin account.
Daily commands and wiping the sandbox cleanly
Once installed, these are the commands you will actually use day to day:
# Pause containers at the end of the day while preserving state
docker compose stop
# Resume existing containers
docker compose start
# Tear down and recreate containers (keeps named volumes intact)
docker compose down
# Tail Apache and PHP error logs
docker compose logs -f drupal
# Open an interactive shell inside the Drupal container
docker compose exec drupal bash
If an experiment goes sideways and you want to erase everything—including the database and Drupal files—add -v to remove the named volumes:
docker compose down -v
Only pass -v when you genuinely want a blank slate; there is no undo for deleted volumes.
Pitfalls that catch people out
Beyond leaving the host as localhost, two habits cause most wasted time. First, running docker compose down -v out of muscle memory when you only meant docker compose down will wipe every node, user, and config change you made. Second, making manual clicks in the admin UI without exporting configuration (drush cex once you graduate to a Composer-based workflow) makes it painful to reproduce your setup on staging later.
Starting with a clean two-container docker-compose.yml demystifies what heavier tools hide under the hood. Once you see why database:3306 works inside the bridge network while localhost:8080 works in your browser, debugging local PHP and MySQL containers becomes straightforward—and you can move on to a bind-mounted Composer project whenever you are ready to track custom modules in Git.