docker-compose for WordPress: run a local site with MySQL
Run WordPress and its database locally in Docker with one command and keep your site between restarts.
This docker-compose file starts WordPress and a database together. Your site files and posts are kept in volumes, so you can stop and start it whenever you like. Choose your port and passwords below.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
services:
db:
image: mysql:8.0
restart: unless-stopped
environment:
MYSQL_DATABASE: wordpress
MYSQL_USER: wpuser
MYSQL_PASSWORD: change-me-wp
MYSQL_ROOT_PASSWORD: change-me-root
volumes:
- dbdata:/var/lib/mysql
wordpress:
image: wordpress:latest
restart: unless-stopped
depends_on:
- db
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: wpuser
WORDPRESS_DB_PASSWORD: change-me-wp
volumes:
- wpdata:/var/www/html
volumes:
dbdata:
wpdata:How this setup works
Step-by-step setup
- Save the config as docker-compose.yml in an empty folder.
- Start both containers: docker compose up -d.
- Open http://localhost:8080 and run the WordPress installer. Wait a minute on the first start.
- Log in at http://localhost:8080/wp-admin with the account you created.
- Stop it with docker compose stop. Start it again with docker compose start.
What the important lines mean
- WORDPRESS_DB_HOST: db:3306 points WordPress at the database container by its service name.
- wpdata keeps your themes, plugins and uploads. dbdata keeps your posts and settings.
- The passwords are only used the first time the database starts.
Common errors and how to fix them
- Error establishing a database connection
- The database is still starting, or the passwords do not match the ones the volume was created with. Wait a minute, then check docker compose logs db. To start fresh, run docker compose down -v. This deletes the site.
- Port 8080 is already allocated
- Another program uses it. Pick another port above, such as 8081.
- The site redirects to the wrong address after I change the port
- WordPress stores its address. Set WP_HOME and WP_SITEURL in wp-config.php, or reset the volumes.
- Uploads fail: the file is too large
- The default PHP limit is small. Add a php.ini file with larger upload_max_filesize and post_max_size values and mount it into the container.
Frequently asked questions
- How do I open WordPress running in Docker?
- Open http://localhost followed by the port you chose, for example http://localhost:8080.
- Where are my files stored?
- In Docker volumes named wpdata and dbdata. Run docker volume ls to see them.
- How do I delete the site completely?
- Run docker compose down -v. This removes the containers and the volumes, including all posts and uploads.
Free tools that help
- Password generatorCreate a random password with the length and characters you choose. It runs in your browser, uses secure randomness and shows how strong it is.
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
Fix a problem with this setup
- Docker port already allocatedFix "port is already allocated" in Docker and Docker Compose: find the container or program that holds the port and free it.
- Localhost redirected too many timesFix ERR_TOO_MANY_REDIRECTS on localhost: clear cookies, fix HTTP and HTTPS rules, and WordPress URL settings.
- MySQL access deniedFix error 1045. Default passwords for XAMPP and MAMP, and how to log in on Linux where root uses socket login.
More container configs
- Dockerfile for Node.jsA small, working Dockerfile for a Node.js app, with build step, non-root user and health check options.
- Dockerfile for PythonA working Dockerfile for a Python web app, with Gunicorn, Uvicorn or a plain script.
- Docker Compose for MailpitRun a fake mail server on your computer, so test emails from your app show up in a web page instead of being sent.
- Kubernetes Deployment and ServiceA Deployment and Service YAML for your container image, with replicas, port, resource limits and health checks.
- Docker Compose for MySQL and phpMyAdminRun MySQL and the phpMyAdmin web page on your computer with one docker compose command.
- Docker Compose for PostgreSQL and pgAdminRun PostgreSQL and the pgAdmin web page on your computer with one docker compose command.