Skip to content

A smart way to use the CLI to replace the WORDPRESS_CONFIG_EXTRA variable #544

Closed
@kamihicouki

Description

@kamihicouki

Hi everyone, nice to meet you all.
I've been looking at the documents and discussions in the following order and have come up with a better alternative, so I'll post it.

  1. https://hub.docker.com/_/wordpress/
  2. Inject configuration using environment variable #142
  3. echo >&2
    echo >&2 'WARNING: environment variable "WORDPRESS_CONFIG_EXTRA" is set, but "wp-config.php" already exists'
    echo >&2 ' The contents of this variable will _not_ be inserted into the existing "wp-config.php" file.'
    echo >&2 ' (see https://github.com/docker-library/wordpress/issues/333 for more details)'
    echo >&2

I feel that the Command line interface for WordPress | WP-CLI project is the best way to accurately, reliably, and quickly rewrite the values of variables in wp-config.php.

And it is fortunately available as a wordpress - Docker Hub official image.

MySQL is an essential part of WordPress.
Likewise, considering that WP-CLI is essential for a flexible WordPress/wp-config.php configuration, how about the following?
I think the idempotence of the configuration will be preserved.

---
version: "2.4"
services:
  wp-init:
    image: wordpress:cli
    restart: on-failure
    environment:
      - WP_CONFIG_DB_CHARSET=utf8mb4
      - WP_CONFIG_DB_HOST=db
      - WP_CONFIG_TABLE_PREFIX=wp_
    # ... other WP_CONFIG_* variables or env_file load files/variables.
    volumes:
      - wordpress:/var/www/html
      - ./wp-config.sh:/wp-config.sh:ro
    working_dir: /var/www/html
    user: root
    command: sh /wp-config.sh
#!/bin/sh

printenv | sed 's/^WP_CONFIG_//;t;d' | sed 's/=/ /' | while read LINE; do wp --allow-root config set $LINE; done

This is just part of my idea of how to do it.
What are your thoughts on this?
Have a great day.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RequestRequest for image modification or feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions