Hello! Welcome back to our module on Containerization with Docker.
In the last lesson, you successfully installed Docker and ran your first containers, including a fully functional Nginx web server. You launched it into the background using docker run -d. This is a common practice in DevOps, but what happens after you run that command? How do you check on that background process? How do you stop it, restart it, or clean it up when it's no longer needed?
Today, we'll answer those questions by diving into the container lifecycle. You'll learn the essential commands to manage your containers from creation to removal.
Today's Learning Outcome: By the end of this lesson, you will be able to manage container lifecycle using docker run, stop, start, and rm commands.
We'll work through the key stages:
- Visualizing the container lifecycle and its states.
- Viewing running and stopped containers.
- Stopping, starting, and restarting containers.
- Removing containers to clean up your environment.
Let's begin managing the Nginx container you created.
1. The States of a Docker Container
A container isn't just "on" or "off." It moves through several states during its life. Understanding these states is key to managing containers effectively. The primary states are: created, running, paused, stopped, and exited.
The following resource provides a great visual overview of these states and the commands that transition a container between them.
Docker Container Lifecycle: Create, Run, Stop & Remove ...
This article from K21 Academy provides a clear diagram of the Docker container lifecycle and introduces the commands we'll be using today.
Please read the introductory section, 'Docker Container Lifecycle Management', and pay close attention to the lifecycle diagram. It perfectly illustrates how commands like create, run, stop, and rm move a container from one state to another.
Now, let's use the commands from that diagram to manage a real container.
2. Viewing and Inspecting Containers
Before you can manage a container, you need to be able to find it and check its status. The docker ps command is your primary tool for this.
Listing Running Containers
In your terminal, run the following command. ps here is a nod to the ps command in Linux for listing processes, which you're already familiar with.
docker ps
If the Nginx container from the previous lesson is still running, you'll see it listed here. The output gives you crucial information:
- CONTAINER ID: A unique identifier for the container.
- IMAGE: The image the container was created from (e.g.,
nginx). - COMMAND: The command running inside the container.
- CREATED: How long ago the container was created.
- STATUS: The current state (e.g., "Up 2 hours").
- PORTS: Any port mappings (e.g.,
0.0.0.0:8080->80/tcp). - NAMES: A human-readable name. Docker assigns a random one if you don't provide one with
--name.
Listing All Containers
What if you stop a container? It disappears from the docker ps list. To see all containers, including stopped ones, add the -a flag.
docker ps -a
This is a critical command for housekeeping. A stopped container doesn't use CPU or memory, but it still exists and takes up disk space. The STATUS column will show "Exited" for stopped containers.
3. Stopping, Starting, and Killing Containers
Let's manipulate the Nginx container. First, get its Container ID or Name from the docker ps output.
Stopping a Container
The docker stop command sends a SIGTERM signal to the main process in the container, asking it to shut down gracefully. This is the standard, safe way to stop a container.
# Replace 'your_container_name_or_id' with the actual name or ID
docker stop your_container_name_or_id
After running this, check docker ps again—the container will be gone. Now check docker ps -a, and you will see its status as "Exited". If you try visiting http://localhost:8080, the connection will fail.
Starting a Container
A stopped container can be restarted with the docker start command. This brings the container back to life with all its previous configuration, including its name and port mappings.
docker start your_container_name_or_id
Run docker ps again. The container is back! And http://localhost:8080 will work once more.
Killing a Container
Sometimes, a container becomes unresponsive and won't shut down with docker stop. In these cases, you can use docker kill. This command is more forceful, sending a SIGKILL signal that terminates the process immediately without giving it a chance to clean up.
docker kill your_container_name_or_id
You should use docker stop by default and only use docker kill when necessary.
The following article explains these commands and the signals they use in more detail.
Docker Container Lifecycle: Create, Run, Stop & Remove ...
To deepen your understanding, let's revisit the K21 Academy article. It clarifies the subtle but important differences between these lifecycle commands.
Please read the sections 'Stop Container', 'Kill Container', 'Difference Between Docker Pause And Docker Stop container', and 'Docker rm Vs. Docker Kill'. Focus on the signals (SIGTERM, SIGKILL) and the distinction between gracefully stopping and forcefully killing a container.
Test your understanding!
You have just stopped a container named web-server. Which command would you use to confirm it is no longer running but still exists on your system?
Show answer
You would use docker ps -a. The docker ps command only shows currently running containers, so web-server would not appear there. By adding the -a flag, you list all containers, and you would see web-server with a status like "Exited".
4. Removing Containers
A stopped container still occupies disk space. Once you are certain you no longer need a container, you can remove it with docker rm.
Removing a Stopped Container
First, ensure the container is stopped. You cannot remove a running container by default.
# First, stop the container if it's running
docker stop your_container_name_or_id
# Then, remove it
docker rm your_container_name_or_id
If you try to remove a running container, Docker will give you an error. This is a safety feature.

Forcing Removal
If you want to stop and remove a container in one step, you can use the force (-f) flag. This is a convenient shortcut.
docker rm -f your_container_name_or_id
This is equivalent to running docker stop followed by docker rm.
Cleaning Up
Over time, you may accumulate many stopped containers. You can remove all stopped containers at once with a handy prune command:
docker container prune
Docker will ask for confirmation before proceeding. This is a very useful command for keeping your development environment clean.
5. run vs. create + start
So far, we've used docker run to both create and start a container. It's important to know that this is a combination of two underlying commands: docker create and docker start.
docker create <image>: This prepares the container. It creates the writable layer on top of the specified image and sets up its configuration (like network settings), but does not start it. The container enters the "Created" state.docker start <container>: This command takes a container from the "Created" or "Stopped" state and puts it into the "Running" state.
The docker run command simply does both of these steps in a single operation. For most daily use, docker run is what you'll use, but it's good to understand the two-step process happening behind the scenes.
Basic Container Lifecycle Management
The resource from dev.to provides a practical walkthrough of these concepts using an Ubuntu container. It reinforces the commands we've discussed in a step-by-step exercise format.
Please read the sections under the heading 'Managing Container Life Cycles' that demonstrate starting a container and then stopping and removing it. Specifically, follow the text describing the use of docker run, docker stop, and docker rm. This will provide a practical recap of what we've covered.
6. Hands-On Practice
Let's put it all together.
- Create and run a new Redis container in detached mode. Give it a name,
my-redis-cache, so it's easy to reference.docker run -d --name my-redis-cache redis - Verify it's running.
docker ps - Stop the container using its name.
docker stop my-redis-cache - Verify it's stopped but not gone.
docker ps -a - Start it again.
docker start my-redis-cache - Finally, stop and remove it permanently.
docker rm -f my-redis-cache - Confirm it has been removed.
Thedocker ps -amy-redis-cachecontainer should no longer appear in the list.
Conclusion
You now have the fundamental commands to manage the lifecycle of any Docker container. You can confidently view, start, stop, and clean up containers, which are essential day-to-day skills for a DevOps engineer.
Key Takeaways:
- A container moves through states like created, running, and stopped.
docker pslists running containers, whiledocker ps -alists all containers.docker stopgracefully shuts down a container, whiledocker killforces an immediate stop.docker startrestarts a stopped container.- Stopped containers still exist on your disk until you remove them with
docker rm. - The
docker container prunecommand is a quick way to remove all stopped containers.
Next Up: Building Your Own Images
So far, we've only used pre-built images from Docker Hub like nginx and redis. But the real power of Docker comes from packaging your own applications. In the next lesson, you will learn how to create a basic Dockerfile to build a custom container image for a simple application. This is the first step toward containerizing your own projects.