Module 2 Lesson 3: Core Docker CLI Commands
Learn the vocabulary of Docker. Master the fundamental commands for pulling images, running containers, and managing your local environment.
Module 2 Lesson 3: Core Docker CLI Commands
Docker is primarily a command-line tool. While the GUIs are nice, a professional engineer needs to be fast in the terminal. This lesson covers the "Essential 8" commands.
1. Image Commands (The Recipes)
docker pull <image>
Downloads an image from Docker Hub.
- Try it:
docker pull nginx
docker image ls (or docker images)
Lists all images currently stored on your computer.
- Look for: Repository, Tag (version), and Size.
2. Container Commands (The Instances)
docker run <image>
Creates AND starts a container. If the image isn't local, it pulls it first.
- Common Flags:
-d: Detached mode (runs in the background).-it: Interactive terminal (allows you to type inside the container).--name: Give your container a friendly name.
- Try it:
docker run --name my-web -d nginx
docker ps
Lists all running containers.
- Tip: Use
docker ps -ato see ALL containers, including stopped ones.
docker stop <container-id/name>
Gracefully shuts down a container.
- Try it:
docker stop my-web
3. Management & Cleanup
docker rm <container-id/name>
Deletes a stopped container.
- Tip: Use
-fto force remove a running one (not recommended!).
docker rmi <image-id>
Deletes an image from your computer. (You can't delete an image if a container is currently using it).
docker exec -it <container-name> bash
"Jumps inside" an already running container.
- Try it:
docker run -d --name helper alpine sleep 1000->docker exec -it helper sh.
4. The Summary Table
| Command | Action | When to use it? |
|---|---|---|
pull | Download | You need a new tool/OS |
run | Create + Start | You want to launch an app |
ps | List | "Is my app running?" |
stop | Shutdown | You want to save resources |
rm | Delete | Cleanup old containers |
images | List | "How much space is used?" |
exec | Access | Debugging inside a container |
Exercise: The Command Sequence
Perform the following actions in your terminal:
- Pull the image
alpine(a tiny Linux distribution). - List your images to confirm it's there.
- Run a container from
alpinewith the namemy-testand have it run the commandecho "hello docker". - Run
docker ps. Why is the container not in the list? (Hint: what happens when a command finishes?) - Run
docker ps -a. Is it there now? What is its "Status"? - Cleanup: Remove the
my-testcontainer and thealpineimage.
Summary
These 8 commands will cover 90% of your daily work with single containers. Mastering them makes the more advanced automation tools (like Compose) much easier to understand.
Next Lesson: We dive deeper into the storage layer: Understanding images and containers.