I want to use the official WP image in a Dockerfile and then run a bash script to customize the base WP install (such as install a custom child theme, set a default theme in wp-config.php, etc).
This needs to be done with a bash script since I want to use docker-compose environment variables.
In other words, if you have a base image which has an ENTRYPOINT can you identify an additional ENTRYPOINT in subsequent images?
I haven't found a direct answer to this yet. Any thoughts?
Thx!
It does not pull it down at run, the WP source is in /usr/src/wordpress/ and it is then copied to /var/www/html in the entrypoint since that directory is a VOLUME so it could be a bind mount. If you create an image FROM wordpress and add your own ENTRYPOINT, it would replace the old ENTRYPOINT value.
You might be able to call your script apache2-something.sh, add it to /usr/local/bin and set the CMD to ["apache2-something.sh"] then it would be called at the end of the current entrypoint after doing all the copy and setup. Just make sure to end with exec apache2-foreground and it should just work
Right...thx! I corrected my original comment.
For anyone else that runs across this....
I needed to execute the updates on run (docker-compose) instead of on build (Dockerfile) because I wanted to use docker-compose environment variables to update wp-config.php and wp-content with custom content.
I was able to do this by copying over custom content along with another ENTRYPOINT script via my Dockerfile. To still fire off the ENTRYPOINT from the official WP image I did the following.
Do let me know if anyone sees anything inherently wrong with this approach. Thx!
#!/bin/bash
# mute CMD from official wordpress image
sed -i -e 's/^exec "$@"/#exec "$@"/g' /entrypoint.sh
# execute bash script from official wordpress image
source /entrypoint.sh
# replace parent image wp-content with custom wp-content
rm -Rf ./wp-content && cp -R /usr/src/<custom wp-content>/* .
chown -R www-data:www-data .
# run additional updates to wp-content and wp-config.php here
# execute CMD
exec "$@"
I hadn't thought of doing it that way, but seems like a solid workaround to me. :+1:
Massive props @bkcummins , your workaround really saved my day/week/month. I'd been searching for a way of working around the VOLUME declaration (looking for some sort of UNVOLUME or something) for a couple of days when I came across this.
I got so excited when I got this working that I stopped working for the day, went out for a bike ride, did some big jumps and generally had an ace time. So thanks for making my afternoon!
@bkcummins Hi there, when executing the entry point from the official Wordpress image, i.e. "docker-entrypoint.sh" from my custom shell script as entry point, I get:
/usr/local/bin/docker-entrypoint.sh: line 26: $1: unbound variable
This is the line:
if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
from the file: https://github.com/docker-library/wordpress/blob/master/docker-entrypoint.sh
I simply added an entrypoint to docker-compose with a custom shell script that has your solution in it (I also changed the name of entrypoint.sh to match wordpress's official "docker-entrypoint.sh")
Any Ideas ?
Thanks
@bkcummins, @myuseringithub or @shankiesan - anyone want to post an example repo of this working?
I'm having a difficult time getting this to work properly.
Thanks!
I don't immediately have a repo @fatjester because I'm using it in some non-open-source stuff at the moment, but I can give you some snippets:
my-custom-entrypoint.sh:
#!/bin/bash
# mute CMD from official wordpress image
sed -i -e 's/^exec "$@"/#exec "$@"/g' /usr/local/bin/docker-entrypoint.sh
# execute bash script from official wordpress image
source /usr/local/bin/docker-entrypoint.sh
# replace parent image wp-content with custom wp-content
cp -R /usr/src/wordpress/wp-content/themes/* ./wp-content/themes/ 2>/dev/null || : # Suppress errors
cp -R /usr/src/wordpress/wp-content/plugins/* ./wp-content/plugins/ 2>/dev/null || : # Suppress errors
chown -R www-data:www-data .
chmod -R g+w .
if [ "$PROPERDOCKER_STAGE" == "prod" ]; then
#statements
export NR_APP_NAME="$PROPERDOCKER_NAME-$PROPERDOCKER_STAGE"
# New Relic install
echo "Enabling APM metrics for ${NR_APP_NAME}"
newrelic-install install
# Update the application name
sed -i "s/newrelic.appname = \"PHP Application\"/newrelic.appname = \"${NR_APP_NAME}\"/" /usr/local/etc/php/conf.d/newrelic.ini
else
# Kill caching if we're on local or staging
rm -rf /usr/local/etc/php/conf.d/opcache-recommended.ini
rm -rf /usr/local/etc/php/conf.d/docker-php-ext-apcu.ini
rm -rf /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini
rm -rf /usr/local/etc/php/conf.d/docker-php-ext-memcache.ini
rm -rf /etc/apache2/mods-enabled/pagespeed.conf
rm -rf /etc/apache2/mods-enabled/pagespeed.load
fi
# run additional updates to wp-content and wp-config.php here
# execute CMD
exec "$@"
Dockerfile:
FROM wordpress:php7.0-apache
# The guts from https://github.com/conetix/docker-wordpress-wp-cli added here so that we can use FPM
# rather than the default Apache image
# Add sudo in order to run wp-cli as the www-data user
RUN apt-get update && apt-get install -y sudo less pwgen unzip jshon wget nano
# Install New Relic daemon
RUN wget -O - https://download.newrelic.com/548C16BF.gpg | apt-key add - && \
echo "deb http://apt.newrelic.com/debian/ newrelic non-free" > /etc/apt/sources.list.d/newrelic.list
RUN apt-get update && \
apt-get -yq install newrelic-php5
# This is actually the correct version – both 5 and 7 support are in the above package
# Install Memcache ## Temporarily disabled for PHP7
#RUN yes '' | pecl install -f memcache
#RUN docker-php-ext-enable memcache
#Install APC php opcode caching
RUN yes '' | pecl install -f apcu-5.1.6
RUN docker-php-ext-enable apcu
# enable Apache headers module
RUN a2enmod headers
# Tweak PHP settings
# Make a more sensible file upload size
RUN printf "upload_max_filesize = 64M\npost_max_size = 64M" > /usr/local/etc/php/conf.d/uploads.ini
# Increase the memory limit
RUN printf "memory_limit = 200M" > /usr/local/etc/php/conf.d/memory.ini
# Install Google Pagespeed module for Apache
RUN mkdir -p /installpagespeed
WORKDIR /installpagespeed
RUN wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb
RUN dpkg -i mod-pagespeed-*.deb
RUN apt-get -f install -y
RUN rm -rf /installpagespeed
RUN mkdir -p /var/cache/mod_pagespeed
RUN chmod g+w /var/cache/mod_pagespeed
RUN chown www-data:www-data /var/cache/mod_pagespeed/
# Shell housekeeping
RUN echo "alias ll=\"ls -lah\"" >> ~/.bashrc
ENV TERM xterm
# Add apache config
RUN printf "<IfModule mpm_prefork_module> \n\
StartServers 3 \n\
MinSpareServers 3 \n\
MaxSpareServers 5 \n\
MaxClients 30 \n\
MaxRequestsPerChild 0 \n\
</IfModule>" >> /etc/apache2/apache2.conf
WORKDIR /var/www/html
# Add other PHP extensions
RUN docker-php-ext-install zip
# Cleanup
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Set the container timezone
RUN echo Europe/London >/etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata
ADD ./scripts /scripts
ADD ./proper-config.json /proper-config.json
### Plugins ###
WORKDIR /usr/src/wordpress/wp-content/plugins/
RUN rm -Rf *
ENV force_update 100002 # Bump this up if you want to force a re-pull of themes and plugins
# Recursively add plugins from proper-config.json
RUN /bin/bash -c '/scripts/install-plugins.sh'
### Themes ###
WORKDIR /usr/src/wordpress/wp-content/themes/
RUN rm -Rf *
# Recursively add themes from proper-config.json
RUN /bin/bash -c '/scripts/install-themes.sh'
RUN usermod -u 1000 www-data
WORKDIR /var/www/html
ENTRYPOINT ["/scripts/proper-entrypoint.sh"]
CMD ["apache2-foreground"]
Thanks! I was missing the CMD ["apache2-foreground"] at the end of the docker file.
If someone has come up a usage from docker-compose.yml, it would be highly appreciated. I keep getting the "no such file or directory", when trying that route.
I have docker-compose.yml:
---
version: '2'
services:
db:
image: mysql:5.7
#volumes:
# - "./db_data:/var/lib/mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
build: .
#volumes:
# - "./uploads:/var/www/html/wp-content/uploads"
depends_on:
- db
ports:
- 80:80
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_PASSWORD: wordpress
volumes:
db_data:
Then Dockerfile:
FROM wordpress:latest
# Add WP-CLI
RUN curl -o /bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod +x /bin/wp
ADD ./scripts /scripts
ENTRYPOINT ["/scripts/wp-init.sh"]
CMD ["apache2-foreground"]
and finally a scripts/wp-init.sh:
#!/bin/bash
set -euo pipefail
# mute CMD from official wordpress image
sed -i -e 's/^exec "$@"/#exec "$@"/g' /usr/local/bin/docker-entrypoint.sh
# execute bash script from official wordpress image
source /usr/local/bin/docker-entrypoint.sh
# I would do my thing here, but this ain't working
# execute CMD
exec "$@"
Then I run this:
docker-compose build --no-cache && docker-compose up && docker-compose down --volumes
But error I keep getting is that
wordpress_1 | standard_init_linux.go:178: exec user process caused "no such file or directory"
wordpress_1 | standard_init_linux.go:178: exec user process caused "no such file or directory"
wordpress_1 | standard_init_linux.go:178: exec user process caused "no such file or directory"
@Ciantic, a simple but maybe overlooked solution: is the file ./scripts/wp-init.sh executable locally? Maybe it just needs a chmod +x and then a docker-compose build?
Closing since this isn't really an issue with the image so much as a request for help (and the original request is solved).
In the future, these sorts of questions/requests would be more appropriately posted to the Docker Community Forums, the Docker Community Slack, or Stack Overflow. Thanks!
Most helpful comment
Right...thx! I corrected my original comment.
For anyone else that runs across this....
I needed to execute the updates on run (docker-compose) instead of on build (Dockerfile) because I wanted to use docker-compose environment variables to update wp-config.php and wp-content with custom content.
I was able to do this by copying over custom content along with another ENTRYPOINT script via my Dockerfile. To still fire off the ENTRYPOINT from the official WP image I did the following.
Do let me know if anyone sees anything inherently wrong with this approach. Thx!