Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Architecture and operations › Deployment and operations

Containers and virtual machines

Why are containers lightweight and quick to start?

Why it exists — If Java and libraries are installed directly on a server and run, versions and settings differ slightly from server to server, and dividing servers with virtual machines gives isolation but is heavy. A container makes Java and the libraries into one image so that it starts in the same environment on any server, and it separates processes with the Linux kernel's namespaces and cgroups and limits the resources they can use, creating an isolated execution environment without running a separate operating system.

Where it's used today — A Spring Boot application is made into a Docker image and run the same way on a development PC, a test server, and a production server, and Kubernetes starts these containers on several servers and starts them again when they die. Java uses memory besides the heap, so the heap is set comfortably smaller than the container's memory limit. If you need to run a different operating system, use a virtual machine.

When you start several copies of the same application, a virtual machine loads a whole operating system for every instance, while containers share the kernel of the host. Change the type and the number, and compare what gets loaded and how much memory is used. Also see what happens when you put a memory limit on a container.

Checked against documentation and standards · The Docker and Linux kernel documentation

Concepts in this lab

Containers and images
A container is a technology that packages an application with the things it needs to run and runs it in an environment isolated from other processes, while sharing the kernel of the host operating system. An image is a read-only template used to create a container, and it holds the application, libraries, and configuration. Running the same image greatly reduces problems caused by differences between environments.
Containers versus virtual machines
Where it's used today — To run several applications lightly on the same Linux, use containers. To run a different operating system or to isolate untrusted code strongly, use virtual machines. In the cloud, running containers on top of virtual machines is a common setup.
A virtual machine virtualizes hardware and runs a whole separate operating system installed on top of it. A container shares the operating system kernel of the host and isolates only the processes. So a container is lighter and starts faster than a virtual machine, but a virtual machine has a higher level of isolation.
Namespaces and cgroups
Why it exists — Processes on the same server can see each other's process lists and the ports in use, and when one uses up all the memory, other processes can slow down or be killed. Namespaces and cgroups let the kernel set separately, for each group of processes, what is visible and how much can be used, which reduces this interference.
Where it's used today — Docker and Kubernetes use them when they start containers. The --memory and --cpus options of docker run and Kubernetes' resources.limits become cgroups settings, and the JVM reads this memory limit to set the default heap size.
Linux kernel features that are used to implement containers. Namespaces separate what a process can see (the process list, the network, the file system, and so on) so that each one looks like an independent environment. cgroups limit the amount of resources, such as CPU and memory, that a group of processes can use.

Try these

Running with the default settings

  1. The host operating system is running on one physical server. You want to start 3 application instances on it, isolated from each other.
  2. The container runtime does not create virtual hardware. It only runs processes in isolation, using features of the host kernel.
  3. Container 1: The image (a bundle of the application and the files it needs to run) is set up read-only, and a process is started on top of it. No operating system is booted, so it takes only as long as starting one process. Containers share the same image.
  4. Container 1: Running. Thanks to namespaces, this process appears to have its own file system, network, and process list, but in reality it is an ordinary process on the host kernel.
  5. Container 2: The image (a bundle of the application and the files it needs to run) is set up read-only, and a process is started on top of it. No operating system is booted, so it takes only as long as starting one process. Containers share the same image.
  6. Container 2: Running. Thanks to namespaces, this process appears to have its own file system, network, and process list, but in reality it is an ordinary process on the host kernel.
  7. Container 3: The image (a bundle of the application and the files it needs to run) is set up read-only, and a process is started on top of it. No operating system is booted, so it takes only as long as starting one process. Containers share the same image.
  8. Container 3: Running. Thanks to namespaces, this process appears to have its own file system, network, and process list, but in reality it is an ordinary process on the host kernel.

Result of this run

Code

# Dockerfile
FROM eclipse-temurin:21-jre           # a base image that includes a JRE
COPY build/libs/app.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]

# Run (containers: 3)
$ docker run -d --memory=1024m my-app:1.0

Assumptions of this model

The memory figures (about 1GB for a guest operating system, about 400MB for the application) and the boot times are rough examples to show the direction of the difference, and the real values vary a great deal depending on the setup. That containers share the host kernel and are isolated with namespaces and cgroups, and that the kernel kills a process when it goes over its limit, follow the explanations in the Docker and Linux kernel documentation. Docker Desktop on Windows or Mac keeps one Linux virtual machine inside and runs the containers in it.

Previous lab: Deployment strategies · Next lab: Kubernetes self-healing

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these