KAHIBARO Bookshelves
Discord Login Register

4.4. Stopping and Removing Containers

Why Stopping and Removing Matters

Once you start running containers, your system can quickly fill up with processes and data that you no longer need. Learning how to stop and remove containers keeps your Docker environment clean, makes troubleshooting easier, and prevents conflicts such as port clashes.

You will work only with containers in this chapter, not with images or volumes in depth. The focus is on how to safely stop what is running and how to clean up what you no longer use.

Stopping a Running Container

A container must be running before you can stop it. When a container is active, Docker shows it in the list of running containers, and its main process is still alive.

To stop a container gracefully, you use the docker stop command followed by the container’s name or ID. Docker then sends a termination signal to the main process in the container and gives it a short period of time to exit on its own.

For example, if you have a container called myapp, you can stop it like this:

docker stop myapp

You can also use the short or full container ID, for example:

docker stop 4f3c1a2b9d0e

If the process inside the container handles the signal properly, it can close open connections, finish current work, and exit cleanly. This is the preferred way to stop most containers.

Always try docker stop first. It allows the container to shut down gracefully and reduces the risk of data loss or corruption.

Forcing a Container to Stop

Sometimes a container refuses to stop. This can happen if the main process ignores termination signals or hangs. In that case, Docker waits for a timeout, and if the process is still alive, docker stop can fail or take longer than expected.

To immediately force a container to stop, you use docker kill. This sends a stronger signal that ends the process right away, without giving it time to clean up.

For example:

docker kill myapp

or with an ID:

docker kill 4f3c1a2b9d0e

You should treat docker kill as a last resort. It is useful in cases where a container is stuck or unresponsive, but it can interrupt work in progress inside the container.

Use docker kill only when docker stop does not work or takes too long. Forced termination can interrupt running operations inside the container.

What Happens After You Stop a Container

When you stop a container, Docker keeps its state on disk. The container is no longer running, but it still exists in Docker’s internal database. You can see it in the list of all containers, and you can start it again later with the same name and configuration.

A stopped container still consumes disk space. This includes container metadata and any data written to the container’s writable layer. Over time, many stopped containers can use a significant amount of storage, especially on development machines where you experiment a lot.

You can restart a stopped container with the start command.

For example:

docker start myapp

This will bring the container back to the running state with the same configuration it had when it was created.

Removing a Container

To permanently delete a container, you use the docker rm command. Removing a container means that you cannot start it again, because its definition and writable layer are deleted.

For example, if you have a stopped container named myapp, you can remove it like this:

docker rm myapp

You can also remove it by ID:

docker rm 4f3c1a2b9d0e

If the container is still running, docker rm will fail by default and Docker will tell you that the container is in use. Docker expects you to stop the container first.

The common flow looks like this:

docker stop myapp
docker rm myapp

You can only remove containers that are not running, unless you explicitly force removal. Once a container is removed, you cannot restart it, so be sure you no longer need it.

Forcing Removal of Containers

Docker supports a force option when removing containers. This combines stopping and removing in one step. It is useful when you are certain that you want to get rid of a container immediately.

To force removal, add the -f or --force flag:

docker rm -f myapp

This tells Docker to kill the container if it is running and then remove it. It behaves like calling docker kill followed by docker rm.

You should be cautious with this option, especially if the container is doing important work or handling real traffic.

Removing Multiple Containers at Once

During development, you might start many temporary containers. Removing them one by one can be tedious. Docker lets you remove multiple containers in a single command by listing them all, or by combining other commands with docker rm.

If you have several stopped containers called web1, web2, and db1, you can remove them together like this:

docker rm web1 web2 db1

You can also use docker rm with a list of container IDs.

For quick cleanups, the -f flag can be combined with multiple names or IDs:

docker rm -f web1 web2 db1

Be careful, because this will immediately stop and remove any of these containers that are still running.

Cleaning Up “Exited” Containers

