dockerkubernetesdebian

How to pass multiple commands in Kubernetes container?


I'm trying to deploy a container in Kubernetes and at the same time copy a file from one of the persistent volumes to specific directory. The problem is that if i run the command without delay it will try to copy the file before mounting the persistent volume. I'd like to help me with the syntax to delay the copy command few seconds until the persistent volume is mounted.

I tried to type the command field like that but the container crashes while deploying the pod.

    spec:
      containers:
      - image: andreask81/syspass:latest
        name: syspass-app
        command: ["/bin/bash"]
        args: ["sleep 30", "cp /var/www/html/sysPass/config/backup/config.xml /var/www/html/sysPass/config/config.xml"]

And this one also did not work and caused the pod to crash right after container creation. command: ["/bin/bash","-c","sleep 10 && cp /var/www/html/sysPass/config/backup/config.xml /var/www/html/sysPass/config/config.xml"]

In the meanwhile, when I run the following command inside similar container directly in the shell it works. /bin/bash -c "sleep 1" && cp /var/www/html/sysPass/config/backup/config.xml /var/www/html/sysPass/config/config.xml

Any idea how can I format the "command:" field in the YAML file to make this work?

***** Update 1 ***** I've updated the container to include a script that does the required steps as the following

    #!/bin/bash
    
    while [ ! -f /var/www/html/sysPass/config/backup/config.xml ]; do
        sleep 10;
    done
    
    cp /var/www/html/sysPass/config/backup/config.xml /var/www/html/sysPass/config/config.xml
    chown www-data:www-data /var/www/html/sysPass/config/config.xml
    chmod 664 /var/www/html/sysPass/config/config.xml

The pod fails with the following errors:

/tmp/script.sh: line 2: $'\r': command not found
/tmp/script.sh: line 10: syntax error: unexpected end of file

I also tried to manually run the script from command line but it failed with the same errors.


Solution

  • You could update your container to contain a script that performs this work for you before starting the main app with something like:

    script.sh

    #!/usr/bin/env bash
    
    while [ ! -f /path/to/some/file ]; do
        sleep 3;
    done
    

    Make it executable.

    chmod u+x script.sh
    

    Add the script to your docker image and set it as the first thing to run in the ENTRYPOINT - this defers the file checking until run-time.

    FROM some-image:0.0.1
    ....
    COPY script.sh /tmp/script.sh
    ENTRYPOINT /bin/bash /tmp/script.sh && <THE_REST_OF_YOUR_COMMAND_HERE_FOR_MAIN_APPLICATION>
    

    Test your new image runs locally with docker run