1

So I am running a local kubernetes cluster on my macOS using Docker for Desktop.

I am setting up a Kubernetes cluster that I'd like to test locally, but I have some issues copying other my local folder to the source image.

My Dockerfile is pretty darn simple:

FROM wordpress:latest

WORKDIR /var/www/html

# Copy wp-content
COPY ./wp-content/ ./wp-content/ # I don't find the /wp-content/ in my container/image/pod once deployed, but I do find it if I run just the docker image

In the directory where my Dockerfile belongs, I have a wp-content folder, that I would expect to see on the container once it's been built.

When I run docker build -t johndoe/wordpress:<someUniqueVersion> and then docker push johndoe/wordpress:<versionTag> I see the repository, I also see that it's quite bigger than the official WordPress image (hence some files must've been copied over).

However, when update my Kubernetes deployment yaml config file to use the specific version I just pushed, and then run kubectl apply wordpress-deployment.yaml, I see the POD restarting as well as using the correct Docker Image commit (kubectl get pods <podname> -o jsonpath="{..imageID}"), BUT the files are not present if I do kubectl exec <podname> -it sh and look in the files. All the other WordPress files (from the native WordPress image) is there, but the files I told Docker to specifically copy over is not present.

I have also tried to do a find / -name "myfile" on the whole file system, in case the file was copied over somewhere else, but it's simply not on the image.

The docker build finishes without issues:

[+] Building 27.7s (9/9) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                     0.0s
 => => transferring dockerfile: 380B                                                                                                                                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                        0.0s
 => => transferring context: 2B                                                                                                                                                                                                                          0.0s
 => [internal] load metadata for docker.io/library/wordpress:latest                                                                                                                                                                                      0.0s
 => [1/3] FROM docker.io/library/wordpress:latest                                                                                                                                                                                                        0.0s
 => [internal] load build context                                                                                                                                                                                                                        8.9s
 => => transferring context: 190.57MB                                                                                                                                                                                                                    8.8s
 => CACHED [2/3] WORKDIR /var/www/html                                                                                                                                                                                                                   0.0s
 => [3/3] COPY ./wp-content ./                                                                                                                                                                                                                           8.7s
 => exporting to image                                                                                                                                                                                                                                   9.3s
 => => exporting layers                                                                                                                                                                                                                                  9.3s
 => => writing image sha256:7b567cfc8609661edfcc6495df25ba448c9b85957av3692184beca3e6h9ce8c7                                                                                                                                                             0.0s
 => => naming to docker.io/johndone/wordpress:v1.7

Things I have also tried:

  • Deleting the PVC/PV volume claim, in case that could be an issue
  • Tried different variations of the COPY (e.g COPY ./wp-content/ ./wp-content/, COPY ./wp-content/ /var/www/html/wp-content)
  • Tried copying over a single test file like so: COPY ./test /var/www/html however, that does not appear either.
  • Ensured directory is not present in .dockerignore. I even tried renaming the file completely to ensure it wouldn't be the issue.

Update

My wordpress-deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      component: wordpress
  template: 
    metadata:
      labels: 
        component: wordpress
    spec:
      volumes:
        - name: wordpress-storage
          persistentVolumeClaim:
            claimName: wordpress-persistent-volume-claim
      containers:
        - name: wordpress
          image: myusername/wordpress:v1.7.2
          ports:
            - containerPort: 8000
          env:
            - name: WORDPRESS_DB_HOST
              value: db-cluster-ip-service:3306
            - name: WORDPRESS_DB_USER
              value: wordpress
            - name: WORDPRESS_DB_NAME
              value: thedatabase
            - name: WORDPRESS_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: dbpassword
                  key: MYSQL_PASSWORD
            - name: WORDPRESS_DEBUG
              value: "1"
            - name: WORDPRESS_TABLE_PREFIX
              value: wp_
          volumeMounts:
            - name: wordpress-storage
              mountPath: /var/www/html
              subPath: wordpress
      imagePullSecrets:
        - name: regcred

My WordPress persistent volume claim file:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-persistent-volume-claim
spec:
  accessModes: 
    - ReadWriteMany
  resources:
    requests:
      storage: 6Gi
4
  • 1
    If you run the docker image without Kubernetes does the files are also missing? Can you update you deployment/pod yaml into the question? – thomas Nov 17 '20 at 14:17
  • what happens if you use the absolute path in the COPY step? (COPY wp-content/ /var/www/html/). – Patrick W Nov 19 '20 at 1:36
  • @thomas I just tried building locally, just with docker, and there the files are where they are supposed to be. – FooBar Nov 19 '20 at 16:53
  • @PatrickW Yup, that was a mistake on my end. Updated my question to reflect. – FooBar Nov 19 '20 at 16:54
0

The problem here is that you are replacing your html directory (where the files you copied over are) with your pvc. The pvc does.not contain these new files so they won't be present

0

I tested this with docker 19.03.13 and used your dockerfile and copy works fine for me. However I did receive an error while building the image:

When using COPY with more than one source file, the destination must be a directory and end with a /

I solved this with deleting / and leaving the . as destination so you might want experiment with destination path and check other possibilities (I was testing this on docker on linux machine).

Since you mention PV/PVC in the question I suspect and it is most probable cause for this, that your are mounting volume in the place whereas the copied files should be located. So your PVC lies in the place where you want the files to be.

Having said that, we don't see the full picture here since we can`t see your yaml files.

2
  • thanks for your time @thomas. I have updated my answer with the original answer. If I run docker locally, it works fine. So the issue must be with PVC as you say. But, then again, isn't the PVC supposed to allow updates? E.g. after code changes locally, I would create a new image with the current code, push this image with a new version, and then have Kubernetes pull the new image, and file changes should reflect? – FooBar Nov 19 '20 at 16:57
  • @FooBar, I can`t quite grasp what you mean exactly when you say that PVC suppose to allow updates. In the comment example above you push new image and ask kubernetes to recreate the pod, so kubernetes goes again thru your manifest, create a pod and mounts again the volume in the place where your expect your files to be. PV are design to persist the data beyond the pod lifecycle but the data must be there in the first place. Since you already accepted the answer with solution provided by I would appreciate at least upvote :) – thomas Nov 20 '20 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.