How to use docker for beginners (A “hello world” guide)
If you are a developer, a system administrator, or just someone interested in modern technology, you have probably heard of Docker. It is a powerful platform that has revolutionized how applications are built, shipped, and run. But for beginners, the concepts of containers, images, and Dockerfiles can seem overwhelming. This guide is designed to demystify Docker by taking you through the essential first steps: setting it up and running your very first container, the quintessential “Hello World.” We will break down the core components, provide clear instructions for installation, and execute the basic commands necessary to see Docker in action. By the end of this tutorial, you will have a solid foundation to explore more complex containerization tasks, turning abstract concepts into practical skills.
Installing docker: getting started
Before we can run any containers, we need to ensure Docker is properly installed on your system. Docker provides installers for all major operating systems, bundled under the name Docker Desktop for Windows and macOS, and standard package managers for Linux distributions. Using Docker Desktop is highly recommended for beginners as it includes the necessary Docker Engine, Docker CLI (Command Line Interface) client, Docker Compose, and a user friendly GUI.
For Windows and macOS, visit the official Docker website and download the Docker Desktop installer. The installation process is straightforward: run the installer and follow the on-screen prompts. Once installation is complete, you must ensure Docker is running. On Windows, you might need to enable WSL 2 (Windows Subsystem for Linux 2) as Docker Desktop relies heavily on virtualization technology to run Linux containers natively. On both operating systems, look for the Docker whale icon in your system tray or menu bar. A stable, running Docker Desktop usually displays a green light or a similar indicator confirming the service is active.
To confirm your installation and check the version, open your terminal (Command Prompt, PowerShell, or Bash) and execute the following command:
docker --version
You should see output similar to Docker version 24.0.5, build 24.0.5-0ubuntu1~22.04.1 (the version numbers will vary). This confirmation means your environment is ready to start interacting with containers.
Understanding the core concepts: images and containers
Before executing the “Hello World,” it is crucial to grasp the fundamental distinction between Docker Images and Containers. Think of a Docker Image as a blueprint or a template. It is a lightweight, standalone, and executable package that includes everything needed to run a piece of software, including the code, runtime, libraries, environment variables, and configuration files. Images are read-only and are built from instructions defined in a Dockerfile.
A Docker Container, on the other hand, is a running instance of an Image. When you execute an image, Docker creates a container, which is an isolated process running on your host machine. This isolation is key to Docker’s success, ensuring that the application within the container operates consistently regardless of the underlying infrastructure. Multiple containers can run simultaneously from the same image without interfering with each other.
Let us compare these concepts to object oriented programming or virtualization:
| Concept | Analogy | Docker term |
|---|---|---|
| Template/Blueprint | Class Definition or VM Template | Image |
| Running Instance | Object Instantiation or Running VM | Container |
| Instructions to build | Source Code | Dockerfile |
Understanding this relationship ensures you know what commands like docker pull (fetching an image) and docker run (creating and starting a container) actually achieve.
Executing your first container: the “hello world” test
With Docker installed and the core concepts understood, we can now run the classic “Hello World” example. This process demonstrates Docker’s basic workflow: pulling an image and running it as a container. Open your terminal and type the following command:
docker run hello-world
When you press Enter, Docker performs several steps automatically:
- Docker checks if the
hello-worldimage is locally available on your machine. Since this is likely your first time running it, Docker cannot find it. - Docker connects to the default image registry (Docker Hub) and pulls (downloads) the
hello-worldimage. You will see a message indicating the download progress. - Once the image is downloaded, Docker creates and starts a new container based on that image.
- The container executes the simple program contained within the image, which prints the output: Hello from Docker! along with some explanatory text about what Docker just did.
- Since the program finishes immediately after printing the message, the container stops and exits.
This single command beautifully encapsulates the entire container lifecycle for a basic application. To prove that the image is now saved locally, run the following command:
docker images
You will see hello-world listed among your local images. Similarly, you can check the status of all containers (even stopped ones) using:
docker ps -a
You will find the container ID and status for the hello-world execution, confirming it ran and exited.
Beyond hello world: running an interactive container
While hello-world is a great starting point, most real world containers need to run continuously or interactively. To demonstrate a slightly more useful application, let us run a simple Linux distribution, like Ubuntu, interactively. This allows us to prove the container’s isolation and functional environment.
Use the following command:
docker run -it ubuntu bash
Let us break down the flags used here:
run: Creates and starts a new container.-i(interactive): Keeps the standard input (stdin) open, allowing you to interact with the container.-t(tty): Allocates a pseudo TTY (terminal), which is necessary for a command line interface experience.ubuntu: The image we want to run (if not local, Docker pulls it from Docker Hub).bash: The command we execute inside the container, starting the Bash shell.
After executing this, your terminal prompt will change, indicating you are now inside the running Ubuntu container. You can run standard Linux commands, such as ls / or cat /etc/os-release, and you will confirm that you are operating within a fresh, isolated Ubuntu environment, even if your host machine runs Windows or macOS. To exit the container and return to your host terminal, simply type exit. The container will stop running but will remain listed when you use docker ps -a.
This exercise confirms the power of containers: providing consistent and isolated environments instantly, without the overhead of traditional virtual machines.
We have successfully navigated the first crucial steps in using Docker, transforming confusing jargon into executable commands. Starting with the essential installation of Docker Desktop, we established a working environment ready for containerization. We then clarified the pivotal relationship between Docker Images (the blueprint) and Docker Containers (the running instance), which is key to understanding the platform’s architecture. The subsequent execution of the hello-world container provided immediate, visible proof of Docker’s capabilities in pulling, creating, and running processes in isolation. Finally, by running an interactive Ubuntu container, we demonstrated how Docker provides immediate access to fully isolated environments, confirming the practical application of these foundational concepts. You are now equipped with the fundamental knowledge to run basic containers and confidently continue your Docker journey, perhaps moving on to Dockerfiles and networking next.
Image by: Wolfgang Weiser
https://www.pexels.com/@wolfgang-weiser-467045605