-3

We had build a docker image that contains an existing file inside. /opt/app/agent/oneagent. When I test the docker image individually, i can see the file inside the directory.

However, when I use it as an initcontainer to mount that directory to the main container, it does not see the directory.

apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
    volumeMounts:
    - name: workdir
      mountPath: /data
  # These containers are run during pod initialization
  initContainers:
  - name: dynainit
    image: localhost:5000/dyna1
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh"]
    args: ["ls -la /opt/app/agent/oneagent"]
    volumeMounts:
    - name: workdir
      mountPath: "/data"
  dnsPolicy: Default
  volumes:
  - name: workdir
    emptyDir: {}

the init container logs it shows /bin/sh: can't open 'ls -la /opt/app/agent/oneagent': No such file or directory

What i really want to do is mount /opt/app/agent/oneagent from the init container to the main container.

Is that possible to mount existing files or init containers can only work by downloading the file in the beginning?

chocokoala
  • 191
  • 4
  • 15
  • If you want the main container to have some binary, the best way to do that is to install it in its image's Dockerfile. To get around your immediate problem you might consider breaking up your command into words, rather than trying to involve a shell; `command: ['ls', '-la', '/opt/app/agent/oneagent']`. I don't think an init container or a volume is the right way to install a binary, though. – David Maze Jan 29 '22 at 02:29

2 Answers2

3

You can copy files from your init container /opt/app/agent/oneagent to workdir volume. When your main container mount workdir it will see the files copied. The error in your question is not volume related, just add -c to your command will do.

    ...
    command: ["/bin/sh","-c"]
    args: ["ls -la /opt/app/agent/oneagent"]
    ...
gohm'c
  • 13,492
  • 1
  • 9
  • 16
  • yes, thanks. I got it working by copying a /bin/sh -c command and it worked. It's volume related in a sense where if the files can be seen, then i can look into copying the files to the correct dest. i cannot mount the init volume directly to the main containers using the same path without copying. – chocokoala Jan 27 '22 at 15:48
0

I got it working using "-c" inside the command key.

apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  initContainers:
  - name: dynainit
    image: localhost:5000/dyna1
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh", "-c"]
    args: ["cp -R /opt/app/agent /data"]
    volumeMounts:
    - name: workdir
      mountPath: "/data"
  dnsPolicy: Default
  volumes:
  - name: workdir
    emptyDir: {}
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
    volumeMounts:
    - name: workdir
      mountPath: /opt/app
chocokoala
  • 191
  • 4
  • 15