Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a Docker container after executing code

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
like image 640
jz22 Avatar asked Jun 14 '17 09:06

jz22


People also ask

What is the command to stop a container?

Note that pressing `Ctrl+C` when the terminal is attached to a container output causes the container to shut down. Use `Ctrl+PQ` in order to detach the terminal from container output.

Which command is used to stop a running container in docker?

The docker kill subcommand kills one or more containers. The main process inside the container is sent SIGKILL signal (default), or the signal that is specified with the --signal option.

What happens when you exit a docker container?

If you exit the container this way, your container stops as well. As you can see on the output above, the docker ps command shows no running containers. This docker tutorial discusses methods to stop a single docker container, multiple docker containers or all running docker containers at once.

How to restart a docker container?

You can restart a Docker container by executing the following: If you want to wipe out all running and stopped containers on your system at once without passing IDs or names, use the following command: It’s worth noting that the preceding command is dangerous and you should be very careful when typing it.

What does exit code 0 mean in Docker?

Exit Code 0 1 Exit code 0 indicates that the specific container does not have a foreground process attached. 2 This exit code is the exception to all the other exit codes to follow. It does not necessarily mean something bad happened. 3 Developers use this exit code if they want to automatically stop their container once it has completed its job.

What does it mean when a docker container says SIGTERM?

From the Docker container standpoint, this either indicates an issue with the application code or sometimes an issue with the base images used by the container. This indicates that container received SIGTERM. Common events that initiate a SIGTERM are docker stop or docker-compose stop.


1 Answers

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.

like image 103
VonC Avatar answered Oct 25 '22 17:10

VonC