Skip to content

docker wp-config.php & database connection  #5513

Closed as not planned
Closed as not planned

Description

@Kipjr

Hi,

I've got a running WP-container with the following wp-config.php (official source):

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', getenv_docker('WORDPRESS_DB_NAME', 'wordpress') );

/** MySQL database username */
define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'example username') );

/** MySQL database password */
define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', 'example password') );

Then I want to execute a basic command but that fails due to the parsing of the database credentials

docker run -it --rm \
    --volumes-from wp \
    --network container:wp  \
     wordpress:cli-php8.0 user list

Error: Error establishing a database connection.

Is there any way how I can supply the credentials?

I can already extract them from docker-compose.yml using this:
db_name=$(grep -Po '(?<=WORDPRESS_DB_NAME: )(.*)' docker-compose.yml)

Activity

  1. Kipjr commented on Apr 21, 2021

    @Kipjr
    Author

    workaround for now is to create an additional entrypoint.sh for docker wp container and change the values in wp-config using sed. But I would rather see something native from wp-cli

  2. ElieMyIdea commented on May 31, 2021

    @ElieMyIdea

    Hi,

    Same problem here :/

  3. caliuf commented on Jun 9, 2021

    @caliuf

    Try to pass to the wpcli container the same env vars you pass to the wordpress container.
    e.g. WORDPRESS_DB_HOST, ecc

  4. maurisrx commented on Jul 31, 2021

    @maurisrx

    To further elaborate @caliuf comment, here is the note from Docker WP page:

    NOTE: Since March 2021, WordPress images use a customized wp-config.php that pulls the values directly from the environment variables defined above (see wp-config-docker.php in docker-library/wordpress#572 and docker-library/wordpress#577). As a result of reading environment variables directly, the cli container also needs the same set of environment variables to properly evaluate wp-config.php.

  5. dbrosy commented on Jul 6, 2022

    @dbrosy

    is there a solution for this yet? I have built a new docker wordpress image and the env vars are there but are not being read.

    Appreciate any assistance as my solution is to replace wp-config.php with old style file

  6. Kipjr commented on Jul 7, 2022

    @Kipjr
    Author
    Currently I use this workaround:
    
    version: '3.8'
    
    x-name:
     - &NAME 'container'
     - &NET 'container_net'
    
    x-variables:
     - &DB_HOST 'db'
     - &DB_ROOT_PASSWORD 'rootpw'
     - &DB_DATABASE 'db'
     - &DB_USERNAME 'user'
     - &DB_PASSWORD 'pw'
     - &DB_PREFIX '_prefix'
    x-template: &default-template
      environment:
        TZ: 'Europe/Amsterdam'
      networks:
        - net
      restart: "unless-stopped"
    
    
    services:
      #MySQL Service
      db:
        <<: *default-template
        hostname: db
        container_name: db
        image: 'yobasystems/alpine-mariadb:latest'
        environment:
          MYSQL_ROOT_PASSWORD: *DB_ROOT_PASSWORD
          MYSQL_DATABASE: *DB_DATABASE
          MYSQL_USER:  *DB_USERNAME
          MYSQL_PASSWORD:  *DB_PASSWORD
    

    And with this wrapper so I can source the env from a json file:
    https://raw.githubusercontent.com/VSLCatena/docker-wordpress/master/wp-cli.sh

  7. danielbachhuber commented on Apr 26, 2023

    @danielbachhuber
    Member

    Feel free to stop by the #cli channel on WordPress.org Slack if you'd like help debugging further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions