Using `start`, `stop`, `restart`, and `rm` to manage containers.
A container, once created with `docker run`, has a distinct lifecycle that you can manage with a set of simple commands. The `docker ps` command shows you all currently running containers. To see all containers, including those that have been stopped, you can use the `docker ps -a` flag. If you need to stop a running container, you use the `docker stop <container_id_or_name>` command. This sends a SIGTERM signal to the main process inside the container, allowing it to shut down gracefully. If it doesn't stop within a grace period, a SIGKILL signal is sent. To start a stopped container, you use `docker start <container_id_or_name>`. This is useful for reusing a container without having to create a new one from scratch. The `docker restart` command provides a convenient shortcut for stopping and then starting a container. Once you are finished with a container and its data, you can remove it permanently using `docker rm <container_id_or_name>`. It's important to note that you can only remove stopped containers. To remove a running container, you must first stop it, or you can use the force flag `docker rm -f`. Understanding this lifecycle is crucial for managing applications, performing maintenance, and cleaning up resources to keep your host system tidy.