Question

Why my Root Directory is empty when my docker image is deployed on a managed Kubernetes cluster?

Posted February 12, 2020 126 views
ApachePHP

Hi. I have created a local docker image and deployed it to a managed kubernetes cluster. I have tested the image locally and works. But when I deploy to the kubernetes cluster I am getting an error from Apache that the Root Directory is empty. I know that the MySQL server installation is missing. At tis step I wish to make sure that the pho files are correctly deployed.

This is my Dockerfile:

FROM php:7.4-apache
COPY wordpress/ /var/www/html
RUN chmod -R 755 /var/www/html
EXPOSE 80

And these are my YAML files to setup the kubernetes cluster:

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: do-block-storage
  resources:
    requests:
      storage: 1Gi #put here your required storage capacity
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: ......my Docker Hub Image.....
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html #default root path for webserver
      imagePullSecrets:
        - name: docker-hub-cred #credentials to sync with Docker Hub & pull the image          
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pv-claim

I have connected to the local docker container and executed the ls command in the root directory and I can see the wordpress files, but when doing the same on the deployed pod, I am getting an empty root directory.

Any suggestions what is wrong?

edited by MattIPv4

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
4 answers

Hello,

This actually looks correct. Have you tried checking your pod logs?

kubectl logs -f pod_name -c container_name

Also, I can see that you are setting your PVC storage to 1GB, is it possible that your /var/www/html folder is larger than that? Have you tried setting the size to 5GB, for example, depending of course on the size of the actual folder inside your container?

Regards,
Bobby

Basically the log file messages are of the following categories:

  • Could not reliably determine the server’s fully qualified domain name,
  • No matching DirectoryIndex (index.php,index.html) found, and server-generated directory index forbidden by Options directive (which makes sense as the Root Directory is empty).

Though the www folder size is much much less than 1GB, I also tried with 5GB capacity. I still have the same issues.

After consultation with the DO support team I think that what is causing the problem is that first the Docker image is deployed and then the persistent volume is claimed and the fs is mounted with an empt dir and therefore, replacing the current files.

I put this option in the YAML file for the deployment case:

        lifecycle:
          postStart:
            exec:
              command:  ["/bin/sh", "-c", "touch /var/www/html/index.html"] 

After this addition, I am not getting a 403 error, but the empty index.html page that is created.

So now the question is how can I prevent the pv claim from deleting my docker image files.

I managed to solve the issue by following these steps:

1) Added the following in my Dockerfile:

RUN mkdir ~/initial-deployment
RUN chmod -R 755 ~/initial-deployment

2) Added the following section in the YAML file (Deployment section), in spec > container:

lifecycle:
  postStart:
    exec:
      command:  ["/bin/sh", "-c", "cp -R ~/initial-deployment/* /var/www/html; chmod -R 755 /var/www/html; rm -rf ~/initial-deployment"]
Submit an Answer