Junior — Middle
What methods can be used to stop and exit a Docker container when it is running?
sobes.tech AI
Answer from AI
There are several methods to stop and exit a Docker container upon its launch:
-
Ending the main process inside the container — the container runs as long as the main process runs. If this process ends, the container stops.
-
Using the
exitcommand in interactive mode — if the container is run with-it(interactive), you can simply typeexitin the container's terminal. -
Sending a termination signal — for example,
docker stop <container_id>sends a SIGTERM signal to the main process, leading to its termination. -
Using the
docker killcommand — forcibly terminates the container by sending a SIGKILL signal.
Example of interactive launch and exit:
docker run -it ubuntu /bin/bash
# inside the container
exit
After typing exit, the container will stop.