Dockerfile & Kubernetes – Fix COPY Function Not Working in Pod Deployment

dockerkubernetes

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

Best Answer

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