Search code examples
wordpressdockerwp-cli

How to automate wp instance setup + theme/plugin customization with docker?


I want to create a docker image for wordpress, which creates a ready to use wp instance with already setup and completed installation, activated theme and plugins and other configurations. I was exploring WP CLI to do that as part of the Dockerfile build, however it turned out that this is a stateful process (it requires existing mysql database already in place), which is impossible during docker build.

I'm thinking about creating a bash script which brings up a docker WP CLI container and a pure ubuntu/php container. The wp cli container will then install and setup the wp instance on the ubuntu/php container which will become the defacto WP container, and at last the wp cli container will be shut down by the script.

I'm not sure if this is a good solution, do you have better advises for this use case?


Solution

  • It's a broad and opinion-based question. I think you basically need to figure out three things:

    1. Find and choose a Composer template to manage your dependencies. For example https://roots.io/bedrock/.
    2. WP-CLI commands. I see you are already at it.
    3. Find a way to export and import your WordPress config. For example https://wordpress.org/plugins/wp-cfm/

    A basic setup routine then could maybe look like this:

    # Download WordPress core and plugins.
    composer install -n --no-dev
    
    # Default DB name and user.
    wp config create --dbname="test" --dbuser="root" --dbpass="" --dbhost="127.0.0.1"
    
    # Install a basic site. The title doesn't matter.
    wp core install --url="blog.localhost" --title="Lorem ipsum dolor sit amet" --admin_user="admin" --admin_password="admin" --admin_email="[email protected]" --skip-email
    
    # Language.
    wp language core install de_DE
    
    # Activate WP-CFM and import default conf.
    wp plugin activate wp-cfm
    wp config pull default
    

    You don't necessarily need Composer. You can download WordPress and plugins with just WP-CLI as well. The advantage of Composer is the versioning, and that you only need a composer.json and -.lock file to keep your repo clean. It's then just one command composer install that downloads you and your colleagues everything. Big advantage also is vendor/ can be cached during builds depending on the composer.lock checksum for example. This can shorten build time a lot.

    Alternatively to the above sample routine you could also try to get an existing database backup imported and then just update the base URL and import the updated config. That would skip the WordPress install step and give you dummy content to maybe target automated tests at.