简体   繁体   English

无法从kubernetes容器内运行的docker写入文件

[英]Unable to write file from docker run inside a kubernetes pod

I have a docker image that uses a volume to write files: 我有一个使用卷写入文件的docker映像:

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. 当我尝试在Pod内运行此容器时,容器正常退出,但未写入任何文件。

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: 如果找不到卷,则容器抛出错误,例如,如果我在不带-v选项的情况下运行该卷,则会抛出错误:

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. 我对Kubernetes还是很陌生,但这让我发疯了。

Does kubernetes prevent files from being written? kubernetes是否会阻止文件写入? or am I missing something obvious? 还是我缺少明显的东西?

The whole Kubernetes context is managed by GCP composer-airflow, if it helps... 整个Kubernetes上下文由GCP composer-airflow管理,如果有帮助...

docker -v: Docker version 17.03.2-ce, build f5ec1e2

If you want to have that behavior in Kubernetes you can use a hostPath volume. 如果要在Kubernetes中具有该行为,可以使用hostPath卷。

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. 本质上,您可以在Pod规范中指定它,然后将卷安装在Pod运行所在的节点上,然后在Pod退出后该文件应位于该节点中。

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

when I try to run this inside a pod the container exit normally but no file is written 当我尝试在Pod内运行此容器时,容器正常退出,但未写入任何文件

First of all, there is no need to run the docker run command inside the pod :). 首先,不需要在pod :)中运行docker run命令。 A spec file (yaml) should be written for the pod and kubernetes will run the container in the pod using docker for you. 应该为Pod编写一个规范文件 (yaml),并且kubernetes将使用docker在docker中运行容器中的容器。 Ideally, you don't need to run docker commands when using kubernetes (unless you are debugging docker-related issues). 理想情况下,你不需要跑docker使用kubernetes时(除非你正在调试码头工人有关的问题)的命令。

This link has useful kubectl commands for docker users. 该链接对码头用户具有有用的kubectl命令。

If you are used to docker-compose , refer Kompose to go from docker-compose to kubernetes: 如果您习惯于Kompose docker-compose ,请参考Kompose从docker docker-compose转到kubernetes:

Some options to mount a directory on the host as a volume inside the container in kubernetes: 在kubernetes中将目录作为卷安装在主机上的一些选项:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM