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

8/15/2021

I am trying to initialize my container but I keep on getting directory or file does not exist error on the following script. I essentially need to get config files from repo into the folder.

I am not looking for alternative solutions by placing the exact file, I need this to be a solution for an arbitrary number of files.

Looking into the alpine docker, the /usr/share folder should exist.

This is the initContainer used:

      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

How do I properly move the files into a volume?

Edit 1: I am using the alpine/git image.

-- Zerg Overmind
alpine
docker
kubernetes
persistent-volumes
rancher

2 Answers

8/15/2021

You are cloning the repository content to /usr/repo. So your copy command should be

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

-- Thomas
Source: StackOverflow

8/16/2021

First of all, I recommend using:

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

instead of:

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

Additionally, you shouldn't specify the docker-misp directory as all the contents of the docker-misp.git repository have been cloned to /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

I've prepared an example to illustrate how it works:
NOTE: I used 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

We can check if the server-configs directory has been copied to the 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

As you can see in the example above, everything works as expected.

-- matt_j
Source: StackOverflow