Skip to main content
In this tutorial, we’ll cover essential Docker container lifecycle commands using the Docker CLI. You’ll learn how to create, start, list, interact with, monitor, and clean up containers.

1. Creating and Starting a Container

  1. Create a container from the official httpd image.
    If the image isn’t available locally, Docker pulls it from Docker Hub.
  2. List containers (none are running yet):
  3. Include stopped containers with -a:
  4. Start the container by its CONTAINER ID:
  5. Verify it’s running:

Table: docker container ls Field Descriptions

2. Listing Options

Use different flags with docker container ls to customize output: Example—list only IDs of running containers:

3. Running Containers Interactively

Combine create and start with run:
If ubuntu:latest isn’t local, you’ll see the pull progress, then a shell prompt:
Exiting the shell stops the container:

Detaching Without Stopping

To leave a container running and return to the host shell, press Ctrl+P then Ctrl+Q:
Detaching this way leaves the container running in the background.

4. Executing Commands in Running Containers

Run additional commands inside an active container using exec:
You can even use partial container IDs:

5. Attaching to a Container

Use attach to connect to the primary process of a running container:
Exiting an attached session (exit) will terminate the container’s main process.

6. Stopping and Removing Containers

  • Stop a running container:
  • Remove a stopped container:
  • Prune all stopped containers:
docker container prune removes all stopped containers. Use with caution.
Or combine stop and remove for all containers:

7. Detached Mode and Naming

Run in detached mode (-d) and assign a custom name:
Rename an existing container:

8. Inspecting Container Details

Retrieve full metadata with inspect:
Sample output:

9. Monitoring Containers

9.1 Resource Usage

Display real-time stats:

9.2 Process List

Show host PIDs inside a container:

9.3 Logs

View past logs:
Follow logs live:

Conclusion

You’ve now mastered:
  • Creating, starting, and listing containers
  • Interactive sessions with run, exec, and attach
  • Naming, renaming, and inspecting container details
  • Monitoring resource usage and logs
  • Cleaning up with stop, rm, and prune
For more commands, see the Docker CLI reference.

Watch Video