Unable to connect Wordpress docker container to local mysql server

Hi there. I try to connect my wordpress container to mysql. But it didn’t work. I don’t need to use docker-compose, I have to do it locally.

Here is my command.

sudo docker run --name wordpress -p 9001:9000 \
 -v /var/www/html:/var/www/html/ \
 -e WORDPRESS_DB_USER=wp-user \
 -e WORDPRESS_DB_NAME=wordpress \
 --rm wordpress:php8.1-fpm

How can I do it?

Due to the lack of log outputs regarding the issue, I will make a guess: missing grant permission for the user on the database. Make sure that wp-user@% has the grants to actualy access the wordpress database on the host.

The documentation of the image does not mention wheter WORDPRESS_DB_HOST actualy allows IP:PORT or just expects IP (or hostname).

Thanks for your reply. I was able to run wordpress container with flag --net=host and with variable WORDPRESS_DB_HOST= and it worked well, but I need to have 9001 port on this container. So the issue is not with variables because they are work. Something with bridge connection or network between container and the host, I think I have some mistake but I can’t understand where exactly it is.

Just to be sure: you do understand what I ment about grants and the effect of --network=host has on it.

This should be a common usecase, and I can assume the forum search should have plenty of hits for it or at least other users that already succeeded with what you try to do.

good luck!

Since you used the host network, there is no bridge.for your container. Since it doesn’t have a bridge, you can’t forward any port. PHP FPM is listening on the host network, on on port 9000.

Thanks all of you for your replies. I solved this problem by using flag --add-host=host.docker.internal:host-gateway and also changed hostname in WORDPRESS_DB_HOST variable.

sudo docker run --name wordpress --add-host=host.docker.internal:host-gateway -p \
 -v /var/www/html:/var/www/html \
 -v /var/wordpress/uploads:/var/www/html/wp-content/uploads \
 -e WORDPRESS_DB_HOST=host.docker.internal:3306 \
 -e WORDPRESS_DB_USER=wp-user \
 -e WORDPRESS_DB_NAME=wordpress \

Also I maked changes to the file /etc/mysql/mariadb.conf.d/50-server.cnf and changed the string from bind-address = to bind-address = *. After this I executed sudo systemctl restart mysql command and my container worked fine.