The example from the official WordPress Docker image page (actual for the end of 2024):
services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- wordpress:/var/www/html
db:
image: mysql:8.0
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
volumes:
wordpress:
db:
My preset is close to this one:
services:
WordPress:
container_name: Example-WordPressHomePage-WordPress
image: wordpress:6.7.1-php8.1-apache
ports:
- 80:80
volumes:
- ./wordpress:/var/www/html
restart: always
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
Database:
container_name: Example-WordPressHomePage-Database
image: mysql:8.4.3
volumes:
- ./database:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
expose:
- 3306
- 33060
Currently, the WordPress cannot connect with the database:
However, the direct connection via IntelliJ IDEA tools does not work too:
I suppose, the above preset is not enough for mysql:8.4.3 image. What else must be done?
In Compose, you use the service names as hostnames. You've renamed db
to Database
, so you also have to use WORDPRESS_DB_HOST=Database
.
I'll recommend that you use lowercase names as Linux is case sensitive and you'll get less errors if you just use lowercase all the time.
The reason you can't connect from IntelliJ is that you don't map a host port to the MySQL container. expose
doesn't do that. You need to map port 3306 with the ports
directive like it's done on the Wordpress container if you want to be able to access it from outside the Docker network.
Something like this should work better
WordPress:
container_name: Example-WordPressHomePage-WordPress
image: wordpress:6.7.1-php8.1-apache
ports:
- 80:80
volumes:
- ./wordpress:/var/www/html
restart: always
environment:
- WORDPRESS_DB_HOST=Database
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
Database:
container_name: Example-WordPressHomePage-Database
image: mysql:8.4.3
volumes:
- ./database:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
ports:
- 3306:3306