Run Your First Docker Container | Docker Run Command Explained
Mastering the mechanics of the docker run command, understanding the container lifecycle, managing base images (Ubuntu, Alpine, BusyBox), and bridging the gap between Command Line Interface (CLI) workflows and Docker Desktop graphical management.
1. Verifying Installation & Service Status
Before deploying containers, it is essential to verify that the Docker daemon is installed correctly and running on your host system (e.g., Ubuntu Linux EC2 instance or local development machine).
-
Check Docker Version: Execute
docker --version(ordocker version) to inspect the currently installed package version.BASHdocker --version # Output example: Docker version 29.2.1, build ... -
Verify Daemon State via Systemd: Manage the Docker service using standard Linux
systemctlcommands.- Check status:
sudo systemctl status docker - Stop service:
sudo systemctl stop docker - Start service:
sudo systemctl start docker - Restart service:
sudo systemctl restart docker
- Check status:
2. Core Mental Model: Images vs. Containers
To understand what happens when you execute Docker commands, you must distinguish between the two primary entities:
- Docker Image: A read-only template or blueprint containing application code, libraries, dependencies, and environment variables. It is stored statically on disk or in a registry like Docker Hub.
- Docker Container: A running, instantiated process derived from a Docker Image. When an image is executed, Docker layers a writable file system on top of it, turning the static blueprint into an active computing environment.
Real-Life Analogy: Think of a Docker Image as a blueprinted architectural blueprint or a sealed baking mold. The Docker Container is the actual physical cake baked from that mold. You can use the exact same blueprint (image) to bake multiple independent cakes (containers) simultaneously.
3. Dissecting the docker run hello-world Command
When you first installed Docker, you likely ran docker run hello-world. Behind this single command, Docker executes a precise multi-step operational workflow.
Backstage Execution Flow
4. Inspecting Local Images and Containers
Docker provides straightforward CLI commands to inspect what resources exist on your host machine.
-
List Local Images:
BASHdocker imagesKey columns explained:
- REPOSITORY: The name of the image (e.g.,
hello-world,nginx,ubuntu). - TAG: The specific version specifier (defaults to
latestif omitted). - IMAGE ID: A unique SHA hash used for precise identification.
- CREATED / SIZE: Age and disk footprint of the image.
- REPOSITORY: The name of the image (e.g.,
-
List Running Containers:
BASHdocker psNote:
docker psonly shows active containers. For transient containers likehello-worldthat execute a short-lived task and terminate,docker pswill return an empty list. -
List All Containers (Including Stopped):
BASHdocker ps -aThis reveals exited containers, their container IDs, creation timestamps, and exit codes.
5. Working with Nginx Web Server & Port Mapping
Unlike hello-world, a web server like Nginx needs to run continuously in the background and accept incoming network traffic from the host machine or internet.
Pulling and Running Nginx in Background Daemon Mode (-d)
-
Pull the Nginx Image:
BASHdocker pull nginx -
Run with Port Mapping (
-p) and Daemon Mode (-d):BASHdocker run -d -p 8000:80 nginx-d: Run container in detached mode (in the background as a daemon process).-p 8000:80: Map host port8000to container port80(where Nginx listens internally).
Container Port Forwarding Architecture
External Browser / Client
|
| HTTP Request to http://<Public-IP>:8000
v
Host Machine (EC2 / Localhost: Port 8000)
|
| Forwards traffic via Docker network bridge
v
Nginx Container (Internal Port 80)
6. Container Lifecycle Management Commands
Managing running and stopped containers is handled via intuitive lifecycle commands:
7. Linux Distribution Comparison: Ubuntu vs. Alpine vs. BusyBox
Docker allows you to pull and execute entire Linux operating system environments as containers. However, image size and resource efficiency vary dramatically across distributions.
Comparative Image Size & Capabilities
8. Interactive Terminal Mode (-it)
To step directly inside a container's shell and execute Linux commands interactively, combine the -i (interactive) and -t (tty) flags:
- Launch Interactive Ubuntu Shell:
Once inside, you are logged in asBASH
docker run -it ubunturoot. You can verify the OS release:BASHcat /etc/os-release - Launch Interactive Alpine Shell:
Verify Alpine release:BASH
docker run -it alpineBASHcat /etc/os-release - Exit the Container: Type
exitto terminate the interactive shell session and stop the container.
9. Docker Desktop vs. CLI Workflow
While the CLI is powerful for automation and scripting, Docker Desktop provides a fully featured Graphical User Interface (GUI) for developers who prefer visual management:
- Searching Images: Navigate to the Docker Hub / Search tab inside Docker Desktop, type the image name (e.g.,
ubuntu), and select official repositories instantly without opening a web browser. - Pulling & Running: Click the Pull button to download image layers visually, then click Run to configure port mappings and environment variables via modal dialog boxes.
- Container Inspection: Use the Containers tab to inspect resource utilization (CPU/Memory), view live logs, and open an integrated terminal shell directly within the browser UI.
Summary and Key Takeaways
- Initialization Verification: Always verify your installation status using
docker --versionandsystemctl status dockerbefore running containers. - Images vs. Containers: An image is a static blueprint (class), while a container is an active running instance of that blueprint (object).
- The
docker runLifecycle: Docker automatically searches local cache first, pulls from Docker Hub if missing, creates a container layer, and executes the default entrypoint. - Port Mapping (
-p): To expose internal container services (like Nginx on port80) to the outside world, explicitly map host ports (e.g.,-p 8000:80). - Detached Mode (
-d): Run containers in the background as daemon processes so your terminal remains free for further commands. - Resource Optimization: Heavy base images like Ubuntu (
~190 MB) offer full package managers, whereas lightweight alternatives like Alpine (~13 MB) drastically reduce disk footprint and security attack surfaces. - Interactive Shells (
-it): Usedocker run -it <image>to open an interactive terminal session inside a container for debugging and hands-on Linux exploration. - Cleanup Discipline: Stop unused containers with
docker stop, remove them withdocker rm, and prune dangling images withdocker rmito maintain a clean workspace.