skip to Main Content

Bash script:

clonePath=/data/config/
git branch -r | fgrep -v 'origin/HEAD' | sed 's|  origin/|git checkout |' > checkoutAllBranches.sh
chmod +x checkoutAllBranches.sh
echo "Fetch branch: `cat checkoutAllBranches.sh`"
./checkoutAllBranches.sh
git checkout master
git remote rm origin
rm checkoutAllBranches.sh

for config_dir in `ls -a`; do
  cp -r $config_dir $clonePath/;
done

echo "API Config update complete..."

Dockerfile which issues this script execution

ENTRYPOINT ["sh","config-update-force.sh","|| true"]

The error below causes the container startup failure despite setting the command status to 0 manually using || true

ERROR:

Error:

cp: cannot create regular file '/data/./.git/objects/pack/pack-27a9d...fb5e368e4cf.pack': Permission denied

cp: cannot create regular file '/data/./.git/objects/pack/pack-27a9d...fbae25e368e4cf.idx': Permission denied

I am looking for 2 options here:

  1. Change these file permissions and then store them in the remote with rwx permissions

  2. Do something to the docker file to ignore this script failure error and start the container.

DOCKERFILE:

FROM docker.hub.com/java11-temurin:latest
USER root
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get install -y rsync telnet vim wget git
RUN mkdir -p /opt/config/clone/data
RUN chown -R 1001:1001 /opt/config
USER 1001
ADD build/libs/my-api-config-server.jar .
ADD config-update-force.sh .
USER root
RUN chmod +x config-update-force.sh
USER 1001
EXPOSE 8080
CMD java $BASE_JAVA_OPTS $JAVA_OPTS -jar my-api-config-server.jar
ENTRYPOINT ["sh","config-update-force.sh","|| true"]

BASH SCRIPT:

#!/bin/bash

set +e
set +x

clonePath=/opt/clone/data/data

#source Optumfile.properties
echo "properties loaded: example ${git_host}"

if [ -d my-api-config ]; then
  rm -rf my-api-config;
  echo "existing my-api-config dir deleted..."
fi

git_url=https://github.com/my-api-config-server
git clone https://github.com/my-api-config-server
cd my-api-config-server
git branch -r | fgrep -v 'origin/HEAD' | sed 's|  origin/|git checkout |' > checkoutAllBranches.sh
chmod +x checkoutAllBranches.sh
echo "Fetch branch: `cat checkoutAllBranches.sh`"
./checkoutAllBranches.sh
git checkout master
git remote rm origin
rm checkoutAllBranches.sh

for config_dir in `ls -a`; do
  cp -r $config_dir $clonePath/;
done

echo "My API Config update complete..."

2

Answers


  1. When you do in the script…

    chmod +x checkoutAllBranches.sh
    

    …than why not before cp

    chmod -R +rwx ${clonePath}
    

    …or if the stderr message ‘wont impact anything’…

    cp -r $config_dir $clonePath/ 2>/dev/null;
    

    …even cp dont copy -verbosly.
    ?

    Login or Signup to reply.
  2. When your Dockerfile declares an ENTRYPOINT, that command is the only thing the container does. If it also declares a CMD, the CMD is passed as additional arguments to the ENTRYPOINT; it is not run on its own unless the ENTRYPOINT makes sure to execute it.

    Shell errors are not normally fatal, and especially if you explicitly set +e, even if a shell command fails the shell script will keep running. You see this in your output where you get multiple cp errors; the first error does not terminate the script.

    You need to do two things here. The first is to set the ENTRYPOINT to actually run the CMD; the simplest and most common way to do this is to end the script with

    exec "$@"
    

    The second is to remove the || true from the Dockerfile. As you have it written out currently, this is passed as the first argument to the entrypoint wrapper – it is not run through a shell and it is not interpreted as a "or" operator. If your script begins with a "shebang" line and is marked executable (both of these are correct in the question) the you do not explicitly need the sh interpreter.

    # must be a JSON array; no additional "|| true" argument; no sh -c wrapper
    ENTRYPOINT ["./config-update-force.sh"]
    
    # any valid CMD will work with `exec "$@"
    CMD java $BASE_JAVA_OPTS $JAVA_OPTS -jar my-api-config-server.jar
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search