28

I've created a small docker-compose.yml which used to work like a charm to deploy small WordPress instances. 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 since docker update 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 of the wordpress_1 there are entries for MySQL:

172.17.0.10 mysql 12a564fdbc56 mariadb

and I am able to ping the MariaDB server.

When I docker-compose up, WordPress gets installed 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

Which schould indicate it to be running, isn't it?

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

5
  • 1
    what port is your mysql container exposing 3306 on? do you have an environment variable: MYSQL_PORT_3306_TCP_PORT?
    – Michael
    Dec 3, 2015 at 17:04
  • Thanks for your answer. Its running on 3306, as you can see in the started mariadb docker message (scroll right)... still got no idea, why this settup isn't working anymore
    – Harry
    Dec 4, 2015 at 16:25
  • That is inside the container, I am asking what is the exposed port on the docker container: docker port <CONTAINER_ID>
    – Michael
    Dec 4, 2015 at 16:26
  • Thanks @Michael for your will to help. A complete restart after server update solved any of the wierd behaviours and the setup started working again.
    – Harry
    Dec 6, 2015 at 18:33
  • 1
    Since this topic was recently active, I'll note that WORDPRESS_DB_HOST should point to the database service name as listed in the docker-compose file, which in the case above is "mysql" and not "mariadb".
    – otravers
    Sep 15, 2016 at 20:54

7 Answers 7

12

To fix this issue the first thing to do is:

Add the following code to wordpress & database containers (in the docker-compose file):

restart: unless-stopped

This will make sure you Database is started and intialized before wordpress container trying to connect to it. Then restart docker engine

sudo restart docker

or (for ubuntu 15+)

sudo service docker restart 

Here the full configuration that worked for me, to setup wordpress with 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"
2
  • 4
    You saved me mate! Cheers Jul 9, 2018 at 11:50
  • Please keep in mind that you should probably not expose the MySQL port to the public. The "ports" section is probably not even needed in this case, since the WordPress instance can communicate with the database using the link defined in the links section.
    – xxmicloxx
    Apr 20, 2020 at 15:02
10

The reason for this behaviour probably was related to a recent kernel and docker update. I recognized several other connection issues in other docker-compose setups. Therefore I restarted the server (not just the docker service) and didn't have had any issues like this ever since.

2
  • Yeah docker daemon restart helped
    – lisak
    May 4, 2016 at 22:30
  • 4
    Restarting docker daemon doesn't fix this. Jan 8, 2018 at 10:09
3

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

$ docker restart wordpress

I hope this help many people.

2

I too had troubles here. I was using docker-compose to set up multiple wordpress websites on a single (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, the MariaDB databases would keep restarting all the time.

It turns out that all that stuff just wouldn't fit on a micro 512M Single CPU service. I never received error messages that told me directly that size was an issue, but after adding things up, I realized that when all the databases were starting up, I was running out of memory. An upgrade to 1Gb, 1 CPU service worked just fine.

1
  • Thank you a lot @zipzit Helped me to fix problem i fought hours
    – Bullwinkle
    Jul 17, 2020 at 20:59
1

I was using your docker-compose.yml, had the same problem. Just restarting didn't fix. After nearly an hour of researching the logs, I found the problem was: wordpress service started connecting mysql service before it had fully started. Simply adding depends_on won't help.Docker Compose wait for container X before starting Y

the work around could be start the db server before Up. When it has fully started, run docker-compose up. Or just use external service.

1
  • 1
    you can use depends_on: database in wordpress service, but still this is not working for me Jan 8, 2018 at 10:11
1

This simply means you are trying to connect to the wrong host. In order to use this in localhost just use the name of your service as the database host example in your case, it would be mysql you can fix this by specifying the name of the localhost with a default variable like this MYSQL_ROOT_HOST: localhost

0

In my case, I'm using Mysql (not MariaDb) but I had the same problem. After upgrading the MySQL version, it's works fine.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.