Skip to main content
In this lesson you’ll learn the most commonly used Docker CLI commands for running, listing, stopping, removing, and inspecting containers and images. Each command includes examples you can run locally. Practice these commands in the hands-on exercises to build confidence with container workflows.

docker run — create and start a container

  • docker run creates and starts a container from an image.
  • If the requested image isn’t present locally, Docker pulls it from Docker Hub the first time and reuses the downloaded image for subsequent runs.
  • By default docker run runs the container in the foreground (attached). Use -d to run in detached (background) mode.
Example: running nginx for the first time triggers a pull and then starts the container:
Note: Docker Hub enforces pull rate limits for anonymous users. Authenticate with docker login to increase limits on the free Personal tier, or use a paid plan for unlimited pulls.
An infographic titled "Heads‑Up — Docker Hub Rate Limits" showing three account tiers. It lists Anonymous (no login) at 100 pulls/6 hrs per IP, Free (after login) at 200 pulls/6 hrs per account, and Paid (Pro/Team/Business) as unlimited.
If you hit “too many requests” or rate limit errors while following this lesson, run docker login or sign up for a paid Docker Hub plan to avoid interruptions.

docker ps and docker ps -a — list containers

  • docker ps lists currently running containers and shows: CONTAINER ID, IMAGE, COMMAND, CREATED, STATUS, PORTS, and NAMES.
  • Each container receives an automatic name (e.g., silly_sammet) unless you supply one with --name.
  • docker ps -a lists all containers, including exited ones.
Example: running containers:
Example: all containers (including exited):

Stop and remove containers

  • Stop a running container:
    • docker stop <container> where <container> is a container name or ID (short prefix allowed if unambiguous).
  • Remove a stopped container:
    • docker rm <container>
Example workflow:

Image management: docker images, docker rmi, docker pull

  • docker images lists images stored locally with REPOSITORY, TAG, IMAGE ID, CREATED, and SIZE.
  • docker rmi <image> removes an image. You must stop and remove any containers using that image first.
  • docker pull <image> downloads an image to the host without running it — useful to avoid the initial pull delay when you later docker run.
Commands and purpose (quick reference): Example output:
If you plan to pull many images, authenticate first to avoid anonymous rate limits.

Why some containers exit immediately

Containers run the process given as PID 1. When that process exits, the container stops. Many base images (like ubuntu) do not include a long-running foreground process, so starting them without a command will simply exit. Example: default ubuntu run exits immediately:
Run a short-lived command inline:
The container will run sleep 5 and exit after five seconds.

Executing commands in a running container

Use docker exec to run commands inside an already-running container. Common uses: inspect files, check processes, or open an interactive shell.
  • Run a single command inside a container:
    • docker exec <container> <command>
  • Start an interactive shell:
    • docker exec -it <container> /bin/bash (or /bin/sh if bash isn’t available)
Examples:

Foreground (attached) vs detached mode

  • By default docker run attaches the container’s stdout/stderr to your terminal (foreground). Ctrl-C sends SIGINT to that process and usually stops the container.
  • Use -d to run detached (background):
To interact with a running detached container:
  • Follow logs: docker logs -f my-web-app
  • Open a shell: docker exec -it my-web-app /bin/bash
  • Attach to the main process: docker attach my-web-app (see warning below)
docker attach connects directly to the container’s primary process stream and can interfere with signal handling (for example, Ctrl-C may stop the container). Prefer docker logs or docker exec -it for most debugging and interactive tasks.

That covers the basic Docker CLI commands you’ll use to run, list, stop, remove, and inspect containers and images. Practice these commands in the hands-on labs to solidify your understanding.

Watch Video