Stop Docker container after executing code

To get a deeper understanding of Docker, I created a dockerfile that executes a python script. It works fine, but after executing the script, the container crashes. How can I modify my dockerfile to destroy the container after execution, rather than allow the container to 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

      

+3


source to share


1 answer


Note: the default for is . CMD

python:3

python3

exit code 143 meansSIGTERM

as mentioned here . This is what docker is sending .
So you need a python3 app to handle the SIGTERM signal gracefully

Don't forget that your python application, upon completion and exit from the main function, will cause the container to automatically stop and exit.



The OP adds in the comments :

In the meantime, I found that SIGTERM

the Docker processing works fine.

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

I converted my script to a REST server using a flash framework. Now it always works, but only performs its task when called through its url.

+2


source







All Articles