2

I have a docker image that uses a volume to write files:

docker run --rm -v /home/dir:/out/ image:cli args

when I try to run this inside a pod the container exit normally but no file is written.

I don't get it.

The container throw errors if it does not find the volume, for example if I run it without the -v option it throws:

Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path '/out/file.txt'.

But I don't have any error from the container. It finishes like it wrote files, but files do not exist.

I'm quite new to Kubernetes but this is getting me crazy.

Does kubernetes prevent files from being written? or am I missing something obvious?

The whole Kubernetes context is managed by GCP composer-airflow, if it helps...

docker -v: Docker version 17.03.2-ce, build f5ec1e2
Rico
  • 58,485
  • 12
  • 111
  • 141
donkino
  • 110
  • 1
  • 9
  • hey! so you are running a docker container within a container because pod itself is a container. One thing to make sure he is that, where is the "/home/dir" is it outside your pod or otherwise. then only it can be mounted. – Narendra Nov 15 '18 at 22:20
  • Yeah, that's docker in docker. The /home/dir is inside the pod. Here the pod is the container host right? – donkino Nov 15 '18 at 22:25
  • so is your pod (outer docker) always running. or it exits as well. – Narendra Nov 15 '18 at 22:28
  • Standard Kubernetes does _not_ run Docker-in-Docker (that's generally discouraged); and the Pod is a separate layer from either the host or the container proper. You need to create a volume in the Pod and mount it in the container. This workload doesn't seem like a great match for Kubernetes as you've described it, though. – David Maze Nov 15 '18 at 22:54
  • Thx @DavidMaze . About kubernetes I does not have much of choice, this problem is part of a workflow inside composer-airflow, which run in kubernetes. And our workflow consist in a serie of commands run on different docker containers, each using files generated by the precedent. So I'll give a try to the volume in the pod mounted in the container. Still Idon't get why once inside the pod (and staying in the pod) the files are not created without the container crashing.. (i'll paste an example, it starts to be a lot of containers haha) – donkino Nov 15 '18 at 23:33
  • Kubernetes doesn't expect containers in a Pod to exit; it will restart them, and if it has to do it more than a couple of times, you'll start seeing CrashLoopBackOff state. – David Maze Nov 15 '18 at 23:47
  • For anyone who runs into this as well... it seems like when you have docker in docker set up on a Kubernetes deployment/pod, if you run e.g. `docker -it --rm -v $(pwd):/opt/src python`, the volume mount does nothing, in this new container there's nothing in `/opt/src`. Didn't see many people discussing about this, can't find anywhere else to confirm this, and this SO seems like the closest, but still great if someone can confirm if the `-v` become useless for docker-in-docker on k8s? – Shawn Jun 29 '21 at 11:17

2 Answers2

1

If you want to have that behavior in Kubernetes you can use a hostPath volume.

Essentially you specify it in your pod spec and then the volume is mounted on the node where your pod runs and then the file should be there in the node after the pod exits.

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: image:cli
    name: test-container
    volumeMounts:
    - mountPath: /home/dir
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /out
      type: Directory
Rico
  • 58,485
  • 12
  • 111
  • 141
  • Thx, I'll try that. But problem is the file doesn't exist while the pod is alive. I mean I enter the pod (via kubectl exec - it pod - - bin/bash), run the container, and no files... – donkino Nov 15 '18 at 22:51
  • You may not be able to write to `/out` – Rico Nov 15 '18 at 22:54
  • Hey! I was using an `emptyDir` to mount `/home/dir`. I changed that to an hostPath and it worked! Thanks so much :) – donkino Nov 16 '18 at 14:57
1

when I try to run this inside a pod the container exit normally but no file is written

First of all, there is no need to run the docker run command inside the pod :). A spec file (yaml) should be written for the pod and kubernetes will run the container in the pod using docker for you. Ideally, you don't need to run docker commands when using kubernetes (unless you are debugging docker-related issues).

This link has useful kubectl commands for docker users.

If you are used to docker-compose, refer Kompose to go from docker-compose to kubernetes:

Some options to mount a directory on the host as a volume inside the container in kubernetes:

Vikram Hosakote
  • 3,528
  • 12
  • 23