Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

583
Views
How to use docker ENTRYPOINT to exec java and then exec touch once java process completes

I am trying to run a pod with 2 containers. Main container executes a docker java image which runs the cron job and the other sidecar(logstash) container reads the log and ship it to the Kibana server. As main container completes once the cron job is finished but sidecar container keeps running as it doesn't gracefully shutdown. I tried https://github.com/karlkfi/kubexit and preStop container lifecycle hook but didn't work. Now, I want to try to stop sidecar container using dockerfile. Docker ENTRYPOINT exec java image jar and once java process is finished then I wanted to create / touch a file inside the main container which my other sidecar container looks for that touched file.

This is my current dockerfile

FROM mcr.microsoft.com/java/jre:11-zulu-alpine
ARG projectVersion
WORKDIR /opt/example/apps/sync
COPY /target/sync-app-${projectVersion}.jar app.jar
ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar

I wanted to add following once the java process execution completes

echo "Going to kill sync-app container..."
trap 'touch /opt/example/apps/sync/container/done' EXIT

I really wanted to know if it's possible execute the above command once ENTRYPOINT execution is finished? (may be adding these commands together in a script and pass that script to the ENTRYPOINT). But, I am not sure what is required to add those command to execute it properly.

The code I have for the logstash container which would look for the file (which is created by the first container)

- args:
  - |
    dockerd-entrypoint.sh &
    while ! test -f /opt/example/apps/sync/container/done; do
      echo "Waiting for sync-app to finish..."
      sleep 5
    done
    echo "sync-app finished, exiting"
    exit 0
  command:
  - /bin/sh
  - -c

Please let me know if any more information is required. Thanks for all the help in advance!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can wait for the process to finish by the process PID. The bash interpreter has a build in command called wait where if called with a child PID as argument, it will wait until the child had finished.

So you can create the following entrypoint.sh and add it to your java container.

entrypoint.sh

#!/bin/bash

echo "Cleaning the file signal and running the entrypoint"
rm -rf /opt/example/apps/sync/container/done

exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar &
child_pid=$!
echo "Process started, waiting for the exit"
wait $child_pid

echo "Entrypoint exited, signaling the file"
touch /opt/example/apps/sync/container/done 
over 4 years ago · Santiago Trujillo Report

0

You can mount an emptyDir in both your main and sidecar containers.

Main container writes a file in a given location where this emptydir is mounted as the last statement in your entrypoint just before it exits, and the sidecar has a loop checking for existence of this same file.

Once it’s detected, the sidecar container does exit 0;

It’s a bit hacky but should do the work.

On a side note, Docker and Kubernetes have some defaults as to how to handle logs. They collect them from each container’s STDOUT and store the output in a file on the node running the container (/var/lib/docker/containers). Logstah could be setup as a daemonset running on each of these nodes with the containers logs location mounted as a hostPath and tail the files mentioned above. This way your containers don’t require any sodecar and in my opinion simplifies the overall setup, leaving to logstash the responsibility to do whatever is required with logging.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!