<p>In order to do ease development in Docker, the code is attached to the containers through volumes. In that way, there is no need to rebuild the images each time the code is changed.</p>

<p>So, is it correct to think to use the same idea in Kubernetes?</p>

<p><strong>PS:</strong> I know that the concepts <code>PersistentVolume</code> and <code>PersistentVolumeClaim</code> allow to attach volume, but they are intended for data.</p>

<hr>

<h1>Update</h1>

<p>To ease the development, I do need to use the volume for both code and data. This will avoid me to rebuild the images at each change of code. </p>

<p>Below this is what I am trying to do in minikube:</p>

<p><strong>the deployment</strong></p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: php-hostpath
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: php-hostpath
    spec:
      containers:
      - name: php-hostpath
        image: php:7.0-apache
        ports:
        - containerPort: 80
        volumeMounts:
          - name: vol-php-hostpath
            mountPath: /var/www/html
      volumes:
      - name: vol-php-hostpath
        hostPath:
          path: '/home/amine/DockerProjects/gcloud-kubernetes/application/06-hostPath-volume-example-minikube/src/'
</code></pre>

<p><strong>the service</strong></p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: php-hostpath
  namespace: default
  labels:
    app: php-hostpath
spec:
  selector:
    app: php-hostpath
  ports:
  - port: 80
    targetPort: 80
  type: "LoadBalancer"
</code></pre>

<p>The service and the deployment are well created in minikube:</p>

<pre><code>$ kubectl get pods -l app=php-hostpath
NAME                            READY     STATUS    RESTARTS   AGE
php-hostpath-3796606162-bt94w   1/1       Running   0          19m

$ kubectl get service -l app=php-hostpath
NAME           CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
php-hostpath   10.0.0.110   &lt;pending&gt;     80:30135/TCP   27m
</code></pre>

<p>The folder <code>src</code> and the file <code>src/index.php</code> are also well created.</p>

<pre><code>&lt;?php
echo "This is my first docker project";
</code></pre>

<p>Now I want to check that every thing is running:</p>

<pre><code>$ kubectl exec -ti php-hostpath-3796606162-bt94w bash
root@php-hostpath-3796606162-bt94w:/var/www/html# ls
root@php-hostpath-3796606162-bt94w:/var/www/html# exit
exit
</code></pre>

<p>The folder <code>src</code> and the file <code>index.php</code> are not in <code>/var/www/html</code>!</p>

<p>Have I missed something?</p>

<p>PS: if I were in production env, I will not put my code in a volume.
Thanks,</p>

<p>Based on this <a href="https://github.com/kubernetes/minikube/blob/master/README.md" rel="nofollow noreferrer">doc</a>, Host folder sharing is not implemented in the KVM driver yet. This is the driver I am using actually.</p>

<p>To overcome this, there are 2 solutions:</p>

<ul>
<li><p>Use the <strong>virtualbox</strong> driver so that you can mount your hostPath volume by changing the path on you localhost <code>/home/THE_USR/...</code> to <code>/hosthome/THE_USR/...</code> </p></li>
<li><p>Mount your volume to the minikube VM based on the command <code>$ minikube mount /home/THE_USR/...</code>. The command will return you the path of your mounted volume on the minikube VM. Example is given down.</p></li>
</ul>

<h1>Example</h1>

<p><strong>(a) mounting a volume on the minikube VM</strong></p>

<p>the <code>minikube mount</code> command returned that path /mount-9p</p>

<pre><code>$ minikube mount -v 3 /home/amine/DockerProjects/gcloud-kubernetes/application/06-hostPath-volume-example-minikube
Mounting /home/amine/DockerProjects/gcloud-kubernetes/application/06-hostPath-volume-example-minikube into /mount-9p on the minikubeVM
This daemon process needs to stay alive for the mount to still be accessible...
2017/03/31 06:42:27 connected
2017/03/31 06:42:27 &gt;&gt;&gt; 192.168.42.241:34012 Tversion tag 65535 msize 8192 version '9P2000.L'
2017/03/31 06:42:27 &lt;&lt;&lt; 192.168.42.241:34012 Rversion tag 65535 msize 8192 version '9P2000'
</code></pre>

<p><strong>(b) Specification of the path on the deployment</strong></p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: php-hostpath
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: php-hostpath
    spec:
      containers:
      - name: php-hostpath
        image: php:7.0-apache
        ports:
        - containerPort: 80
        volumeMounts:
          - name: vol-php-hostpath
            mountPath: /var/www/html
      volumes:
      - name: vol-php-hostpath
        hostPath:
          path: /mount-9p
</code></pre>

<p><strong>(c) Checking if mounting the volume worked well</strong></p>

<pre><code>amine@amine-Inspiron-N5110:~/DockerProjects/gcloud-kubernetes/application/06-hostPath-volume-example-minikube$ kubectl exec -ti php-hostpath-3498998593-6mxsn bash
root@php-hostpath-3498998593-6mxsn:/var/www/html# cat index.php 
&lt;?php
echo "This is my first docker project";
root@php-hostpath-3498998593-6mxsn:/var/www/html# cat index.php                                                                                                                                 
&lt;?php

echo 'This is my first hostPath on kubernetes';
root@php-hostpath-3498998593-6mxsn:/var/www/html# cat index.php 
&lt;?php

echo 'This is my first hostPath on kubernetes';
root@php-hostpath-3498998593-6mxsn:/var/www/html# 
</code></pre>

<p><strong>PS:</strong> this kind of volume mounting is only development environment. If I were in production environment, the code will not be mounted: it will be in the image.</p>

<p><strong>PS:</strong> I recommend the virtualbox in stead of KVM.</p>

<p>Hope it helps others.</p>
