Docker-compose wordpress mysql connection failed

I created a small docker-compose.yml that was used as a charm to deploy small instances of WordPress. It looks like this:

 wordpress: image: wordpress:latest links: - mysql ports: - "1234:80" environment: WORDPRESS_DB_USER: wordpress WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_PASSWORD: "password" WORDPRESS_DB_HOST: mariadb MYSQL_PORT_3306_TCP: 3306 volumes: - /srv/wordpress/:/var/www/html/ mysql: image: mariadb:latest mem_limit: 256m container_name: mariadb environment: MYSQL_ROOT_PASSWORD: "password" MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: "password" volumes: - /srv/mariadb:/var/lib/mysql 

But when I start it now (maybe from the moment the dockers were upgraded to Docker version 1.9.1, build a34a1d5 ), it fails

 wordpress_1 | Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10 wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused 

When I cat /etc/hosts for wordpress_1 , there are entries for MySQL:

 172.17.0.10 mysql 12a564fdbc56 mariadb 

and I can ping the MariaDB server.

When I docker-compose up , WordPress installs and after several restarts, the MariaDB container prints:

 Version: '10.0.22-MariaDB-1~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution 

What is the point of indicating that it works, right?

How do I get WordPress to connect to the MariaDB container?

+7
docker mariadb docker-compose wordpress
source share
6 answers

The reason for this behavior was probably due to a recent kernel and docker update. I found several other connection problems in other docker-compose installations. So I restarted the server (and not just the docker service) and since then I have not had such problems.

+5
source share

To fix this problem, the first thing to do is:

Add the following code to the containers for Wordpress and the database (in the file for building dockers):

 restart: unless-stopped 

This will start the database and initialize it before trying to connect to the wordpress container. Then restart the docker engine

 sudo restart docker 

or (for ubuntu 15+)

 sudo service docker restart 

Here's the full configuration that worked for me to install wordpress using MariaDB:

 version: '2' services: wordpress: image: wordpress:latest links: - database:mariadb environment: - WORDPRESS_DB_USER=wordpress - WORDPRESS_DB_NAME=mydbname - WORDPRESS_TABLE_PREFIX=ab_ - WORDPRESS_DB_PASSWORD=password - WORDPRESS_DB_HOST=mariadb - MYSQL_PORT_3306_TCP=3306 restart: unless-stopped ports: - "test.dev:80:80" working_dir: /var/www/html volumes: - ./wordpress/:/var/www/html/ database: image: mariadb:latest environment: - MYSQL_ROOT_PASSWORD=password - MYSQL_DATABASE=mydbname - MYSQL_USER=wordpress - MYSQL_PASSWORD=password restart: unless-stopped ports: - "3306:3306" 
+5
source share

I used your docker-compose.yml, had the same problem. Just rebooting does not fix it. After nearly an hour of researching the logs, I found that the problem was this: the wordpress service started connecting the mysql service before it was fully started. Just adding depend_on will not help. Docker Compose waits for container X to launch Y

work around can be started by db server up to. When it is fully running, run docker-compose up . Or just use an external service.

+1
source share

I had almost the same problem, but only restarting the Wordpress container saved me:

 $ docker restart wordpress 

I hope this helps a lot of people.

+1
source share

I also had problems. I used docker-compose to create multiple Wordpress sites on one (micro) virtual private server, including phpmyadmin and jwilder/nginx-proxy as a controller.

$ docker logs XXXX will help indicate areas of concern. In my case, MariaDB databases will constantly restart.

It turns out that all this simply would not fit on a micro-512M Single CPU. I never received error messages that directly told me that size was a problem, but after adding something, I realized that when all the databases started up, I ran out of memory. Upgrading to 1 GB, 1 CPU service worked fine.

0
source share

In my case, I use Mysql (not MariaDb), but I had the same problem. After updating the version of MySQL, everything works fine.

You can see my open source docker-compose configuration: https://github.com/rimiti/wordpress-dockerized-environment

0
source share

All Articles