Sobes.tech
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:

  1. Ending the main process inside the container — the container runs as long as the main process runs. If this process ends, the container stops.

  2. Using the exit command in interactive mode — if the container is run with -it (interactive), you can simply type exit in the container's terminal.

  3. Sending a termination signal — for example, docker stop <container_id> sends a SIGTERM signal to the main process, leading to its termination.

  4. Using the docker kill command — 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.