Docker Compose for MySQL and phpMyAdmin on localhost
Run MySQL and the phpMyAdmin web page on your computer with one docker compose command.
This file starts a MySQL database and the phpMyAdmin web page together. phpMyAdmin is already set to talk to the database, so you can open it in the browser and log in. Set your passwords below and run docker compose up.
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.4
container_name: mysql-db
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: Change-root-456
MYSQL_DATABASE: appdb
MYSQL_USER: app
MYSQL_PASSWORD: Change-me-123
ports:
- "3306:3306"
volumes:
- mysql-data:/var/lib/mysql
phpmyadmin:
image: phpmyadmin
container_name: phpmyadmin
restart: unless-stopped
environment:
PMA_HOST: db
ports:
- "8080:80"
depends_on:
- db
volumes:
mysql-data:How this setup works
Step-by-step setup
- Save the file above as docker-compose.yml.
- Start both: docker compose up -d. The first start takes a minute while MySQL sets itself up.
- Open http://localhost:8080.
- Log in with user app and your user password, or root and the root password.
- To stop: docker compose down. Your data stays in the volume. Add -v to delete it too.
What the important lines mean
- PMA_HOST: db tells phpMyAdmin to connect to the service called db, not to localhost.
- MYSQL_DATABASE, MYSQL_USER and MYSQL_PASSWORD create your database and user on the first start only.
- mysql-data is a volume, so the data survives restarts.
- Your apps on this computer connect to MySQL at 127.0.0.1 port 3306.
Common errors and how to fix them
- Access denied for user
- The user and password must match the values above. The passwords are only set on the first start. If you changed them later, run docker compose down -v to start with a new volume.
- phpMyAdmin: #2002 Connection refused
- MySQL is still starting. Wait a minute and reload. If it persists, read docker compose logs db.
- port is already allocated
- Another program uses that port, often an installed MySQL on 3306. Change the left number of the port line.
- Your app cannot connect to the database
- From your computer use 127.0.0.1 and the port above. From another container in the same file use the host db and port 3306.
Frequently asked questions
- Is it safe to leave this running?
- It is meant for development on your own computer. Do not publish these ports to the internet, and use strong passwords if anyone else can reach your machine.
- How do I import a database in phpMyAdmin?
- Open the database, choose Import and pick a .sql file. Large files may exceed the upload limit, so import them with the mysql command instead.
- Where is my data stored?
- In the Docker volume mysql-data. It stays until you remove the volume.
- How do I point phpMyAdmin at my database?
- Set PMA_HOST to the host name (the db service name inside compose). PMA_HOSTS takes a comma-separated list, PMA_PORT sets a non-default port and PMA_ARBITRARY=1 lets you type any server.
- What are the default import limits?
- UPLOAD_LIMIT is 2048K, MEMORY_LIMIT 512M and MAX_EXECUTION_TIME 600 seconds. UPLOAD_LIMIT changes both upload_max_filesize and post_max_size.
- Which port does the container use?
- 80 inside the container; the docs examples map host 8080 to it. APACHE_PORT changes the container port.
- When should I set PMA_ABSOLUTE_URI?
- Only behind a reverse proxy that changes the address. The image page says not to set it unless needed.
Free tools that help
Fix a problem with this setup
- phpMyAdmin #2002 cannot connectFix phpMyAdmin error #2002, "The server is not responding": start MySQL, use 127.0.0.1 and the right port, or fix the socket and Docker host.
- MySQL access deniedFix error 1045. Default passwords for XAMPP and MAMP, and how to log in on Linux where root uses socket login.
- 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.
- Docker port mappingWhat -p 8080:80 and ports: "8080:80" mean in Docker: the first number is on your computer, the second is inside the container. Which URL to open and how to fix it.
- localhost/phpmyadminphpMyAdmin is the web tool for MySQL and MariaDB. How to open it in XAMPP, WAMP and MAMP, and fix 404, #1045 and #2002 errors.
More container configs
- Docker Compose for PostgreSQL and pgAdminRun PostgreSQL and the pgAdmin web page on your computer with one docker compose command.
- docker-compose for MongoDBRun a local MongoDB database with a password that keeps its data.
- docker-compose for PostgreSQLRun a local PostgreSQL database that keeps its data.
- docker-compose for MySQLRun a local MySQL database in Docker with your own user, password and port, and keep the data between restarts.
- docker-compose for RedisRun Redis in Docker with a password, saved data and a memory limit.
- docker-compose for WordPressRun WordPress and its database locally in Docker with one command and keep your site between restarts.