<p>For various reasons (the primary one being that I am lazy) I want to mount my wordpress files to /var/www/html/blog rather than /var/www/html and then use the sidecar pattern to have nginx and wordpress-fpm share a directory. I mounted an emptydir to /var/www/html which I expected to be empty (Duh!) and then copy in my files to /var/www/html/blog</p>
<p>My Dockerfile:</p>
<pre><code>FROM wordpress:5.7.2-fpm-alpine
LABEL author=&quot;wayne@...co.uk&quot;

COPY public/wordpress /app/blog

</code></pre>
<p>Wordpress's dockerfile:</p>
<pre><code>#
# NOTE: THIS DOCKERFILE IS GENERATED VIA &quot;apply-templates.sh&quot;
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#

FROM php:7.4-fpm-alpine

# persistent dependencies
RUN set -eux; \
    apk add --no-cache \
# in theory, docker-entrypoint.sh is POSIX-compliant, but priority is a working, consistent image
        bash \
# BusyBox sed is not sufficient for some of our sed expressions
        sed \
# Ghostscript is required for rendering PDF previews
        ghostscript \
# Alpine package for &quot;imagemagick&quot; contains ~120 .so files, see: https://github.com/docker-library/wordpress/pull/497
        imagemagick \
    ;

# install the PHP extensions we need (https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions)
RUN set -ex; \
    \
    apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        freetype-dev \
        imagemagick-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        libzip-dev \
    ; \
    \
    docker-php-ext-configure gd \
        --with-freetype \
        --with-jpeg \
    ; \
    docker-php-ext-install -j &quot;$(nproc)&quot; \
        bcmath \
        exif \
        gd \
        mysqli \
        zip \
    ; \
    pecl install imagick-3.4.4; \
    docker-php-ext-enable imagick; \
    rm -r /tmp/pear; \
    \
    runDeps=&quot;$( \
        scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
            | tr ',' '\n' \
            | sort -u \
            | awk 'system(&quot;[ -e /usr/local/lib/&quot; $1 &quot; ]&quot;) == 0 { next } { print &quot;so:&quot; $1 }' \
    )&quot;; \
    apk add --no-network --virtual .wordpress-phpexts-rundeps $runDeps; \
    apk del --no-network .build-deps

# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN set -eux; \
    docker-php-ext-enable opcache; \
    { \
        echo 'opcache.memory_consumption=128'; \
        echo 'opcache.interned_strings_buffer=8'; \
        echo 'opcache.max_accelerated_files=4000'; \
        echo 'opcache.revalidate_freq=2'; \
        echo 'opcache.fast_shutdown=1'; \
    } &gt; /usr/local/etc/php/conf.d/opcache-recommended.ini
# https://wordpress.org/support/article/editing-wp-config-php/#configure-error-logging
RUN { \
# https://www.php.net/manual/en/errorfunc.constants.php
# https://github.com/docker-library/wordpress/issues/420#issuecomment-517839670
        echo 'error_reporting = E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_RECOVERABLE_ERROR'; \
        echo 'display_errors = Off'; \
        echo 'display_startup_errors = Off'; \
        echo 'log_errors = On'; \
        echo 'error_log = /dev/stderr'; \
        echo 'log_errors_max_len = 1024'; \
        echo 'ignore_repeated_errors = On'; \
        echo 'ignore_repeated_source = Off'; \
        echo 'html_errors = Off'; \
    } &gt; /usr/local/etc/php/conf.d/error-logging.ini

RUN set -eux; \
    version='5.7.2'; \
    sha1='c97c037d942e974eb8524213a505268033aff6c8'; \
    \
    curl -o wordpress.tar.gz -fL &quot;https://wordpress.org/wordpress-$version.tar.gz&quot;; \
    echo &quot;$sha1 *wordpress.tar.gz&quot; | sha1sum -c -; \
    \
# upstream tarballs include ./wordpress/ so this gives us /usr/src/wordpress
    tar -xzf wordpress.tar.gz -C /usr/src/; \
    rm wordpress.tar.gz; \
    \
# https://wordpress.org/support/article/htaccess/
    [ ! -e /usr/src/wordpress/.htaccess ]; \
    { \
        echo '# BEGIN WordPress'; \
        echo ''; \
        echo 'RewriteEngine On'; \
        echo 'RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]'; \
        echo 'RewriteBase /'; \
        echo 'RewriteRule ^index\.php$ - [L]'; \
        echo 'RewriteCond %{REQUEST_FILENAME} !-f'; \
        echo 'RewriteCond %{REQUEST_FILENAME} !-d'; \
        echo 'RewriteRule . /index.php [L]'; \
        echo ''; \
        echo '# END WordPress'; \
    } &gt; /usr/src/wordpress/.htaccess; \
    \
    chown -R www-data:www-data /usr/src/wordpress; \
