Home > front end >  while infinite loop SH does not work as expected on docker startup
while infinite loop SH does not work as expected on docker startup

Time:12-03

I have sh code (DashBoardImport.sh) like down below. It checks apı response to import a kibana dashboard in a infinite loop, If it gets a reponse with success, it breaks the loop :

#!/bin/sh
# use while loop to check if kibana is running 
while true
do
    response=$(curl -X POST elk:5601/api/saved_objects/_import -H "kbn-xsrf: true" --form file=@/etc/elasticsearch/CityCountDashBoard.ndjson | grep -oE "^\{\"success")
    #curl -X GET elk:9200/git-demo-topic | grep -oE "^\{\"git"  > /dev/null
    #match=$?
    echo $response
    if [ '{"success' = $response ]
        then
            echo "Running import dashboard.."
            #curl -X POST elk:5601/api/saved_objects/_import -H "kbn-xsrf: true" --form file=@/etc/elasticsearch/CityCountDashBoard.ndjson
            break
        else
            echo "Kibana is not running yet"
            sleep 5
    fi
done
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

I run DashBoardImport.sh via docker file:

ADD  ./CityCountDashBoard.ndjson /etc/elasticsearch/CityCountDashBoard.ndjson
ADD  ./DashBoardImport.sh /etc/elasticsearch/DashBoardImport.sh
#ENTRYPOINT /etc/elasticsearch/DashBoardImport.sh &  

USER root

RUN chmod  x /etc/elasticsearch/DashBoardImport.sh
#RUN /etc/elasticsearch/DashBoardImport.sh &
RUN nohup bash -c "/etc/elasticsearch/DashBoardImport.sh" >/dev/null 2>&1 &
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

I tried many options as you can see commented out. The sh works perfectly when I run it manually on the Docker Container. I kill the kibana service. then run the code. after I started the kibana, code succesfully workes as expected and imports the dashboard. But It does not work when it start on container automatically.

Do you have any idea?

Thanks alot in advance :)

CodePudding user response:

A RUN step executes in a temporary container until the command returns and then docker captures the changes to the filesystem as a new layer in your image. Nothing else remains, no environment variables, running processes, etc, only the filesystem changes.

So when you RUN nohup ... & that process immediately returns since it's in the background (nohup ... & explicitly does that), and so the container exits, killing any processes that were running in the container, and captures the filesystem changes made, if any, to your image.

If you want something to run when you start the container, add it to your ENTRYPOINT or CMD.

  • Related