In active development environments, you often accumulate many containers with an “exited” status. These are containers that have stopped but have not been removed. They take up space and make it harder to find the containers that matter.

A common pattern is to remove containers that are no longer running. One way is to use the docker container prune command, which automatically removes all stopped containers.

When you run:

docker container prune

Docker asks for confirmation and then deletes every container that is not running. It will keep running containers, but remove all exited ones, and free up space.

docker container prune removes all stopped containers at once. Do not use it if you still need any exited container or its data.

Relationship Between Stopping, Removing, and Images

Stopping or removing a container does not delete the image that was used to create it. Images are separate from containers. After you remove a container, you can still create a new container from the same image.

This separation is important. You clean up containers to manage running instances and disk usage related to them, but you manage images with different commands entirely. As a result, removing a container that you no longer use is safe if you still have the image and can recreate the container when needed.

Practical Habits for Daily Use

In everyday work, you will often start containers to test something, then stop and remove them when you are done. A common sequence for a short-lived container is:

Run a container for some task.
Stop it when finished with docker stop.
Remove it with docker rm once you are sure it is no longer needed.

For temporary experiments, you might use force removal to simplify this:

docker rm -f temp-container

With time, you will develop your own habits, but the core idea stays the same. Stop containers to halt their processes. Remove containers to clean up resources and avoid clutter.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Introduction
    • ☰ 1.1. What is Docker?
      • ☰ 1.2. Why Docker?
        • ☰ 1.3. Virtual Machines vs Containers
          • ☰ 1.4. When Not to Use Docker
            • ☰ 1.5. How Docker Is Used in Real Projects
            • ☰ 2. Core Concepts
              • ☰ 2.1. Images
                • ☰ 2.2. Containers
                  • ☰ 2.3. Docker Engine and Architecture
                    • ☰ 2.4. Docker Hub and Container Registries
                      • ☰ 2.5. Tags, Versions, and Digests
                      • ☰ 3. Installing Docker
                        • ☰ 3.1. Docker on Linux
                          • ☰ 3.2. Docker on Windows
                            • ☰ 3.3. Docker on macOS
                              • ☰ 3.4. Verifying the Installation
                                • ☰ 3.5. Common Installation Issues
                                • ☰ 4. Your First Docker Container
                                  • ☰ 4.1. Running Your First Container
                                    • ☰ 4.2. Understanding `docker run`
                                      • ☰ 4.3. Interactive vs Detached Mode
                                        • ☰ 4.4. Stopping and Removing Containers
                                          • ☰ 4.5. Listing Containers
                                          • ☰ 5. Working with Docker Images
                                            • ☰ 5.1. Pulling Images from Docker Hub
                                              • ☰ 5.2. Listing and Removing Images
                                                • ☰ 5.3. Image Layers Explained
                                                  • ☰ 5.4. Image Size Optimization
                                                    • ☰ 5.5. Inspecting Images
                                                    • ☰ 6. Dockerfiles – Building Your Own Images
                                                      • ☰ 6.1. What Is a Dockerfile?
                                                        • ☰ 6.2. Basic Dockerfile Structure
                                                          • ☰ 6.3. Common Instructions (`FROM`, `RUN`, `COPY`, `CMD`, `ENTRYPOINT`)
                                                            • ☰ 6.4. Building Images with `docker build`
                                                              • ☰ 6.5. Best Practices for Dockerfiles
                                                                • ☰ 6.6. Multi-Stage Builds
                                                                • ☰ 7. Managing Data with Volumes
                                                                  • ☰ 7.1. Why Containers Are Ephemeral
                                                                    • ☰ 7.2. Bind Mounts vs Volumes
                                                                      • ☰ 7.3. Creating and Using Volumes
                                                                        • ☰ 7.4. Inspecting and Removing Volumes
                                                                          • ☰ 7.5. Real-World Examples
                                                                          • ☰ 8. Networking in Docker
                                                                            • ☰ 8.1. Docker Networking Basics
                                                                              • ☰ 8.2. Bridge Networks
                                                                                • ☰ 8.3. Port Mapping
                                                                                  • ☰ 8.4. Container-to-Container Communication
                                                                                    • ☰ 8.5. Custom Networks
                                                                                    • ☰ 9. Docker Compose
                                                                                      • ☰ 9.1. Why Docker Compose?
                                                                                        • ☰ 9.2. `docker-compose.yml` Structure
                                                                                          • ☰ 9.3. Defining Services, Networks, and Volumes
                                                                                            • ☰ 9.4. Running Multi-Container Applications
                                                                                              • ☰ 9.5. Web App and Database Example
                                                                                                • ☰ 9.6. Docker Compose Best Practices
                                                                                                • ☰ 10. Debugging and Monitoring Containers
                                                                                                  • ☰ 10.1. Viewing Logs
                                                                                                    • ☰ 10.2. Executing Commands in Running Containers
                                                                                                      • ☰ 10.3. Inspecting Containers
                                                                                                        • ☰ 10.4. Resource Usage
                                                                                                          • ☰ 10.5. Common Errors and Fixes
                                                                                                          • ☰ 11. Security Basics
                                                                                                            • ☰ 11.1. Running Containers as Non-Root
                                                                                                              • ☰ 11.2. Image Trust and Verification
                                                                                                                • ☰ 11.3. Secrets and Environment Variables
                                                                                                                  • ☰ 11.4. Reducing Attack Surface
                                                                                                                    • ☰ 11.5. Security Best Practices
                                                                                                                    • ☰ 12. Performance and Optimization
                                                                                                                      • ☰ 12.1. Reducing Image Size
                                                                                                                        • ☰ 12.2. Caching and Layer Ordering
                                                                                                                          • ☰ 12.3. Resource Limits
                                                                                                                            • ☰ 12.4. Choosing the Right Base Image
                                                                                                                            • ☰ 13. Docker in Development Workflows
                                                                                                                              • ☰ 13.1. Docker for Local Development
                                                                                                                                • ☰ 13.2. Hot Reloading with Volumes
                                                                                                                                  • ☰ 13.3. Docker and Git
                                                                                                                                    • ☰ 13.4. Docker in CI/CD Pipelines
                                                                                                                                      • ☰ 13.5. Common Development Patterns
                                                                                                                                      • ☰ 14. Deployment Basics
                                                                                                                                        • ☰ 14.1. Running Containers in Production
                                                                                                                                          • ☰ 14.2. Environment Configuration
                                                                                                                                            • ☰ 14.3. Restart Policies
                                                                                                                                              • ☰ 14.4. Logging Strategies
                                                                                                                                                • ☰ 14.5. Basic Scaling Concepts
                                                                                                                                                • ☰ 15. Beyond the Basics
                                                                                                                                                  • ☰ 15.1. Docker Swarm Overview
                                                                                                                                                    • ☰ 15.2. Kubernetes vs Docker
                                                                                                                                                      • ☰ 15.3. Container Registries
                                                                                                                                                        • ☰ 15.4. When to Move Beyond Docker
                                                                                                                                                        • ☰ 16. Practical Projects
                                                                                                                                                          • ☰ 16.1. Dockerizing a Simple Web App
                                                                                                                                                            • ☰ 16.2. Dockerizing a Backend API
                                                                                                                                                              • ☰ 16.3. Full-Stack App with Docker Compose
                                                                                                                                                                • ☰ 16.4. Common Project Pitfalls
                                                                                                                                                                • ☰ 17. Troubleshooting and FAQ
                                                                                                                                                                  • ☰ 17.1. Common Beginner Mistakes
                                                                                                                                                                    • ☰ 17.2. Docker Command Cheat Sheet
                                                                                                                                                                      • ☰ 17.3. FAQ
                                                                                                                                                                        • ☰ 17.4. Useful Resources