Why docker exec is killing nohup process on exit?

Marek Smigielski picture Marek Smigielski · Nov 16, 2015 · Viewed 14.4k times · Source

I have running docker ubuntu container with just a bash script inside. I want to start my application inside that container with docker exec like that:

docker exec -it 0b3fc9dd35f2 ./main.sh

Inside main script I want to run another application with nohup as this is a long running application:

#!/bin/bash
nohup ./java.sh &
#with this strange sleep the script is working
#sleep 1
echo `date` finish main >> /status.log

The java.sh script is as follow (for simplicity it is a dummy script):

#!/bin/bash
sleep 10
echo `date` finish java >> /status.log

The problem is that java.sh is killed immediately after docker exec returns. The question is why?

The only solution I found out is to add some dummy sleep 1 into the first script after nohup is started. Than second process is running fine. Do you have any ideas why it is like that?

[EDIT]

Second solution is to add some echo or trap command to java.sh script just before sleep. Than it works fine. Unfortunately I cannot use this workaround as instead of this script I have java process.

Answer

aderubaru picture aderubaru · Dec 29, 2015

This is not an answer, but I still don't have the required reputation to comment.

I don't know why the nohup doesn't work. But I did a workaround that worked, using your ideas:

docker exec -ti running_container bash -c 'nohup ./main.sh &> output & sleep 1'