Cloud & DevOps6 min read10 interview questions
Docker containers or virtual machines
Two ways to run software in isolation, and when to use each.
In one sentenceA virtual machine pretends to be a whole computer. A container shares the real computer's core but keeps your app and its files separate.
Think of it like thisA virtual machine is a separate house with its own plumbing. A container is a flat in a shared building: private rooms, shared pipes.
The big picture
Tap any step to see where to use it, how to use it and an example result.
Virtual machines
Each virtual machine includes a full operating system on top of a layer that pretends to be hardware. They are strongly isolated and can run different operating systems, but they are heavy: gigabytes in size and slower to start.
Containers
Containers share the host's operating system core and package only the app and its dependencies. They are small, start in seconds and give the same result on your laptop and on a server. They are isolated, but less strongly than virtual machines.
Using both
Real systems often use both: containers for the apps, running inside virtual machines that give stronger separation between customers.
Side by side
| Feature | Virtual machine | Container |
|---|---|---|
| Includes | Full operating system | App and libraries only |
| Size | Gigabytes | Megabytes |
| Start time | Minutes | Seconds |
| Isolation | Strong | Good, but lighter |
| Best for | Different operating systems, strong separation | Packaging and moving apps |
Common mistakes
- Treating a container like a small server you log in to and change by hand. Rebuild the image instead.
- Storing important data inside a container. It disappears when the container is removed.
- Running everything as the administrator user inside a container.
Quick recap
- Containers share the host core, virtual machines do not
- Containers are small and fast to start
- Virtual machines isolate more strongly
- Data you need to keep goes in a volume, not in the container
Try it yourself
Install Docker and run docker run hello-world. Then write a three-line Dockerfile that runs a Python script.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is a container?Basic
A packaged application with everything it needs to run, isolated from other processes but sharing the host machine's operating system kernel.
What is a virtual machine?Basic
A full computer simulated in software. It has its own operating system on top of virtual hardware, managed by a hypervisor.
What is the main difference between a container and a VM?Basic
A VM includes a whole guest operating system, so it is heavier and slower to start. A container shares the host kernel, so it is lighter and starts in seconds, but isolation is not as strong.
What is the difference between a Docker image and a container?Basic
An image is the read-only template, like a recipe or a class. A container is a running instance created from it, like a dish or an object.
What is a Dockerfile?Intermediate
A text file with the steps to build an image: choose a base image, copy in code, install dependencies and set the command to run.
How do containers keep data when they are removed?Intermediate
Container storage is temporary by default. Use volumes or bind mounts to store data outside the container so it survives restarts and replacement.
How do you make a Docker image smaller and safer?Intermediate
Use a small base image, multi-stage builds so build tools are not shipped, run as a non-root user, copy only what is needed, and scan images for known vulnerabilities.
When would you still choose a VM over a container?Intermediate
When you need strong isolation between tenants, a different operating system or kernel, legacy software, or to run a full system.
How do Linux namespaces and cgroups make containers possible?Advanced
Namespaces give a process its own view of things like processes, network and filesystem. Cgroups limit how much CPU, memory and I/O it can use. A container is essentially a normal process with those two applied.
How do containers talk to each other and to the outside world?Advanced
Docker connects them to virtual networks where they reach one another by name, and publishes chosen ports to the host. In bigger systems an orchestrator such as Kubernetes handles service discovery and routing.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.