# pre-create wp-content (and single-level children) for folks who want to bind-mount themes, etc so permissions are pre-created properly instead of root:root
# wp-content/cache: https://github.com/docker-library/wordpress/issues/534#issuecomment-705733507
    mkdir wp-content; \
    for dir in /usr/src/wordpress/wp-content/*/ cache; do \
        dir=&quot;$(basename &quot;${dir%/}&quot;)&quot;; \
        mkdir &quot;wp-content/$dir&quot;; \
    done; \
    chown -R www-data:www-data wp-content; \
    chmod -R 777 wp-content

VOLUME /var/www/html

COPY --chown=www-data:www-data wp-config-docker.php /usr/src/wordpress/
COPY docker-entrypoint.sh /usr/local/bin/

ENTRYPOINT [&quot;docker-entrypoint.sh&quot;]
CMD [&quot;php-fpm&quot;]
</code></pre>
<p>My Deployment</p>
<pre><code>---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 1
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  selector:
    matchLabels:
      app: wordpress
  revisionHistoryLimit: 5
  template: 
    metadata:
      labels:
        app: wordpress
    spec:
      volumes:
        - name: shared
          emptyDir: {}
      containers:
        - name: nginx
          image: nginx
          volumeMounts:
            - name: shared
              mountPath: /var/www/html
        - name: wordpress
          image: redacted/imagename:0.0.1
          volumeMounts:
            - name: shared
              mountPath: /var/www/html
          # Important! After this container has started, the PHP files
          # in our Docker image aren't in the shared volume. 
          # If we tried to write directly to this volume from our Docker image
          #  the files wouldn't appear in the nginx container.
          # So, after the container has started, copy the PHP files from this
          # container's local filesystem
          lifecycle:
            postStart:
              exec:
                command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;cp -r /app/. /var/www/html&quot;]
      imagePullSecrets:
      - name: regcred
</code></pre>
<h2>Now here comes the weird bit... buckle up...</h2>
<p>When I kubectl exec into the container and list the contents of <code>/var/www/html</code> I get:</p>
<pre><code>/var/www/html# ls
blog         readme.html      wp-blog-header.php    wp-content   wp-links-opml.php  wp-mail.php      wp-trackback.php
index.php    wp-activate.php  wp-comments-post.php  wp-cron.php  wp-load.php        wp-settings.php  xmlrpc.php
license.txt  wp-admin         wp-config-sample.php  wp-includes  wp-login.php       wp-signup.php
</code></pre>
<p>There is my blog folder but there are also loads of wordpress files as well, like it has copied the files from <code>/var/lib/docker/volumes</code> into my emptydir... but this isn't how the docs say emptydir is supposed to work. The documentation says:</p>
<blockquote>
<h3>emptyDir</h3>
<p>An emptyDir volume is first created when a Pod is assigned to a node, and exists as long as that Pod is running on that node. As the name says, the emptyDir volume is initially empty. All containers in the Pod can read and write the same files in the emptyDir volume, though that volume can be mounted at the same or different paths in each container. When a Pod is removed from a node for any reason, the data in the emptyDir is deleted permanently.</p>
</blockquote>
<h3>So finally my question... what going on here then???</h3>

Wayne Theisinger
<p>You're starting from the <a href="https://hub.docker.com/_/wordpress" rel="nofollow noreferrer">Docker Hub <code>wordpress</code> image</a>, which has <a href="https://github.com/docker-library/wordpress/tree/1d90641dc2075168fe59df2f02502df068cc5531/latest/php7.4/fpm-alpine" rel="nofollow noreferrer">its Docker image setup in GitHub</a>.</p>
<p>The important detail there is that the <a href="https://github.com/docker-library/wordpress/blob/1d90641dc2075168fe59df2f02502df068cc5531/latest/php7.4/fpm-alpine/Dockerfile" rel="nofollow noreferrer">Dockerfile</a> ends with</p>
<pre class="lang-sh prettyprint-override"><code>ENTRYPOINT [&quot;docker-entrypoint.sh&quot;]
CMD [&quot;php-fpm&quot;]
</code></pre>
<p>This is a standard pattern of using a shell script as a wrapper to do first-time setup, and giving it the actual command to run (Docker <a href="https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact" rel="nofollow noreferrer">passes the <code>CMD</code> as arguments to the <code>ENTRYPOINT</code></a>).  The <a href="https://github.com/docker-library/wordpress/blob/1d90641dc2075168fe59df2f02502df068cc5531/latest/php7.4/fpm-alpine/docker-entrypoint.sh" rel="nofollow noreferrer">Wordpress image <code>docker-entrypoint.sh</code></a> in turn has the fragment:</p>
<pre class="lang-sh prettyprint-override"><code>if [ ! -e index.php ] &amp;&amp; [ ! -e wp-includes/version.php ]; then
  echo &gt;&amp;2 &quot;WordPress not found in $PWD - copying now...&quot;
  ...
  for contentPath in \
    /usr/src/wordpress/.htaccess \
    /usr/src/wordpress/wp-content/*/*/ \
  ; do
    ...
  done
  tar cf - ... . | tar xf -
fi
</code></pre>
<p>That fragment looks at the current directory; if it doesn't have an <code>index.php</code> file, it copies <code>/usr/src/wordpress</code> there.  This runs when the container starts up, after any volumes have been mounted, and before your <code>postStart</code> hook triggers.</p>
<p>You could take advantage of this setup by copying your own content into the Wordpress base tree, instead of setting up the separate hook:</p>
<pre class="lang-sh prettyprint-override"><code>FROM wordpress:5.7.2-fpm-alpine
COPY public/wordpress /usr/src/wordpress/blog
</code></pre>
