Video in progress

What do Docker containers give you for microservices?

A container wraps a service together with its runtime and dependencies into one image. As a result it runs the same in any environment, starts quickly, and orchestrators (Kubernetes) can easily scale or replace it.

📖 Answer

With dozens of services, the "it works on my machine" problem gets very painful: one runs Java 17, one runs Java 21, one needs a special native library. A container seals all of that inside the image.

FROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/order-service.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

In Spring Boot you can also build an image without a Dockerfile: ./mvnw spring-boot:build-image (Cloud Native Buildpacks).

What it gives you for microservices:

  • the same image runs in dev, test and prod, and only the configuration differs;
  • a new instance comes up in seconds, which you need for autoscaling and rolling deploys;
  • you can run several services with limited resources side by side on one server.

A note for Java: if you set a memory limit on the container, the JVM takes it into account (the default in modern versions). Setting the heap with -XX:MaxRAMPercentage is more convenient than hardcoding -Xmx.