Installing and Using the Docker CLI
Before you can build or run containers, you need Docker installed and the docker command-line tool available in your shell. This section covers installation options and the core CLI commands you will use constantly: running containers, listing them, inspecting logs, and cleaning up resources.
Cricket analogy: Before you can play a match you need your kit bag packed and bat in hand; installing Docker and having the CLI ready is that essential prep before you can run, list, or inspect any 'innings' of containers.
Installation Options
On Linux, the recommended approach is Docker Engine installed directly from Docker's official APT or YUM repositories, giving you the daemon, CLI, and containerd as native services. On macOS and Windows, Docker Desktop provides a lightweight Linux VM (since both operating systems lack the Linux kernel features Docker needs) along with the daemon, CLI, and a GUI. Docker Desktop is also available for Linux as an alternative to a native Engine install.
Cricket analogy: Installing Docker Engine natively on Linux is like a home ground where the pitch (kernel) is already prepared; on foreign turf (macOS/Windows) you need to ship in a portable pitch (a lightweight Linux VM) via Docker Desktop first.
# Install Docker Engine on Ubuntu/Debian (official convenience script, for evaluation)
$ curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh
# Verify the installation
$ docker --version
Docker version 25.0.3, build 4debf41
# Run the classic hello-world test container
$ docker run hello-worldThe get-docker.sh convenience script is intended for testing and development environments, not production. For production servers, follow Docker's official package-manager installation instructions (apt/yum repositories) so you can control versions and receive updates through your normal patching process.
Running Your First Container
The docker run command creates and starts a container from an image in one step. Common flags include -d (detached, runs in the background), --name (assigns a friendly name), -p host:container (maps a host port to a container port), and -e KEY=value (sets an environment variable).
Cricket analogy: docker run is like sending a player onto the field in one motion: -d puts them on as a substitute working quietly (background), --name gives them a jersey number, -p maps the stadium gate to their position, and -e briefs them with match instructions.
# Run an nginx web server in the background, named 'web', on host port 8080
$ docker run -d --name web -p 8080:80 nginx:1.25-alpine
c3f1a9e2d8b7...
# Confirm it's running
$ docker ps
CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES
c3f1a9e2d8b7 nginx:1.25-alpine "/docker-entrypoint.…" Up 3 seconds 0.0.0.0:8080->80/tcp webOnce a container is running, you frequently need to check its logs, open a shell inside it, or view detailed configuration. The commands docker logs, docker exec, and docker inspect cover the vast majority of day-to-day debugging needs.
Cricket analogy: Checking a running container's logs, shell, and config is like reviewing a player's post-match footage (docker logs), talking to them directly in the dugout (docker exec), and reading their full performance report (docker inspect).
# View recent logs, following new output
$ docker logs -f web
# Open an interactive shell inside the running container
$ docker exec -it web sh
/ # ls /usr/share/nginx/html
index.html
/ # exit
# Inspect detailed JSON configuration (IP address, mounts, env vars, etc.)
$ docker inspect webStopping a container with docker stop sends SIGTERM (then SIGKILL after a grace period) but does not delete it; the container still exists and can be restarted with docker start. docker rm permanently removes a stopped container. Because images, containers, networks, and volumes can accumulate over time, docker system prune is a common way to reclaim disk space.
Cricket analogy: docker stop is calling a batsman in for a break (SIGTERM, then forced off with SIGKILL) without retiring them from the squad; docker rm retires them permanently, and docker system prune clears out the old kit bags cluttering the dressing room.
# Stop and remove the container
$ docker stop web
$ docker rm web
# Or do both in one step
$ docker rm -f web
# Remove unused images, stopped containers, and dangling networks
$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
Are you sure you want to continue? [y/N]- Docker Engine is the native install for Linux; Docker Desktop provides a VM-backed install for macOS and Windows.
docker runcreates and starts a container in one command; common flags are -d, --name, -p, and -e.docker pslists running containers; add -a to include stopped ones.docker logs,docker exec, anddocker inspectare the core tools for debugging a running container.docker stophalts a container gracefully;docker rmdeletes it;docker rm -fdoes both.docker system prunereclaims disk space by removing unused containers, networks, and dangling images.
Practice what you learned
1. Why does Docker Desktop on macOS and Windows include a lightweight Linux VM?
2. Which flag makes `docker run` start a container in the background instead of attaching to it?
3. What does `docker ps -a` show that plain `docker ps` does not?
4. What is the difference between `docker stop` and `docker rm`?
5. What does `docker exec -it web sh` do?
Was this page helpful?
You May Also Like
Docker Architecture Overview
An overview of Docker's client-server architecture, including the Docker daemon, CLI client, images, containers, and registries.
Running and Managing Containers
Learn the core Docker CLI commands to start, inspect, stop, and remove containers, plus how to view logs and execute commands inside a running container.
Docker Images Explained
An introduction to what Docker images are, how they differ from containers, and how their layered, read-only filesystem structure works.
Writing a Dockerfile
A practical guide to Dockerfile syntax and the core instructions used to build a custom image from scratch.
Related Reading
Related Study Notes in DevOps
Browse all study notesNginx Study Notes
DevOps · 30 topics
DevOpsAnsible Study Notes
DevOps · 30 topics
DevOpsAdvanced Kubernetes Study Notes
Kubernetes · 30 topics
DevOpsAdvanced Bash Scripting Study Notes
Bash · 30 topics
DevOpsApache Kafka Study Notes
Kafka · 30 topics
DevOpsCI/CD Tools & Pipelines Study Notes
YAML · 37 topics