Docker: OS Level Virtualization
When I started learning Docker, one concept that stood out was OS Level Virtualization. In this post, I’ll explain what it means in simple terms and share the steps I followed with examples you can try yourself.
What is OS Level Virtualization?
Virtualization usually means running multiple systems on the same machine. Traditionally, it works like this:
Hardware → Host OS → Hypervisor → Virtual Machines → Applications
Each virtual machine carries its own OS, which makes it heavy.
With OS Level Virtualization (containers), the picture is a little different:
Hardware → Host OS → Docker Engine → Containers → Applications
Here, containers share the same OS kernel but remain isolated. They are lightweight and faster to start compared to full virtual machines.
Cleaning Up Containers
Before jumping into hands-on steps, it’s good to know how to clean up containers and images. These are some commands I use often:
# Delete a stopped container
docker rm <ContainerName>
# Delete all stopped containers
docker container prune
# Delete stopped containers, unused images, and networks
docker system prune -a
# Delete a running container (stop it first)
docker stop <ContainerName>
docker rm <ContainerName>
Hands-On Example
Let’s go through an example with Ubuntu.
Step 1: Pull and Run Ubuntu
docker pull ubuntu
docker images
docker run -it --name container001 ubuntu
Now you are inside the Ubuntu container.
Step 2: Install Software Inside the Container
apt update -y
apt install git -y
git --version
apt install maven -y
mvn --version
At this point:
Git and Maven are installed inside the container.
You can also create some test files:
echo "print('Hello from Python')" > app.py
echo "class Demo { public static void main(String[] args){ System.out.println(\"Hello from Java\"); }}" > Demo.java
Creating a Custom Image
If you don’t want to repeat the above steps every time, you can turn your container into a new image.
docker commit container001 KASTRO
Now run a new container from the custom image:
docker run -it --name newContainer KASTRO
Check inside:
ls
git --version
mvn --version
Everything you installed and created earlier will still be there. That’s the idea of OS Level Virtualization – a complete backup of the OS environment.
Adding More Tools
You can extend this further by installing other tools like Python or Node.js inside the container:
apt install python3 -y
python3 --version
apt install nodejs -y
node -v
Commit again to create another custom image with these tools included.
Why Use Dockerfile Instead?
While committing containers into images works for practice, it’s not how things are done in real projects. The reason is simple – it’s manual and inconsistent.
The real approach is to use a Dockerfile, where everything is defined once and can be reproduced anywhere.
Example Dockerfile
FROM ubuntu:20.04
RUN apt-get update -y && \
apt-get install -y git maven python3 nodejs
WORKDIR /app
COPY . /app
CMD ["bash"]
Build and Run
docker build -t dev_image .
docker run -it --name devContainer dev_image
This way, every time you or your team builds the image, the same environment is ready without any manual steps.
Conclusion
OS Level Virtualization in Docker is about creating lightweight, isolated environments.
Committing containers into images is a good way to learn and understand how backup works.
In real-world projects, always use dockerfiles for automation and consistency.