Skip to main content
In this lesson you’ll learn commonly used Docker commands for creating, running, inspecting, and cleaning up containers and images. A short hands-on quiz follows the conceptual overview to help you practice. Table of contents:
  • docker run — create & start containers
  • docker ps — list containers
  • stop & remove containers
  • docker images, docker rmi, docker pull — image management
  • why some containers (like ubuntu) exit immediately
  • running specific commands and docker exec
  • foreground vs detached mode
  • quick tips and references

docker run — Create and start a container

The docker run command creates a container from an image and starts it. If the image is not available locally, Docker pulls it from Docker Hub on first use and reuses the downloaded image for subsequent runs. Example: starting the official nginx image
A graphic titled "Heads‑Up — Docker Hub Rate Limits" showing three tiers: Anonymous (100 pulls/6 hrs per IP), Free (200 pulls/6 hrs per account), and Paid (unlimited/no rate limit).
Docker Hub enforces pull rate limits for unauthenticated and authenticated accounts. If you see “too many requests” errors while following this lesson, log in with docker login or use a paid plan to avoid interruptions.

docker ps — List running (and all) containers

  • docker ps lists running containers and shows columns such as CONTAINER ID, IMAGE, COMMAND, CREATED, STATUS, PORTS, and NAMES.
  • Docker auto-generates a friendly container name (for example, silly_sammet) unless you set one with --name.
To include stopped/exited containers, add -a:
Example outputs:

Stop and remove containers

  • Stop a running container:
You can provide the full container ID or the first unique characters.
  • After stopping, the container will appear in docker ps -a with Exited status.
  • Remove a stopped container permanently:
Example sequence:
Before removing an image with docker rmi, stop and remove any containers that depend on it. Removing an image in use will fail.

docker images, docker rmi, and docker pull — Image management

  • docker images lists images on the host including repository, tag, image ID, creation time, and size.
  • Remove an image with:
(Stop and remove containers that use the image first.)
  • Pull an image without running it:
Example:

Why an ubuntu container often exits immediately

Containers run the command specified in the image’s CMD or ENTRYPOINT. Base images like ubuntu usually don’t run a long-lived process, so the container starts, runs the default command (often non-interactive or nothing), then exits. Example:
To keep a container alive, run a long-running command or service (for example, sleep, a shell with interactive mode, or an actual daemon).

Run a specific command when starting a container

You can override the image’s default command by appending a command to docker run:
This starts the container, runs sleep 5, then the container exits after 5 seconds. If you want an interactive shell:

Execute commands inside a running container: docker exec

docker exec runs a command inside an already-running container. This is handy for debugging, inspecting files, or running a shell. Example: viewing /etc/hosts inside a container
To get an interactive shell inside a running container:

Foreground (attached) vs detached mode

  • By default docker run attaches the container’s stdout/stderr to your terminal (foreground). Pressing Ctrl-C will stop the container.
  • To start a container in the background, use -d (detached):
  • Reattach to a container’s streams with:
Example:

Quick reference — common Docker commands

Note: In the examples above, when angle brackets are used for placeholders, use backticks around them. For example, docker stop <container-name-or-id>`

Tips and reminders

  • You only need the leading unique characters of a container ID to reference it.
  • Stop and remove containers before removing their images.
  • Authenticate with docker login to increase pull quotas and reduce rate-limit interruptions.
  • Use -it for interactive shells (-i keeps STDIN open; -t allocates a pseudo-TTY).
Next: practice these commands in the hands-on exercises to reinforce the concepts covered.

Watch Video

Practice Lab