Listing and Managing Docker Containers

Docker containers are at the heart of application packaging and deployment. Effectively managing these containers requires knowing how to list, inspect, and control them. This tutorial will guide you through the essential commands for listing Docker containers and related management tasks.

Listing Containers

The primary command for listing Docker containers is docker ps. This command, by default, displays running containers.

docker ps

This will output a table with information about each running container, including:

  • CONTAINER ID: A unique identifier for the container.
  • IMAGE: The image used to create the container.
  • COMMAND: The command executed when the container started.
  • CREATED: How long ago the container was created.
  • STATUS: The current status of the container (e.g., “Up” indicates running).
  • PORTS: Port mappings between the container and the host machine.
  • NAMES: An automatically assigned or user-defined name for the container.

Listing All Containers

To view all containers, regardless of their status (running, stopped, exited, etc.), use the -a or --all flag with docker ps.

docker ps -a

This will include containers that have finished executing or are currently stopped. This is essential for cleanup and inspection of past executions.

Modern Command Structure: docker container ls

Docker has evolved its command structure to be more object-oriented. Instead of docker ps, you can now use docker container ls.

docker container ls

This command provides the same functionality as docker ps (listing running containers). To list all containers using the new structure, use:

docker container ls -a

Both docker ps and docker container ls are supported, but docker container ls is the recommended approach for new users as it aligns with the evolving Docker CLI design.

Filtering and Formatting Output

You can refine the output of docker ps or docker container ls using various flags:

  • -q or --quiet: Displays only the container IDs. This is useful for scripting and automation.

    docker ps -aq
    
  • -s: Shows the size of running containers.

    docker ps -s
    
  • -l: Displays the latest created container (includes all states).

    docker ps -l
    
  • -n <number>: Displays the last number of created containers. For example, to see the last 2 containers:

    docker ps -n 2
    
  • -f <filter>: Filters the containers based on specified criteria. For example, to list only containers with the status “exited”:

    docker ps -a -f status=exited
    

    You can filter by various attributes like status, name, image, etc. See the Docker documentation for a complete list of filter options.

Managing Containers: Stopping and Removing

After listing containers, you might need to stop or remove them:

  • docker stop <container_id>: Stops a running container gracefully.
  • docker kill <container_id>: Forcefully stops a running container.
  • docker rm <container_id>: Removes a stopped container.
  • docker rm -f <container_id>: Forcefully removes a running container.

You can remove multiple containers at once using a command like this:

docker rm $(docker ps -aq)

This removes all stopped containers. Be careful when using this command, as it permanently deletes the containers.

Leave a Reply

Your email address will not be published. Required fields are marked *