How can I run script automatically after Docker container startup

The image itself has an entrypoint ENTRYPOINT ["/run/"] specified in the Dockerfile. You can replace it by your own script. So for example create a new script, mount it and first call /run/ and then wait for start of elasticsearch before running your

Not sure this will solves your problem, but its worth check my repo'sDockerfile

I have created a simple file copied to docker image and in the Dockerfile I wrote CMD [""]. In the same way define whatever you want in and write CMD [""]. You can find another example like below


FROM java:8

RUN apt-get update && apt-get install stress-ng -y 
ADD target/restapp.jar /restapp.jar 
COPY /usr/local/bin/ 
RUN chmod +x /usr/local/bin/ 
CMD [""]

java -Dserver.port=8095 -jar /restapp.jar &
hostname="hostname: `hostname`"
nohup stress-ng --vm 4 &
while true; do
  sleep 1000

I was trying to solve the exact problem. Here's the approach that worked for me.

  1. Create a separate shell script that checks for ES status, and only start initialization of SG when ES is ready:

Shell Script


echo ">>>>  Right before SG initialization <<<<"
# use while loop to check if elasticsearch is running 
while true
    netstat -uplnt | grep :9300 | grep LISTEN > /dev/null
    if [ 0 = $verifier ]
            echo "Running search guard plugin initialization"
            /elasticsearch/plugins/search-guard-6/tools/ -h -cd plugins/search-guard-6/sgconfig -icl -key config/client.key -cert config/client.pem -cacert config/root-ca.pem -nhnv
            echo "ES is not running yet"
            sleep 5

Install script in Dockerfile

You will need to install the script in container so it's accessible after it starts.

RUN chmod +x /

Update entrypoint script

You will need to edit the entrypoint script or run script of your ES image. So that it starts the in the background BEFORE starting ES process.

# Run sginit in background waiting for ES to start
/ &

This way the will start in the background, and will only initialize SG after ES is started.

The reason to have this script starts before ES in the background is so that it's not blocking ES from starting. The same logic applies if you put it after starting of ES, it will never run unless you put the starting of ES in the background.