What is a container and how is it different from a virtual machine?

Containers versus virtual machines: how each is isolated, size, start time, security, a Dockerfile example, and when to use which.

0 0
Read Time:1 Minute, 28 Second

Both let you run software in isolation on shared hardware. The difference is what they virtualise: a VM virtualises the hardware, a container virtualises the operating system.

Virtual machines                    Containers
App A | App B                       App A | App B
Libs  | Libs                        Libs  | Libs
Guest OS | Guest OS                 Container runtime (Docker, containerd)
Hypervisor                          Host operating system (shared kernel)
Hardware                            Hardware

Compared

Virtual machine                 Container
Isolation        Own full OS and kernel          Shares the host kernel; isolated with
                                                 namespaces and cgroups
Size             Gigabytes                       Megabytes
Start time       Tens of seconds to minutes      Seconds or less
Density          Few per host                    Many per host
Security         Stronger boundary               Weaker by default; needs hardening
OS choice        Any (Windows on Linux host OK)  Same kernel family as the host

A container in practice

FROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/orders.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
docker build -t orders:1.0 .
docker run -p 8080:8080 orders:1.0

The image bundles the app with exactly the runtime it needs, so it runs the same on a laptop, in CI and in production. That solves “it works on my machine”.

When to use which

  • Containers: microservices, CI/CD, scaling many copies quickly, usually orchestrated with Kubernetes (for example Red Hat OpenShift, EKS, AKS or GKE).
  • VMs: different operating systems, legacy applications, or a stronger isolation boundary between tenants.
  • In the cloud you usually run containers on VMs: the two work together.

Follow-ups

  • Image versus container.
  • What does Kubernetes add on top of Docker?
  • How do you keep data when a container restarts (volumes)?
  • How do you make a container image smaller and safer?

This answer was written by Debugger’s Space as practice material. See how IBM hires, step by step, and more IBM practice questions.

Previous post What does this recursive function return for n = 5?
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Close
0
Would love your thoughts, please comment.x
()
x