Home > database >  Docker Wordpress: Error establishing a database connection
Docker Wordpress: Error establishing a database connection

Time:10-05

I'm having trouble deploying a Wordpress install via Docker compose/Portainer. Every time I try to deploy the docker-compose I get the following error: Error establishing a database connection on the Wordpress install.

I believe there is a mistake somewhere in the docker-compose below as I have eliminated other possibilities. It seems the WP install cannot communicate with the database volume in the container.

Any help would be appreciated, thanks

System info:

OS: Ubuntu 21.04
System: Rpi4 64bit
Docker-compose version: 1.29.2

docker-compose:

version: "3.9"
services:
        
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    container_name: wordpress
    restart: unless-stopped
    ports: # For inital setup
      - "91:80"
    security_opt:
      - no-new-privileges:true
    environment:

      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: mbA2tU9RyA5r@U
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - sbabaya:/var/www/html
      
  mariadb:
    image: mariadb:latest
    container_name: db
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: mbA2tU9RyA5r@U
      MYSQL_DATABASE: wordpress
    volumes:
      - "db:/var/lib/mysql"
    logging:
      driver: "json-file"
      options:
        max-size: "10Mb"
        max-file: "5"
volumes:
  wordpress:
  db:

CodePudding user response:

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:
  • Related