Docker for Beginners: Your First Container in 20 Minutes
"It works on my machine." If you have ever heard those four words on a development team,
you already understand why Docker exists. Docker solves the age-old problem of
environment inconsistency by letting you package your application and all its
dependencies into a single, portable container that runs identically everywhere —
on your laptop, your teammate's machine, and the production server.
In 2026, Docker knowledge is a baseline expectation for backend developers, full-stack engineers, and anyone touching deployment or DevOps. This guide skips the theory overload and gets you hands-on immediately — with real commands, clear output explanations, and a working container by the end.
A computer running Windows 10/11, macOS, or Ubuntu Linux. Basic comfort using a terminal or command prompt. That's it — no prior Docker or DevOps knowledge required. 👉 Want to go deeper? Join K2Infocom's Join Cloud & DevOps Masterclass
1. What is Docker? (The 2-Minute Explanation)
Before touching a single command, you need a clear mental model of what Docker actually is. Think of it this way:
Imagine you are shipping a product internationally. You don't hand the factory worker a pile of loose parts and hope they assemble it correctly — you pack everything into a standardised shipping container that can be loaded onto any truck, ship, or train without modification. Docker does exactly this for software.
Key Docker Concepts You Must Know:
- Image: A read-only blueprint for your container — like a recipe or a class definition. It contains your app, its runtime, dependencies, and configuration.
- Container: A running instance of an image — like an object created from a class. You can run dozens of containers from the same image simultaneously.
- Dockerfile: A text file with step-by-step instructions to build your own custom image. Think of it as the recipe you write yourself.
- Docker Hub: A public registry of pre-built images — like GitHub but for Docker images. Contains official images for Node.js, Python, Nginx, MySQL, and thousands more.
- Docker Engine: The background service running on your machine that builds and runs containers. It's the engine that powers everything.
2. Install Docker on Your Machine (5 Minutes)
Docker provides a free tool called Docker Desktop that installs everything you need — the Docker Engine, a GUI dashboard, and the Docker CLI — in one package.
Installation by Operating System:
-
Windows 10/11: Download Docker Desktop from
https://www.docker.com/products/docker-desktop. Run the installer. Make sure WSL 2 (Windows Subsystem for Linux) is enabled — the installer will prompt you if it isn't. Restart your machine after installation. -
macOS (Intel or Apple Silicon): Download the correct Docker Desktop
build for your chip from the same URL. Open the
.dmgfile, drag Docker to Applications, and launch it. Allow the system extension when prompted. -
Ubuntu Linux: Run the following commands in your terminal one by one:
sudo apt-get update
sudo apt-get install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker $USER
Log out and back in after the last command so the group change takes effect.
Verify the Installation:
Open your terminal (or PowerShell on Windows) and run:
docker --version
You should see something like: Docker version 26.1.0, build a7c6e89.
If you do — Docker is installed and ready. You're already ahead of most beginners.
3. Pull Your First Image from Docker Hub (2 Minutes)
Docker Hub is the world's largest library of container images. Your first task is to pull the official "hello-world" image — a tiny image created specifically to test that Docker is working correctly on your machine.
Run this command in your terminal:
docker pull hello-world
You'll see Docker downloading the image layer by layer:
Using default tag: latest
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:4f53e2564...
Status: Downloaded newer image for hello-world:latest
Now verify it's stored locally with:
docker images
This lists all images on your machine. You should see hello-world in the list
with its size (about 13 KB — incredibly small). That's your first Docker image.
Docker contacted Docker Hub, authenticated (anonymously for public images), and downloaded the image in compressed layers to your local machine. These layers are cached — so future pulls of images that share layers will be much faster.
4. Run Your First Container (2 Minutes)
An image sitting on your machine does nothing on its own. You need to run it as a container. Here's the command:
docker run hello-world
Docker will create a container from the image, execute it, and you'll see output like this:
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...
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
Congratulations — you just ran your first Docker container. It printed its message and exited cleanly. That's the full lifecycle of a short-lived container.
Now run this to see all containers (including stopped ones):
docker ps -a
You'll see your hello-world container listed with a status of Exited (0) —
meaning it ran successfully and terminated. The container ID, image name, creation time,
and exit status are all visible here.
5. Run a Real App — Nginx Web Server in a Container (3 Minutes)
Now let's do something more meaningful. We'll run Nginx — a production-grade web server — inside a Docker container and access it from your browser, all without installing Nginx on your machine.
Run this command:
docker run -d -p 8080:80 --name my-nginx nginx
Breaking down each flag:
-d— Detached mode: Runs the container in the background so your terminal is free. Without this, the container output would stream to your terminal and block it.-p 8080:80— Port mapping: Maps port 8080 on your machine (host) to port 80 inside the container. This is how you access the app running inside the container from your browser.--name my-nginx— Container name: Gives the container a readable name instead of the auto-generated random one Docker assigns by default.nginx— The name of the image to use. Docker will pull it from Docker Hub automatically if it's not already on your machine.
Now open your browser and go to: http://localhost:8080
You'll see the official Nginx welcome page — "Welcome to nginx!" — being served from inside a Docker container. You have a fully running web server that you installed in a single command.
6. Essential Docker Commands Every Developer Must Know
You now have hands-on context for these commands. Here is your Docker quick-reference cheat sheet — the commands you'll use daily as a developer.
Container Management:
docker ps— List all running containersdocker ps -a— List all containers including stopped onesdocker stop my-nginx— Gracefully stop a running containerdocker start my-nginx— Start a stopped containerdocker restart my-nginx— Restart a containerdocker rm my-nginx— Delete a stopped containerdocker rm -f my-nginx— Force-delete a running containerdocker logs my-nginx— View the output/logs of a containerdocker logs -f my-nginx— Follow live logs (like tail -f)docker exec -it my-nginx bash— Open an interactive shell inside a running container — incredibly useful for debugging
Image Management:
docker images— List all images on your machinedocker pull node:20— Pull a specific image and version (tag)docker rmi nginx— Delete an image from your machinedocker build -t my-app .— Build an image from a Dockerfile in the current directorydocker image prune— Remove all unused/dangling images to free disk space
System:
docker info— Show Docker system-wide informationdocker system prune— Remove all stopped containers, unused images, and unused networks in one commanddocker stats— Live CPU, memory, and network usage for all running containers
7. Write Your First Dockerfile (5 Minutes)
Pulling and running existing images is great — but the real power of Docker is building your own image from your own application. That's what a Dockerfile is for.
Let's containerise a simple Node.js app. Create a new folder called
my-docker-app and add these three files:
File 1: app.js
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello from my Docker container! 🐳\n');
});
server.listen(3000, () => console.log('Server running on port 3000'));
File 2: package.json
{"name": "my-docker-app", "version": "1.0.0", "main": "app.js"}
File 3: Dockerfile (no file extension)
# Use the official Node.js 20 image as base
FROM node:20-alpine
# Set the working directory inside the container
WORKDIR /app
# Copy package.json first (for layer caching)
COPY package.json .
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Tell Docker which port the app listens on
EXPOSE 3000
# The command to run when the container starts
CMD ["node", "app.js"]
Build and run your custom image:
docker build -t my-docker-app .
docker run -d -p 3000:3000 --name my-app my-docker-app
Open http://localhost:3000 in your browser.
You'll see: "Hello from my Docker container! 🐳"
You just wrote a Dockerfile, built a custom image from your own code, and ran it as a container. This is exactly how real-world applications are containerised before being deployed to cloud platforms like AWS ECS, Google Cloud Run, or Kubernetes.
Always use alpine variants of base images (e.g.,
node:20-alpine)
— they are dramatically smaller than the default images. Copy package.json
and install dependencies before copying your source code — this way Docker
caches the dependency layer and rebuilds are much faster when you only change your app code.
8. What to Learn Next: Your Docker Roadmap
You've covered the essentials. Here is the natural progression to go from Docker beginner to production-ready DevOps engineer:
-
Docker Compose: The next immediate skill to learn. Compose lets you
define and run multi-container applications (e.g., your Node.js app +
a PostgreSQL database + a Redis cache) with a single
docker-compose.ymlfile and one command:docker compose up. - Docker Volumes: Understand how to persist data between container restarts — critical for databases and any stateful application running in Docker.
- Docker Networking: Learn how containers communicate with each other using Docker's internal network — bridge, host, and overlay network modes.
- Pushing to Docker Hub / ECR: Learn to push your custom images to a container registry so they can be pulled and deployed anywhere in the world.
- CI/CD with Docker: Integrate Docker builds into GitHub Actions or Jenkins pipelines so your app is automatically containerised and deployed on every push.
- Kubernetes (K8s): The natural next step after Docker — orchestrating dozens or hundreds of containers across clusters, with auto-scaling, self-healing, and rolling deployments.
Developers who know Docker earn 20–40% more than those who don't, according to Stack Overflow's 2025 Developer Survey. In India, Docker/DevOps skills push freshers from ₹4–6 LPA packages into ₹10–18 LPA territory. It's one of the highest-ROI skills you can add to your profile in 2026. 👉 Start your DevOps journey with K2Infocom's Free Masterclass →