Running MySQL and WordPress with Docker on Ubuntu 24.04.4 LTS is relatively straightforward. Once Docker is installed, the main work is creating the database container, preparing a WordPress database, and connecting WordPress to MySQL with the correct host address. There are also a couple of details that can easily cause trouble during the process.
Install Docker first
Install Docker on Ubuntu and make sure it is working before continuing. The Docker installation itself is not covered here; once the installation has been verified, proceed with the container setup below.
Optionally install DPanel
DPanel is not required, but it provides a convenient graphical interface for managing Docker containers and Compose projects. For a local installation, the Lite edition is generally sufficient.
docker run -d --name dpanel --restart=always \
-p 8807:8080 -e APP_NAME=dpanel \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /home/dpanel:/dpanel dpanel/dpanel:lite
After the container starts, DPanel can be used to create and manage the WordPress Compose task instead of handling everything directly from the terminal.
Create the MySQL container
Run the following command to create and start a MySQL container:
# 创建并启动MySQL容器
docker run -d \
--name mysql-bing \
--restart always \
-e MYSQL_ROOT_PASSWORD=bing \
-v mysql_data:/var/lib/mysql \
-p 3306:3306 \
mysql:latest
The value assigned to MYSQL_ROOT_PASSWORD is the MySQL root password. Replace bing with the password you want to use. The named volume mysql_data stores the database files outside the container, so the data remains available when the container is recreated.
Prepare the WordPress database
WordPress needs a separate database before its initial setup can be completed. DBeaver is a convenient choice for this step and can be installed directly from Ubuntu’s application center.
Open DBeaver, connect to the MySQL server created above, and create a database for WordPress. The database name, username, and password must match the values that will later be entered in the Compose configuration.
Deploy WordPress with DPanel Compose
In DPanel, open the Compose section and create a new task. Paste the following YAML into the configuration file:
version: '3.8'
services:
wordpress:
image: wordpress:latest
container_name: wordpress
restart: unless-stopped
ports:
- "8080:80" # 访问 http://localhost:8080
environment:
WORDPRESS_DB_HOST: 192.168.0.88:3306 # 连接宿主机的MySQL
WORDPRESS_DB_USER: wp # 替换为你的MySQL用户名
WORDPRESS_DB_PASSWORD: 321 # 替换为你的MySQL密码
WORDPRESS_DB_NAME: wp # 数据库名(需提前创建)
volumes:
- wordpress_data:/var/www/html
volumes:
wordpress_data:
Before starting the task, replace the database connection settings with your own values:
WORDPRESS_DB_HOST: the LAN IP address of the Ubuntu host and port, such as192.168.0.88:3306WORDPRESS_DB_USER: the MySQL userWORDPRESS_DB_PASSWORD: the corresponding MySQL passwordWORDPRESS_DB_NAME: the WordPress database created earlier
The host value deserves particular attention. In this setup, localhost and 127.0.0.1 did not work as expected, while using the machine’s LAN IP address did. Therefore, replace 192.168.0.88 with the actual local network address of the Ubuntu host rather than leaving the example unchanged.
Confirm the Compose task and start the container. After Docker downloads the WordPress image and prepares the files, open http://localhost:8080/ in a browser to begin the first-time WordPress installation.
For this kind of local deployment, Docker avoids much of the manual environment setup and compatibility troubleshooting. Once the database and connection parameters are correct, the basic MySQL and WordPress stack can be brought up with only a few commands.