How do I copy Docker WordPress files from initContainer into an emptyDir in Kubernetes?

9/23/2020

I'm trying to copy the final WordPress files from the official WordPress Docker image, from an initContainer into an emptyDir volume. I expect the emptyDir volume to contain the entire WordPress installation so I can share this between containers. Instead I only get the wp-contents folder with no content. No other files or folders are copied over.

After reviewing the WordPress Dockerfile and entrypoint, I suspect the entrypoint is not getting executed because the Dockerfile has instructions to create the wp-content folder in /var/www/html, but it seems like nothing within the entrypoint is getting executed.

I have tried changing my args: to args: ["sh", "-c", "sleep 60 && cp -r /var/www/html/. /shared-wordpress"] in hopes that the container just needed some time to finish running through the entrypoint before I run the cp command, but that has no effect. If you inspect the /shared-wordpress mount in the running wordpress container, it only shows the wp-content folder.

Copying the WordPress files from /usr/src/wordpress works great with the exception that it does NOT include the generated wp-config.php file. I need all the WordPress files along with the generated wp-config.php file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress:5.5.1-php7.4-fpm-alpine
        volumeMounts:
        - name: shared-wordpress
          mountPath: /shared-wordpress
      initContainers:
      - name: init-wordpress
        image: wordpress:5.5.1-php7.4-fpm-alpine
        env:
        - name: WORDPRESS_DB_HOST
          value: test_host
        - name: WORDPRESS_DB_USER
          value: root
        - name: WORDPRESS_DB_PASSWORD
          value: hw8djdl21ds
        - name: WORDPRESS_DB_NAME
          value: local_wordpress
        - name: WORDPRESS_DEBUG
          value: "1"
        - name: WORDPRESS_CONFIG_EXTRA
          value: "define( 'FS_METHOD', 'direct' );"
        args: ["/bin/sh", "-c", "cp -r /var/www/html/. /shared-wordpress"]
        volumeMounts:
        - name: shared-wordpress
          mountPath: /shared-wordpress
      volumes:
      - name: shared-wordpress
        emptyDir: {}
-- Aaron
docker
kubernetes
wordpress

2 Answers

9/23/2020

If you need to Configure a Pod Initialization, check if command format shown in the documentation example could help, when applied to your case:

command:
    - cp
    - "-r"
    - "/var/www/html/."
    - "/shared-wordpress"
-- VonC
Source: StackOverflow

9/23/2020

Your data is not being copied because your docker cmd is overwritten by args yaml field.

Docker entrypoint exec requires php-fpm to be passed as an further argument in order to trigger the code that generates the word press files.

After some testing i find a way to address your question. It is a more workaround than solution but it solves your problem.

So to trigger code generation you have to set the first parameter to php-fpm. In the example below I'm using -v parameter so the programs exist immediately. If you don`t pass any arguments it would start and hold init container from exiting.

initContainers:  
- args:  
   - php-fpm  
   - -v

Now the last part is tho address the issues with copying files. I have removed the cp args and mounted EmptyDir into /var/www/html. Now the files are being generated directly to your desired volume so there no need to copying the files afterwards.

 initContainers:
   volumeMounts:
    - name: shared-wordpress
      mountPath: /var/www/html
-- acid_fuji
Source: StackOverflow