Module 2 Lesson 3: Core Docker CLI Commands
·DevOps

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 -a to 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 -f to 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

CommandActionWhen to use it?
pullDownloadYou need a new tool/OS
runCreate + StartYou want to launch an app
psList"Is my app running?"
stopShutdownYou want to save resources
rmDeleteCleanup old containers
imagesList"How much space is used?"
execAccessDebugging inside a container

Exercise: The Command Sequence

Perform the following actions in your terminal:

  1. Pull the image alpine (a tiny Linux distribution).
  2. List your images to confirm it's there.
  3. Run a container from alpine with the name my-test and have it run the command echo "hello docker".
  4. Run docker ps. Why is the container not in the list? (Hint: what happens when a command finishes?)
  5. Run docker ps -a. Is it there now? What is its "Status"?
  6. Cleanup: Remove the my-test container and the alpine image.

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.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn