WordPress Docker “Error establishing a database connection”

Solution:

Your yaml file works fine for me. The only thing I noticed is there’s a missing database name variable in wordpress.environment (WORDPRESS_DB_NAME=wordpress) but this defaults to wordpress if not found. I’m pointing this out in case your actual copy has a db name other than wordpress.

This is the compose file which works fine for me:

version: "3"

services:
   db_node_domain:
     image: mysql:5.7
     volumes:
        - db_data:/var/lib/mysql
     restart: always
     environment:
        MYSQL_ROOT_PASSWORD: somewordpress
        MYSQL_DATABASE: wordpress
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress
     container_name: wp_test_db

   wordpress:
     depends_on:
        - db_node_domain
     image: wordpress:latest
     ports:
        - '8000:80'
     expose:
        - 80
     restart: always
     environment:
        VIRTUAL_HOST: blog.example.com
        LETSENCRYPT_HOST: blog.example.com
        LETSENCRYPT_EMAIL: foo@example.com
        WORDPRESS_DB_HOST: db_node_domain:3306
        WORDPRESS_DB_USER: wordpress
        WORDPRESS_DB_PASSWORD: wordpress
        WORDPRESS_DB_NAME: wordpress # THIS WAS MISSING?
     container_name: wp_test
volumes:
  db_data:

networks:
  default:
    external:
      name: nginx-proxy

If you want to start afresh and try again you could run docker-compose down and then docker-compose upTHIS WILL DESTROY THINGS THOUGH so be careful 🙂