3

I'm looking for how to activate a WordPress plugin via a Dockerfile RUN command in the build process.

The relevant command in the Dockerfile is

# activate plugins
RUN wp plugin activate wp-discourse-export --allow-root --path=/var/www/html

The error I'm getting is

Step 10/10 : RUN wp plugin activate wp-discourse-export --allow-root --path=/var/www/html
---> Running in c5dac3f62c4c
Error: This does not seem to be a WordPress install.
Pass --path=`path/to/wordpress` or run `wp core download`.
ERROR: Service 'wordpress' failed to build: The command '/bin/sh -c wp plugin 
activate wp-discourse-export --allow-root --path=/var/www/html' returned a non-zero code: 1

If I disable that RUN command, build the image, bring the docker images up, log in to the docker image that was created and run

wp plugin activate wp-discourse-export --allow-root --path=/var/www/html
Plugin 'wp-discourse-export' activated.
Success: Activated 1 of 1 plugins.

the activation succeeds.

How can a WordPress plugin in a docker image be activated in the build process?

Update:

I'm working around the issue by copying a script to the image and then run it later from the host.

in the Dockerfile.

# activate plugins
COPY activate-wordpress-plugins.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/activate-wordpress-plugins.sh

after docker-compose I run this command once.

docker exec -it wordpress_1_7522c9dae310 /usr/local/bin/activate-wordpress-plugins.sh

Update 2

I created a script to bring up the instances and then run the script.

#!/usr/bin/env bash

echo docker-compose up -d
docker-compose up -d

echo docker exec -it $(docker-compose ps -q wordpress)  /usr/local/bin/activate-wordpress-plugins.sh
docker exec -it $(docker-compose ps -q wordpress)   /usr/local/bin/activate-wordpress-plugins.sh
Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64

2 Answers2

4

I think the problem is that WordPress plugin activation requires the WordPress installation to be live, with a valid db connection. You don't have that environment instantiated yet during the image build. Building the image is not the same as running that image in a container.

The appropriate time to activate the plugin would be at launch time, as you suggest,when the container instantiates.

Rodrigo Murillo
  • 13,080
  • 2
  • 29
  • 50
3

Wordpress does have a special directory named mu-plugins. Just simply place you plugins there. https://codex.wordpress.org/Must_Use_Plugins.

WordPress only looks for PHP files right inside the mu-plugins directory. You may want to create a proxy PHP loader file inside the mu-plugins directory

  • Always-on, no need to enable via admin and users cannot disable by accident.
  • Can be enabled simply by uploading file to the mu-plugins directory, without having to log-in.
  • Loaded by PHP, in alphabetical order, before normal plugins, meaning API hooks added in an mu-plugin apply to all other plugins even if they run hooked-functions in the global namespace