"Directory or file does not exist" 在 kubernetes 上的 alpine docker 中克隆 repo 和移动文件时

"Directory or file does not exist" while cloning repo and moving files in alpine docker on kubernetes

我正在尝试初始化我的容器,但我在以下脚本中不断收到 directory or file does not exist 错误。我基本上需要从 repo 中获取配置文件到文件夹中。

我不是通过放置确切的文件来寻找替代解决方案,我需要这是一个针对任意数量文件的解决方案。

查看 alpine docker,/usr/share 文件夹应该存在。

这是initContainer使用的:

      initContainers:
        - name: init-config
          image: alpine/git
          command:
            - "git clone https://github.com/coolacid/docker-misp.git /usr/repo && cp -a /usr/repo/docker-misp/server-configs/ /usr/share"
          volumeMounts:
            - mountPath: /usr/share
              name: misp-app-config

如何正确地将文件移动到卷中?

编辑 1:我正在使用 alpine/git 图片。

您正在将存储库内容克隆到 /usr/repo。 所以你的复制命令应该是

cp -a /usr/repo/server-configs/ /usr/share

首先我推荐使用:

command: ["/bin/sh","-c"]
args: ["git clone https://github.com/coolacid/docker-misp.git /usr/repo && cp -a /usr/repo/server-configs/ /usr/share"]

而不是:

command:
            - "git clone https://github.com/coolacid/docker-misp.git /usr/repo && cp -a /usr/repo/docker-misp/server-configs/ /usr/share"
        

此外,您不应指定 docker-misp 目录,因为 docker-misp.git 存储库的所有内容都已克隆到 /usr/repo:

# git clone https://github.com/coolacid/docker-misp.git /usr/repo
Cloning into '/usr/repo'...
remote: Enumerating objects: 905, done.
remote: Counting objects: 100% (160/160), done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 905 (delta 144), reused 136 (delta 136), pack-reused 745
Receiving objects: 100% (905/905), 152.64 KiB | 2.50 MiB/s, done.
Resolving deltas: 100% (444/444), done.

# ls /usr/repo
LICENSE  README.md  build-docker-compose.yml  docker-compose.yml  examples  modules  server  server-configs

我准备了一个例子来说明它是如何工作的:
注意: 我使用了 emptyDir but you probably want to use a different type of volumes

$ cat app-1.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: app-1
  name: app-1
spec:
  initContainers:
    - name: init-config
      image: alpine/git
      command: ["/bin/sh","-c"]
      args: ["git clone https://github.com/coolacid/docker-misp.git /usr/repo && cp -a /usr/repo/server-configs/ /usr/share"]
      volumeMounts:
        - mountPath: /usr/share
          name: misp-app-config
  containers:
  - image: nginx
    name: web-1
    volumeMounts:
    - mountPath: "/work-dir"
      name: misp-app-config
  volumes:
  - name: misp-app-config
    emptyDir: {}
    
$ kubectl apply -f app-1.yml
pod/app-1 created

$ kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
app-1   1/1     Running   0          11s

我们可以检查 server-configs 目录是否已复制到 web-1 Pod:

$ kubectl exec -it app-1 -- sh
Defaulted container "web-1" out of: web-1, init-config (init)
# ls /work-dir
server-configs
# ls /work-dir/server-configs
email.php

如您在上面的示例中所见,一切都按预期进行。