Mubin
Mubin

Reputation: 4425

Unable to connect to docker mysql using docker wordpress

I'm trying to connect docker wordpress container to MySQL server running another container.

MySQL is running 127.0.0.1:3306

WP: 127.0.0.1:10000

If I run $link = mysqli_connect("127.0.0.1", "root", "root", "wp_db"); from Tinker or plain PHP script, it works fine; also I can connect to docker MySQL using Sequel Pro with above settings. But when I try to put these settings in WP admin page to connect to database, it says

Error establishing a database connection

is there anything that I'm missing?

Upvotes: 0

Views: 618

Answers (1)

jonhid
jonhid

Reputation: 2125

Your problem is that the wp is inside a container and cannot comunicate to the other container.

You can from the host down to the container but not from inside a container to inside another container.

I suggest you take a look at docker-compose, is a tool for defining and running multi-container Docker applications just with a docker-compose.yml configuration file.

One very basic example for your problem could be like this

wp:
  image: yourworpressimagefromdockerhub
  ports:
    - 10000:10000
  links:
    - mysql:mysql
mysql:
  image: yourmysqlimagefromdockerhub
  ports:
    - 3306:3306

"wp" container knows of "mysql" container with the "links" entry.

Then whenever you want to call the mysql docker container from the wp container you just use "mysql" instead of "127.0.0.1"

Upvotes: 1

Related Questions