6

In order to get a deeper insight into Docker I created a dockerfile that executes a python script. It works fine but after the execution of the script the container crashes. How can I modify my dockerfile in order to destroy the container after execution instead of letting the container crash and restart all the time?

Dockerfile:

FROM python:3

ADD aa.py /

CMD [ "python", "./aa.py" ]

Python:

print('Hello!')

Error message:

2017-06-14 11:37:09 [CELL/0] OUT Starting health monitoring of container
2017-06-14 11:37:09 [APP/PROC/WEB/0] OUT Hello!
2017-06-14 11:37:09 [APP/PROC/WEB/0] OUT Exit status 0
2017-06-14 11:37:09 [CELL/0] OUT Exit status 143
2017-06-14 11:37:09 [CELL/0] OUT Destroying container
2017-06-14 11:37:09 [API/0] OUT Process has crashed with type: "web"
2017-06-14 11:37:09 [API/0] OUT App instance exited with guid 6fdede46-6751-4725-ad78-b76262dbe701 payload: {"instance"=>"", "index"=>0, "reason"=>"CRASHED", "exit_description"=>"2 error(s) occurred:\n\n* 2 error(s) occurred:\n\n* Codependent step exited\n* cancelled\n* cancelled", "crash_count"=>4, "crash_timestamp"=>1497433029411246770, "version"=>"98e2a035-e38f-4169-95fb-2701c8486e9c"}
2017-06-14 11:37:09 [CELL/0] OUT Successfully destroyed container
2017-06-14 11:38:31 [CELL/0] OUT Creating container
jz22
  • 2,328
  • 5
  • 31
  • 50
  • How did you start your container? You can use `docker run --rm mypython` which will remove your container after executing the script. – lvthillo Jun 14 '17 at 09:50
  • I use Docker on CloudFoundry. Via the user interface I typed in a docker hub repo and started it. – jz22 Jun 14 '17 at 09:52
  • I've never used cloudfundry so can't help you with that. I would suspect that it tries to restart because of cf because just in docker this container will run, show the message and stop. The `--rm` option will remove the container after the run. I can't help you to reproduce this in cf. – lvthillo Jun 14 '17 at 09:55

1 Answers1

3

Note: the default CMD for python:3 is python3.

exit code 143 means SIGTERM as mentioned here. That is what docker sends.
So you need for your python3 application to process SIGTERM signal gracefully

Don't forget that your python app, once completed and exited the main function, would cause the container to automatically stop and exit.

The OP adds in the comments:

In the meantime, I have found out that handling the SIGTERM in the Docker environment works perfectly fine.

However using the same code in Docker on CloudFoundry does not prevent the container from crashing.
In CloudFoundry you need an application that is always running and not just doing a task and then stopping like a script does.
Even stopping without errors is detected as a crash in CloudFoundry.

I transformed my script into a REST server by using the flask framework. Now it is always running but only doing its task when being called via its url.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you. When is the signal sent? Once the script executed? – jz22 Jun 15 '17 at 12:03
  • I tried the solutions but after handling the SIGTERM it crashes again. – jz22 Jun 15 '17 at 12:55
  • @user3080315 the signal is sent when the main process exits and the container is stopping. If your application doesn't handle it correctly, you can be left with zombie processes (https://stackoverflow.com/a/33119321/6309). That is why, with docker 1.13 or more, you have `docker run --init`: https://stackoverflow.com/a/39593409/6309 – VonC Jun 15 '17 at 12:57
  • @user3080315 If it still crashes, when the docker logs, to see if there is any additional clues. – VonC Jun 15 '17 at 12:58
  • I just noticed that after implementing a SIGNIT handler it no longer says exit code 143. Now it says crashed with exit code 0 and with type web. – jz22 Jun 15 '17 at 14:16
  • @user3080315 That seems expected: 143 is the specific signal code associated with SIGTERM (128+15). SIGINT (interrupted from keyboard) is 130 (128+2): http://support.ersa.edu.au/hpc/pbs-exit-codes.html – VonC Jun 15 '17 at 14:18
  • But it crashes even though I handled the sigterm and created a new container afterwards. – jz22 Jun 15 '17 at 14:21
  • @user3080315 Exactly, which is why I follow your other question which addresses that: https://stackoverflow.com/questions/44567981/how-to-properly-handle-sigint-signal-in-python – VonC Jun 15 '17 at 14:22
  • @user3080315 did you solve your other question you just deleted? – VonC Jul 06 '17 at 08:29
  • In the meantime I have found out that handling the SIGTERM in the Docker environment works perfectly fine. However using the same code in Docker on CloudFoundry does not prevent the container from crashing. In CloudFoundry you need an application that is always running and not just doing a task and then stopping like a script does. Even stopping without errors is detected as a crash in CloudFoundry. I transformed my script into a REST server by using the flask framework. Now it is always running but only doing its task when being called via its url. – jz22 Jul 06 '17 at 08:38
  • @user3080315 nice catch! I have included your comment in the answer for more visibility. – VonC Jul 06 '17 at 08:41