Home > other >  Docker init container mounting existing files issue
Docker init container mounting existing files issue

Time:01-28

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?

CodePudding user response:

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"]
    ...

CodePudding user response:

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
  •  Tags:  
  • Related