0
0
Dockerdevops~5 mins

Containers vs virtual machines in Docker - CLI Comparison

Choose your learning style9 modes available
Introduction
Containers and virtual machines both help run software in isolated environments, but they do it differently. Containers share the host system's resources efficiently, while virtual machines run full operating systems, which can be heavier.
When you want to run multiple apps on the same server without them interfering with each other.
When you need fast startup times for your applications.
When you want to save system resources like CPU and memory.
When you need full isolation with separate operating systems for security or compatibility.
When you want to test software in different operating systems on the same hardware.
Commands
This command runs a lightweight container using the Alpine Linux image. It starts a shell inside the container so you can see how containers share the host OS kernel but run isolated processes.
Terminal
docker run --rm -it alpine sh
Expected OutputExpected
/ #
--rm - Automatically removes the container after you exit.
-it - Runs the container interactively with a terminal.
Shows running containers. After exiting the previous container, this confirms no containers are running, demonstrating how containers are temporary and lightweight.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Displays the host operating system information to highlight that containers share the host OS kernel.
Terminal
docker info | grep 'Operating System'
Expected OutputExpected
Operating System: Docker Desktop
Key Concept

Containers share the host operating system kernel and isolate applications, making them lightweight and fast compared to virtual machines that run full guest operating systems.

Common Mistakes
Trying to run a container with a different OS kernel than the host.
Containers cannot run a different kernel; they share the host's kernel, so OS-level differences can cause issues.
Use virtual machines if you need a different OS kernel or full OS isolation.
Leaving containers running without cleanup.
This wastes system resources and can clutter your environment.
Use the --rm flag or manually stop and remove containers when done.
Summary
Run a lightweight container with 'docker run' to see how containers share the host OS kernel.
Use 'docker ps' to check running containers and understand their temporary nature.
Containers are faster and use fewer resources than virtual machines because they do not run full operating systems.