Your First Container – Hello World in Docker
If you have ever heard the excuse, "But it works on my machine!", then you already know why Docker was created. In my 15+ years of software architecture, moving from heavy virtual machines to lightweight Docker containers was one of the biggest paradigm shifts. Whether you are deploying a C# Web API, running a Python Data Science script, or serving an AI/ML model on AWS, containers ensure your code runs exactly the same way everywhere.
Today, we are going to break down the magic. You will run your very first Docker container, understand what happens under the hood, and learn the essential commands you will use every single day as a developer.
- The difference between a Docker Image and a Docker Container.
- How to pull and run your first container using the
docker runcommand. - How the Docker architecture processes your commands.
- Essential lifecycle commands like
docker psanddocker stop.
1. Images vs. Containers: The Core Concept
Before we type any commands, we need to clear up the biggest point of confusion for beginners: the difference between an Image and a Container.
Think of an Image as a blueprint or a recipe. It contains all the source code, libraries, dependencies, tools, and files needed to run an application. A Container is the actual running instance of that image. You can use one single blueprint (Image) to build hundreds of identical houses (Containers).
| Feature | Docker Image | Docker Container |
|---|---|---|
| What is it? | A read-only blueprint containing your app and environment. | A running process created from that blueprint. |
| State | Static (Never changes once built). | Dynamic (Running, stopping, restarting). |
| Analogy | The Class in Object-Oriented Programming (OOP). | The Object (Instance) created from that Class. |
2. Running Your First Container
To prove that your Docker installation is working properly, we are going to run the official "hello-world" image. This is a tiny, free, open-source image provided by Docker specifically for testing.
The "docker run" Command
Open your terminal or command prompt and type the following command:
Example 1: Running the hello-world image
docker run hello-world
Expected output
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:4bd78111b6914a99dbc56f161f3...
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
sudo docker run hello-world or add your user to the docker group.3. The Architecture Visualized
When you hit Enter on that command, a lot happened behind the scenes in just a few milliseconds. Let's look at the Docker architecture.
Docker Execution Workflow
When you issue a command, the Docker Client sends it to the Docker Daemon. The Daemon checks for the image locally, pulls it from the cloud if needed, and spins up the container.
4. Step-by-Step Execution Breakdown
Let's break down exactly what happened when you ran that simple command. Understanding this flow is critical for debugging complex microservices later on.
docker run hello-world. The Docker Client translated this into a REST API call and sent it to the Docker Daemon (the background service running on your machine).5. Essential Container Commands
Running a container is great, but you also need to manage them. Here are the three most important commands you need to memorize.
Checking Running Containers
To see what containers are currently actively running on your machine, use the ps (process status) command:
docker ps
If you run this right now, the list will likely be empty. Why? Because the hello-world container already finished its job and stopped! To see all containers, including the stopped ones, add the -a (all) flag:
docker ps -a
Stopping a Container
If you have a container running a web server (like an Angular UI or a .NET Core API), it will run forever until you tell it to stop. You can stop it by providing the container's ID or name (which you find using docker ps).
docker stop [CONTAINER_ID]
Removing a Container
Stopped containers still take up a little bit of disk space. To clean up your system, you can delete a stopped container using the rm (remove) command:
docker rm [CONTAINER_ID]
6. Hands-On Practice
Now it is your turn to take the wheel. Try these exercises in your terminal.
Use the correct command to find the Container ID of the `hello-world` container you just ran.
Hint: Use the command that lists all containers, not just the running ones.
Copy the Container ID from Exercise 1 and permanently delete that container from your machine.
Expected result: The terminal will echo back the ID you typed, confirming deletion. If you run docker ps -a again, it will be gone.
Run docker run -it ubuntu bash. This will pull the Ubuntu Linux image and drop you directly into its command line terminal!
Hint: The -it flags stand for "interactive" and "tty". Type exit when you are done playing around to stop the container and return to your own machine.
7. Interview Questions
When interviewing for Senior Software Engineer or Architect roles, hiring managers will test if you actually understand how containers work, rather than just knowing the commands.
Answer: A Virtual Machine includes a full, heavy Guest Operating System (like Windows or Linux) layered on top of a Hypervisor, which consumes massive amounts of RAM and CPU. A Docker Container, however, shares the Host machine's Operating System kernel. It only contains your application and its immediate dependencies, making it incredibly lightweight, fast to start, and efficient for microservices.
Answer: The Docker Daemon will automatically attempt to pull the missing image from the configured Docker registry (which defaults to Docker Hub) before executing it.
Conclusion
You have just taken your first step into the world of containerization! By mastering images, containers, and the basic Docker lifecycle commands, you are laying the groundwork for modern system architecture. Whether you are building complex distributed systems, deploying databases like MS SQL Server and MongoDB, or managing open-source Data Science workflows, Docker ensures your environment is consistent, predictable, and scalable.
Official Documentation
Upgradix by Shrash Studio – Learn Technology. Build Your Future.