1

I am using docker to run a develop environment for several wordpress themes / plugins. However, I can't figure out how to enable my themes automatically on container creation. Normally I would just use wp-cli for this. I have created a custom image that extends the official wordpress image, but I can't figure out how to make the wp-cli commands run after the /var/www/html folder get's setup (I think this is created by the wordpress image entrypoint script). If I put the commands in a "RUN" command in the dockerfile, the commands just fail since the /var/www/html directory is empty. However, if I connect to the container once it is setup, the directory is populated, and wp-cli works just fine. How can I run a command after the entrypoint.sh from the parent image is run?

Here is the content of my dockerfile (which does not work):

FROM wordpress
MAINTAINER Me!
COPY docker-install/wp-cli.phar /usr/local/bin/wp
WORKDIR /var/www/html
RUN chmod u+x /usr/local/bin/wp
RUN /usr/local/bin/wp core install --title="Test WP Site" --admin_user=admin --admin_password=something --admin_email=my@cool.email  --url=localhost:8080 --allow-root
RUN /usr/local/bin/wp theme activate mytheme --allow-root
Daniel
  • 794
  • 10
  • 20
  • You'll need to use a custom entrypoint script. `ENTRYPOINT` and `CMD` are runtime instructions so anything you put in the Dockerfile (build-time) will run first. – johnharris85 Apr 09 '17 at 19:04

3 Answers3

1

The short answer: You can't. The ENTRYPOINT and CMD is are used at Docker container runtime, not build time. In this case, the Wordpress environment is actually started at container runtime so you can't interact with it during a build.

The long answer: You might be able to help accomplish your goals by using Docker Compose to create two services, one for Wordpress and another for the Wordpress CLI variant (see the cli tags at https://hub.docker.com/_/wordpress/) with a volumes_from the first one. Then, you could use docker-compose run cli-service wp-cli command to run your commands.

Andy Shinn
  • 26,561
  • 8
  • 75
  • 93
0

I created a prepare.sh file that contains commands I want to run before Apache starts, and then added this to the Dockerfile:

COPY ./prepare.sh /prepare.sh
RUN chmod +x /prepare.sh
CMD /prepare.sh && apache2-foreground
Richard
  • 639
  • 2
  • 8
  • 24
  • Have you tried this script? If You view the original docker-entrypoint.sh https://github.com/docker-library/wordpress/blob/0a5405cca8daf0338cf32dc7be26f4df5405cfb6/php5.6/apache/docker-entrypoint.sh#L26 checks if de CMD starts with apache2 but this overrided script doesn't – doterob Jul 27 '17 at 06:46
  • Thanks for pointing that out! I have tried it and it works. What is the purpose of that line? – Richard Jul 28 '17 at 13:24
0

You should create a script that install themes after docker-entrypoint. The name has to start with apache2 (docker-entrypoint checks name of CMD param https://github.com/docker-library/wordpress/blob/0a5405cca8daf0338cf32dc7be26f4df5405cfb6/php5.6/apache/docker-entrypoint.sh#L26), for example apache2-setup-wordpress.sh.

Dockerfile would be like this

FROM wordpress:4.7.3

COPY apache2-setup-wordpress.sh /usr/local/bin
RUN chmod +x /usr/local/bin/apache2-setup-wordpress.sh
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-setup-wordpress.sh", "apache2-foreground"]
doterob
  • 94
  • 5