EasyConcept

What is the difference between a Docker container and a virtual machine?

Technology:
Docker
Experience:
Intern,
Junior
Topics:
DevOps,
Cloud

Quick answer

A virtual machine runs a full guest operating system on virtualised hardware, while a container shares the host鈥檚 kernel and isolates only the process, its filesystem and resources, which makes containers much lighter and faster to start.

Detailed explanation

A hypervisor gives each virtual machine its own virtual CPU, memory and disk, and each VM boots a complete operating system. That gives strong isolation and lets you run different operating systems side by side, but each VM costs gigabytes and takes time to boot.

A container is an ordinary process on the host that the Linux kernel isolates with namespaces (its own view of processes, network and filesystem) and limits with cgroups (CPU and memory). Because there is no guest kernel, containers start in milliseconds to seconds and image sizes are often tens of megabytes.

The trade-off is isolation: containers share the kernel, so a kernel vulnerability can affect every container on the host. In practice the two are combined: cloud providers run containers inside VMs.

How to explain it in an interview

A VM virtualises hardware and runs its own operating system, so it is heavy but strongly isolated. A container is just an isolated process that shares the host kernel, using namespaces and cgroups, so it starts quickly and uses far fewer resources. I use containers to package applications consistently, and they usually run inside VMs in the cloud anyway.

Key points

  • VM: full guest OS on a hypervisor
  • Container: isolated process sharing the host kernel
  • Namespaces isolate, cgroups limit resources
  • Containers are lighter; VMs isolate more strongly

Common mistakes

  • Saying a container contains its own operating system kernel.
  • Claiming containers are as isolated as VMs.

Follow-up questions

  • DockerEasy

    What is the difference between a Docker image and a container?

    An image is a read-only template made of layers that contains the application and its dependencies; a container is a running (or stopped) instance of an image with its own writable layer on top.

    Intern 路 Junior 路 DevOps
  • DockerEasy

    What are Docker volumes and why do you need them?

    Volumes are storage managed by Docker that lives outside a container鈥檚 writable layer, so data such as database files survives when the container is removed or replaced.

    Junior 路 Mid-level 路 DevOps
  • DockerMedium

    How do you write a good Dockerfile? What are multi-stage builds?

    Use a small, pinned base image, order instructions so dependencies are cached before source code, use multi-stage builds to leave build tools out of the final image, run as a non-root user and keep secrets out of the image.

    Junior 路 Mid-level 路 DevOps
  • DockerMedium

    What is Docker Compose used for?

    Docker Compose defines a multi-container application (services, networks and volumes) in one YAML file so you can start, stop and rebuild the whole stack with a single command, typically for local development and testing.

    Mid-level 路 Senior 路 DevOps
  • Node.jsEasy

    What is the difference between package.json and package-lock.json?

    package.json lists your project's dependencies with allowed version ranges, while package-lock.json records the exact versions that were installed so every machine gets an identical dependency tree.

    Intern 路 Junior 路 Programming Fundamentals
  • GitEasy

    What is Git and how is it different from GitHub?

    Git is a distributed version control system that tracks changes to files on your machine; GitHub is a hosting service for Git repositories that adds collaboration features such as pull requests, code review and CI.

    Intern 路 Junior 路 Git