Docker WordPress: Error establishing a database connection

Solution:

You have missed the database host – this will be the container label used in the compose file, they are automatcally setup in their own private network.

Update your environment as follows –

environment:
WORDPRESS_DB_HOST: sbabaya-db
WORDPRESS_DB_USER: sbabaya
WORDPRESS_DB_PASSWORD: mbA2tU9RyA5r@U
WORDPRESS_DB_NAME: cue_sbabaya
Check your names, keep it simple – you might get more milage form mysql offical image (will work on arm64 as well).

This is also consistent with the offical docs here – https://hub.docker.com/_/wordpress

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: