install wordpress with cli in docker gives "error establishing database connection" with mariadb container

Viewed 38

I try to set up a wordpress website with docker-compose, by having one container for nginx, one for mariadb, and a last one for wordpress, and also two volumes for worpdress and mariadb

I'm not there yet, I'm stuck at an intermediate step : using wp-cli (https://make.wordpress.org/cli/handbook/how-to-install/) I want to configure wordpress with the database, but I get an error :

project architecure :

|_ docker-compose.yml
|_ .env
|_ mariadb/
   |_ Dockerfile
|_ wordpress/
   |_ Dockerfile

wordpress dockerfile : (it's missing steps, like an entrypoint or a cmd, but i'm already getting an error)

FROM debian:buster

RUN apt update && apt install -y \
    php7.3 \
    php7.3-mysqli \
    curl

# install wp-cli : https://make.wordpress.org/cli/handbook/guides/installing/
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar &&\ 
    chmod +x wp-cli.phar && \
    mv wp-cli.phar /usr/local/bin/wp

ARG WP_DIR=/var/www/html

ARG DB_NAME
ARG DB_USER
ARG DB_PSWD
ARG WP_URL
ARG WP_TITLE
ARG WP_ADMIN
ARG WP_ADMIN_PSWD
ARG WP_ADMIN_EMAIL

# install wordpress with cli : https://make.wordpress.org/cli/handbook/how-to-install/
RUN wp core download --path=${WP_DIR} --allow-root
RUN wp config create --dbname=${DB_NAME} \
                     --dbuser=${DB_USER} \
                     --dbpass=${DB_PASS} \
                     --path=${WP_DIR} \
                     --allow-root \
                     --skip-check
# this command gives an error :
RUN wp core install --url=${WP_URL} \
                    --title=${WP_TITLE} \
                    --admin_user=${WP_ADMIN} \
                    --admin_password=${WP_ADMIN_PSWD} \
                    --admin_email=${WP_ADMIN_EMAIL} \
                    --path=${WP_DIR} \
                    --allow-root

mariadb dockerfile :

FROM debian:buster

ARG DB_NAME
ARG DB_USER
ARG DB_PSWD

RUN apt update && apt install -y \
    mariadb-client \
    mariadb-server \
    && \
    rm -rf /var/lib/apt/lists/*

# configure wp database
RUN service mysql start && \
    mariadb --execute="CREATE DATABASE ${DB_NAME};" && \
    mariadb --execute="CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PSWD}';" && \
    mariadb --execute="GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' with grant option;"

# start mysql server (https://www.mysqltutorial.org/mysql-adminsitration/start-mysql)
CMD [ "service", "mysql", "start" ]

docker-compose.yml :

version: "3.8"

services:

  mariadb:
    env_file: .env
    build:
      context: ./mariadb
      args:
        - DB_NAME=${DB_NAME}
        - DB_USER=${DB_USER}
        - DB_PSWD=${DB_PSWD}
    image: mariadb
    container_name: mymariadb

  wordpress:
    env_file: .env
    build:
      context: ./wordpress
      args:
        - WP_URL=${WP_URL}
        - WP_TITLE=${WP_TITLE}
        - WP_ADMIN=${WP_ADMIN}
        - WP_ADMIN_PSWD=${WP_ADMIN_PSWD}
        - WP_ADMIN_EMAIL=${WP_ADMIN_EMAIL}
        - DB_NAME=${DB_NAME}
        - DB_USER=${DB_USER}
        - DB_PSWD=${DB_PSWD}
    image: wordpress
    container_name: mywordpress

.env file :

## MARIADB SETUP
DB_NAME=db_wp
DB_USER=db_user
DB_PSWD=db_pswd

## WORDPRESS SETUP
WP_URL=wp_url.fr
WP_TITLE=wp_blog
WP_ADMIN=wp_admin
WP_ADMIN_PSWD=wp_admin_pswd
WP_ADMIN_EMAIL=wp_email@wp.fr

if I run docker-compose build I get this error :

Error establishing a database connection. This either means that the username and password information in your `wp-config.php` file is incorrect or that contact with the database server at `localhost` could not be established. This could mean your host’s database server is down.

I think that the wordpress container cannot use the database running in the mariadb container. I tried to gives an explicit network but it didn't works either. I also tried to make wordpress build depends on mariadb, but it was not successful either :

wordpress:
  ...
  depends_on:
    mariadb:
      condition: service_completed_successfully
  ...

I don't know if it's even possible to install wordpress during build time ? maybe I should launch a script at run time ? I'm new to all of this (docker, worpress, mariadb, and nginx, php, php-fpm that I didn't show here because it's not relevant to this error) so I'm certainly doing a lot of mistakes, my apologies

I'm confused about the line CMD [ "service", "mysql", "start" ] in mariadb dockerfile, it doesn't act good, if I run the container it stays up for 5 or 6 seconds then it exits. But if I use CMD [ "mysqld" ] instead it works great, although I don't understand why. But I don't think it is connected to my problem with wordpress installation

0 Answers
Related