If you're starting your journey in DevOps, Cloud, or Kubernetes, one of the first technologies you'll come across is Docker.
When I first heard about Docker, terms like containers, images, Dockerfile, registries, and containerization made it sound more complicated than it actually is.
After working with Docker, I realized that the basic idea is quite simple:
Docker allows us to package an application along with everything it needs to run and then run that package consistently in different environments.
In this article, let's understand Docker from the beginning and then look at some of the fundamental Docker commands.
1. What is Docker?
Docker is a platform that allows us to build, package, and run applications inside containers.
But what exactly does that mean?
Imagine that you have developed a Python application on your laptop.
Your application might require:
- Python 3.12
- Specific Python libraries
- Environment variables
- Configuration files
- Certain system dependencies
Everything works perfectly on your laptop.
Now you give the application to another developer.
They install it and run:
python app.py
And they get:
ModuleNotFoundError
Why?
Because their environment is different.
Maybe they have a different Python version. Maybe a required library is missing. Maybe some system dependency isn't installed.
This is where Docker becomes useful.
Instead of giving someone only your application, you can package the application together with its required environment into a Docker image.
That image can then be used to create a container.
2. What Problem Does Docker Solve?
A common problem in software development is:
"It works on my machine."
Developers may have different:
- Operating systems
- Programming language versions
- Libraries
- Dependencies
- Configurations
- Environment variables
Docker helps reduce these environment differences by providing a consistent way to package and run applications.
For example:
Developer Laptop
|
v
Docker
|
v
Container
|
+---- Application
+---- Dependencies
+---- Required runtime
The same image can then be used in different environments.
Developer Machine
|
v
Docker
|
Container
|
v
App
Test Server
|
v
Docker
|
Container
|
v
App
Cloud Server
|
v
Docker
|
Container
|
v
App
The goal is to make the application environment much more predictable.
3. What is Containerization?
Before understanding Docker deeply, we need to understand containerization.
Containerization is the practice of packaging an application together with the things it needs to run into an isolated environment called a container.
Think of a container like a lightweight box containing your application.
For example:
+--------------------------------+
| Container |
| |
| My Application |
| |
| Dependencies |
| |
| Runtime |
| |
| Configuration |
+--------------------------------+
The application runs inside this isolated environment.
Docker provides the tools and platform that make creating and running these containers easy.
4. Docker Container vs Virtual Machine
A common question when learning Docker is:
"Is a Docker container the same as a virtual machine?"
No.
A virtual machine includes an entire guest operating system.
For example:
Physical Server
|
+-- VM 1
| |
| +-- Guest OS
| +-- Application
|
+-- VM 2
|
+-- Guest OS
+-- Application
Containers work differently.
Containers share the host operating system's kernel while keeping applications isolated from each other.
Physical Server
|
+-----------------------------+
| Host OS |
| |
| +---------+ +---------+ |
| |Container| |Container| |
| | App 1 | | App 2 | |
| +---------+ +---------+ |
| |
+-----------------------------+
Because containers don't need a separate complete guest OS for each application, they are generally much lighter and faster to start than traditional VMs.
5. What is a Docker Image?
A Docker image is a packaged, read-only template used to create containers.
You can think of an image as a blueprint.
For example:
Docker Image
|
| creates
v
Docker Container
If you have an Nginx image, you can create a container from it.
nginx image
|
+------> Container 1
|
+------> Container 2
|
+------> Container 3
The same image can be used to create multiple containers.
6. What is a Docker Container?
A container is a running instance of a Docker image.
For example:
docker run nginx
Docker will use the nginx image to create and start a container.
A simple way to remember this is:
Image = blueprint
Container = running instance created from that blueprint
Similar to:
Class → Object
Image → Container
This isn't a perfect technical analogy, but it's useful when you're learning the concept.
7. What is a Dockerfile?
Now comes another important concept: the Dockerfile.
A Dockerfile contains instructions that Docker uses to build an image.
For example:
FROM nginx
COPY index.html /usr/share/nginx/html/index.html
Here:
FROM nginx
means we're starting with the Nginx image.
And:
COPY index.html /usr/share/nginx/html/index.html
copies our HTML file into the image.
We can then build an image:
docker build -t my-nginx .
And run it:
docker run my-nginx
So the basic flow is:
Dockerfile
|
| docker build
v
Docker Image
|
| docker run
v
Docker Container
This is one of the most important workflows to understand when learning Docker.
8. Docker Registry
Where do Docker images come from?
One common source is a container registry.
A registry is a place where container images can be stored and shared.
One of the most popular public registries is Docker Hub.
For example, you can find images such as:
nginx
redis
postgres
mysql
ubuntu
python
node
When you run:
docker pull nginx
Docker downloads the Nginx image from a registry.
Then you can run it:
docker run nginx
The overall flow looks like:
Docker Registry
|
| docker pull
v
Docker Image
|
| docker run
v
Docker Container
9. Fundamental Docker Commands
Now let's get practical.
You don't need to memorize hundreds of Docker commands initially.
Start with these fundamental commands.
9.1 Check Docker Version
docker --version
Example:
Docker version 28.x.x
You can also use:
docker version
The second command provides more detailed information.
10. Download an Image
Use:
docker pull nginx
This downloads the Nginx image from the configured registry.
You can also specify a version:
docker pull nginx:1.27
Here:
nginx → image name
1.27 → image tag/version
If you don't specify a tag, Docker generally uses the latest tag.
11. List Docker Images
After downloading an image:
docker images
You'll see something similar to:
REPOSITORY TAG IMAGE ID SIZE
nginx latest abc123 192MB
This shows the images available locally.
12. Run a Container
The most important command:
docker run nginx
This tells Docker:
Create a container from the Nginx image and start it.
Docker will:
- Check whether the image exists locally.
- Download it if necessary.
- Create a container.
- Start the container.
13. Run a Container in the Background
By default:
docker run nginx
runs the container attached to your terminal.
To run it in the background:
docker run -d nginx
The -d means:
detached mode
The container continues running in the background.
14. Give a Container a Name
You can assign a name using:
docker run -d --name my-nginx nginx
Now the container is called:
my-nginx
This makes it easier to manage.
For example:
docker stop my-nginx
instead of having to remember a container ID.
15. List Running Containers
Use:
docker ps
Example:
CONTAINER ID IMAGE COMMAND STATUS
abc123 nginx ... Up 2 minutes
This shows currently running containers.
16. List All Containers
What if a container has stopped?
docker ps won't show it.
Use:
docker ps -a
The -a means all containers, including stopped containers.
17. Stop a Container
To stop a running container:
docker stop my-nginx
Docker sends a stop request to the container and allows it to shut down gracefully.
18. Start a Stopped Container
If a container already exists but is stopped:
docker start my-nginx
Notice the difference:
docker run
creates a new container from an image.
docker start
starts an existing container.
This distinction is important.
19. Restart a Container
You can restart a container with:
docker restart my-nginx
20. Remove a Container
To delete a stopped container:
docker rm my-nginx
You can also remove a running container using:
docker rm -f my-nginx
Be careful with -f because it forcefully removes the container.
21. View Container Logs
One of the most useful troubleshooting commands is:
docker logs my-nginx
If the application inside the container is producing logs, this command lets you see them.
You can also follow logs:
docker logs -f my-nginx
The -f means follow.
22. Execute a Command Inside a Container
Sometimes you need to get inside a running container.
For example:
docker exec -it my-nginx /bin/bash
If Bash isn't available, you might use:
docker exec -it my-nginx /bin/sh
The important part is:
docker exec
It allows you to execute a command inside an already running container.
23. Port Mapping
Here's an important concept.
Suppose Nginx is running inside a container on port 80.
That doesn't automatically mean you can access it through your host's port 80.
You can map a host port to the container port:
docker run -d --name my-nginx -p 8080:80 nginx
The format is:
-p HOST_PORT:CONTAINER_PORT
So:
8080:80
means:
Host port 8080
|
v
Container port 80
You can then access Nginx through:
http://localhost:8080
24. View Container Details
Use:
docker inspect my-nginx
This provides detailed information about the container, including things such as:
- Network configuration
- Mounts
- Environment
- Container configuration
- IP information
- Runtime settings
It's extremely useful when troubleshooting.
25. A Small Docker Practice
Let's put some of these commands together.
First download Nginx:
docker pull nginx
Run it:
docker run -d --name my-nginx -p 8080:80 nginx
Check that it's running:
docker ps
Open:
http://localhost:8080
You should see the Nginx welcome page.
Check the logs:
docker logs my-nginx
Enter the container:
docker exec -it my-nginx /bin/bash
Exit:
exit
Stop the container:
docker stop my-nginx
Start it again:
docker start my-nginx
Finally remove it:
docker stop my-nginx
docker rm my-nginx
This small exercise gives you hands-on experience with the basic Docker lifecycle.
26. Docker's Basic Architecture
At a high level, you can think about Docker like this:
Docker Registry
|
| pull
v
+-------------+
| Docker Image|
+-------------+
|
| run
v
+-------------+
| Container |
+-------------+
|
v
Application
And when you create your own application:
Dockerfile
|
| docker build
v
Docker Image
|
| docker run
v
Container
|
v
Application
Understanding these two flows will make many other Docker concepts easier later.
27. Why is Docker Important for DevOps?
Docker became extremely important in DevOps because it makes application packaging and deployment more consistent.
A typical workflow might look like:
Developer
|
| writes code
v
Dockerfile
|
| docker build
v
Docker Image
|
| push
v
Container Registry
|
| pull
v
Server
|
| docker run
v
Container
This fits naturally into CI/CD pipelines.
For example:
Code Change
|
v
CI Pipeline
|
v
Build Docker Image
|
v
Test Image
|
v
Push Image
|
v
Deploy
This is one reason Docker is such an important technology to understand if you're moving toward DevOps.
28. Docker and Kubernetes
If you're learning Kubernetes, understanding Docker concepts is still very useful.
Kubernetes manages containers, but Kubernetes itself is not simply "Docker."
Modern Kubernetes clusters use a container runtime such as containerd or CRI-O.
So think about the relationship like this:
Docker
|
| helps you build/package/run containers
v
Container Image
|
v
Container Runtime
|
v
Kubernetes
|
v
Manages containers across multiple machines
Docker concepts such as:
- Images
- Containers
- Registries
- Container networking
- Volumes
- Environment variables
are foundational concepts that will help you understand Kubernetes.
29. Docker Commands Cheat Sheet
Here are the commands worth practicing first:
| Command | Purpose |
|---|---|
docker --version |
Check Docker version |
docker pull nginx |
Download an image |
docker images |
List images |
docker run nginx |
Create and run a container |
docker run -d nginx |
Run in background |
docker ps |
List running containers |
docker ps -a |
List all containers |
docker stop <container> |
Stop a container |
docker start <container> |
Start a stopped container |
docker restart <container> |
Restart a container |
docker rm <container> |
Remove a container |
docker logs <container> |
View logs |
docker exec -it <container> /bin/sh |
Execute a command inside container |
docker inspect <container> |
View detailed container information |
docker build -t <name> . |
Build an image |
docker push <image> |
Push an image to a registry |
Don't try to memorize all of these immediately.
Practice them.
The commands will become familiar naturally.
30. Final Thoughts
Docker can look complicated when you first encounter terms like images, containers, Dockerfiles, registries, volumes, networks, and containerization.
But the fundamental idea is straightforward:
Build an image → create a container → run your application.
The basic workflow is:
Application
|
v
Dockerfile
|
| docker build
v
Docker Image
|
| docker run
v
Docker Container
|
v
Running Application
Once this workflow becomes clear, you can start learning more advanced Docker concepts such as:
- Docker volumes
- Docker networks
- Environment variables
- Multi-stage builds
- Docker Compose
- Image optimization
- Container security
- Private registries
- CI/CD with Docker
- Docker and Kubernetes
Docker is not just another tool to memorize for a DevOps interview. The important thing is to understand why containers exist, how images become containers, and how Docker fits into the software delivery process.
That's the foundation on which many modern DevOps and Kubernetes workflows are built.
Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.