<p>I have a small web application (a Rails app called <code>sofia</code>) that I'm deploying locally with <code>minikube</code>.</p>
<p>When I create the k8s resources and run my deployment, the containers do not contain any of the files that were supposed to be copied over during the image build process.</p>
<p>Here's what I'm doing:</p>
<h2>Dockerfile</h2>
<p>As part of the <code>Dockerfile</code> build, I copy the contents of my local cloned repository into the image working directory:</p>
<pre><code>RUN mkdir -p /app
WORKDIR /app

COPY . ./
</code></pre>
<h2>The (old) <code>docker-compose</code> setup</h2>
<p>Historically I've used a <code>docker-compose</code> file to run this application and all its services. I map my local directory to the container's working directory (see <code>volumes:</code> below). This is a nice convenience when working locally since all changes are reflected &quot;live&quot; inside the container:</p>
<pre class="lang-yaml prettyprint-override"><code># docker-compose.yml
sofia:
    build:
      context: .
      args:
        RAILS_ENV: development
    environment:
      DATABASE_URL: postgres://postgres:sekrit@postgres/
    image: sofia/sofia:local
    ports:
      - # ...
    volumes:
      - .:/app  #&lt;---- HERE
</code></pre>
<h2>Building k8s resource file with <code>kompose</code></h2>
<p>In order to run this on <code>minikube</code>, I use the <a href="https://github.com/kubernetes/kompose" rel="nofollow noreferrer"><code>kompose</code> tool</a> that Kubernetes themselves provide in order to transform my <code>docker-compose</code> file into a k8s resource file that can be consumed.</p>
<pre class="lang-sh prettyprint-override"><code>$ kompose convert --file docker-compose.yml --out k8s.yml --with-kompose-annotation=false
WARN Volume mount on the host &quot;/Users/jeeves/git/jeeves/sofia&quot; isn't supported - ignoring path on the host
INFO Kubernetes file &quot;k8s.yml&quot; created
</code></pre>
<p>As you can see, it generates a warning that my local volume can not be mounted against the remote volume. This makes sense since a k8s deployment runs &quot;remotely&quot;, so I just ignore the warning.</p>
<h2>Running</h2>
<p>Finally I run the above resources with k8s / <code>minikube</code></p>
<pre class="lang-sh prettyprint-override"><code>minikube start
kubectl apply -f k8s.yml
</code></pre>
<p>I notice the <code>sofia</code> container keeps crashing and restarting so I check the logs:</p>
<pre class="lang-sh prettyprint-override"><code>$ kubectl get pods
NAME                             READY   STATUS             RESTARTS   AGE
pod/sofia-6668945bc8-x9267       0/1     CrashLoopBackOff   1          10s
pod/postgres-fc84cbd4b-dqbrh     1/1     Running            0          10s
pod/redis-cbff75fbb-znv88        1/1     Running            0          10s

$ kubectl logs pod/sofia-6668945bc8-x9267
Could not locate Gemfile or .bundle/ directory
</code></pre>
<p>That error is Ruby/Rails specific, but the underlying cause is that there <strong>are no files in the container</strong>! I can confirm this by entering the container and checking files with <code>ls</code> - it is indeed empty.</p>
<h2>Questions</h2>
<ol>
<li>If the <code>sofia/sofia:latest</code> image is correctly built with the <code>COPY</code>-ied file contents, why would it dissapear when runing the container on <code>minikube</code>?</li>
<li>What should I do to ensure my files get copied over correctly?</li>
</ol>
<p>Thanks!</p>

<p>The issue is that Volumes are not behaving the same way in Docker in docker-compose and K8s. Kompose can't perfectly translate volume.
In Docker with docker-compose, your declared volume keep the existing files from the directory, while in k8s, a volume is created empty and shadow the existing content.</p>
<p>There is no direct equivalent of docker-compose volumes that keeps existing files in k8s, you will have to work around that with one of the following options, depending on what makes sense in your use case:</p>
<ul>
<li>leverage <a href="https://kubernetes.io/docs/concepts/storage/volumes/#configmap" rel="nofollow noreferrer">ConfigMaps</a> to add your files to this app volume  (if needed, use subPath). Probably ok for a handfull of config files if that's what lives your app directory when the container starts</li>
<li>in your dockerfile, use COPY to something like app-tmp, then in your entrypoint script copy those files from that app-tmp directory to your &quot;app&quot; volume</li>
<li>Refactor your application so that it uses &quot;app1&quot; directory (without volumes) with existing files, &quot;app2&quot; starts empty and is used as your volume.</li>
</ul>
