Dockerized WordPress shows ‘Error establishing a database connection

Solution:
The issue is that the database host is missing or incorrectly set. In a Docker Compose setup, the host should match the container name for the database service, as Docker automatically sets up a private network for the containers.

Update your environment section like this:

environment:
  WORDPRESS_DB_HOST: sbabaya-db
  WORDPRESS_DB_USER: sbabaya
  WORDPRESS_DB_PASSWORD: mbA2tU9RyA5r@U
  WORDPRESS_DB_NAME: cue_sbabaya

Tips:

  • Keep your service names simple for easier reference.
  • Consider using the official MySQL image, which works on arm64 and is well-documented.
  • This approach aligns with the official WordPress Docker documentation: WordPress on Docker Hub
    .

Example docker-compose.yml:

version: '3.1'

services:
  wordpress:
    image: wordpress
    restart: always
    ports:
      - 80:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wordpress:/var/www/html

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql
    ports:
      - 3306:3306

volumes:
  wordpress:
  db:

This ensures WordPress can properly connect to the database container.