Docker, Maven & Spring Boot Interview Questions and Answers
1. What is Maven? Maven is a build automation and dependency management tool for Java projects. It helps developers: Download project dependencies automatically. Compile Java source code. Runβ¦
1. What is Maven?
Maven is a build automation and dependency management tool for Java projects.
It helps developers:
- Download project dependencies automatically.
- Compile Java source code.
- Run unit tests.
- Package the application into a JAR or WAR.
- Install artifacts into the local Maven repository.
- Support automated builds in CI/CD pipelines.
It uses a configuration file called pom.xml.
2. What is the purpose of pom.xml?
pom.xml (Project Object Model) contains all project information.
It defines:
- Project name
- Version
- Dependencies
- Plugins
- Java version
- Build configuration
Example:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
When Maven runs, it downloads this dependency automatically.
3. What does mvn clean do?
mvn clean deletes the previously generated build artifacts by removing the target/ directory.
This ensures that the next build starts from a clean state and avoids issues caused by stale compiled classes.
4. What does mvn compile do?
It compiles Java source files into .class files.
Example:
OrderService.java
β
OrderService.class
The compiled files are stored in:
target/classes
No JAR is created during this phase.
5. What does mvn test do?
It executes all unit tests present under:
src/test/java
If any test fails, Maven reports a build failure and prevents later lifecycle phases from succeeding.
6. What does mvn package do?
mvn package performs:
Compile
β
Run Tests
β
Create Executable JAR/WAR
The generated JAR is placed inside the target/ directory.
7. What does mvn install do?
mvn install performs everything up to the package phase and then copies the generated artifact into the local Maven repository.
Example:
~/.m2/repository
This allows other local Maven projects to use the artifact as a dependency.
8. What is the difference between mvn package and mvn install?
| mvn package | mvn install |
|---|---|
| Creates the JAR/WAR | Creates the JAR/WAR |
Stores it in target/ | Stores it in target/ and .m2/repository |
| Doesn’t install locally | Installs locally |
9. Why do developers usually run mvn clean install?
Because it performs a complete fresh build.
Flow:
Delete target
β
Compile
β
Test
β
Package
β
Install
It ensures no old build artifacts interfere with the current build.
10. Where does Maven store downloaded dependencies?
Inside
~/.m2/repository
This is called the local Maven repository.
Downloaded dependencies are reused in future builds.
11. What is the target folder?
The target folder contains all generated build artifacts.
Examples:
target/
classes/
test-classes/
application.jar
reports
It is automatically created during the build.
12. What is an executable Spring Boot JAR?
An executable Spring Boot JAR contains:
- Compiled application code
- Spring Boot dependencies
- Embedded Tomcat (or Jetty/Undertow)
- Configuration files
- Resources
It can run independently using:
java -jar application.jar
13. What does mvn spring-boot:run do?
It starts the Spring Boot application directly from the source code.
It compiles the project if necessary but does not package it into a JAR.
This command is mainly used during development.
14. What is the difference between mvn spring-boot:run and java -jar?
| spring-boot:run | java -jar |
|---|---|
| Runs from source code | Runs an already-built JAR |
| No JAR required | JAR is mandatory |
| Mostly used during development | Mostly used after packaging or deployment |
15. What is Docker?
Docker is a containerization platform.
It packages an application together with:
- Operating system libraries
- Runtime
- Dependencies
- Configuration
This ensures the application behaves the same in every environment.
16. Why do we need Docker?
Without Docker:
Developer Laptop
Java 21
Ubuntu
Works
Production
Java 17
Different Linux
Fails
Docker packages everything together so that the application runs consistently everywhere.
17. What is a Dockerfile?
A Dockerfile is a text file containing instructions to build a Docker image.
Example:
FROM eclipse-temurin:21-jdk
COPY app.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
Docker executes these instructions while building the image.
18. What does docker build do?
docker build creates a Docker image.
It:
- Reads the Dockerfile
- Downloads the base image if necessary
- Copies the application files
- Creates image layers
- Stores startup instructions
- Produces a Docker image
Example:
docker build -t order-service .
19. Does docker build start the application?
No.
docker build only creates a Docker image.
The application starts only when a container is created using:
docker run
20. What is a Docker Image?
A Docker image is an immutable package containing everything required to run an application.
It includes:
- Linux filesystem
- Java runtime
- Application JAR
- Libraries
- Configuration
- Startup command
Think of it as a blueprint.
21. What is a Docker Container?
A container is a running instance of a Docker image.
The image is static.
The container is dynamic.
Example:
Image
β
Container
β
Running Application
22. Can multiple containers be created from one image?
Yes.
Example:
order-service Image
β
Container 1
β
Container 2
β
Container 3
Each container runs independently.
23. What does docker run do?
docker run:
- Creates a container from an image.
- Executes the
ENTRYPOINTcommand. - Starts the application.
Example:
docker run order-service
24. What happens internally when docker run is executed?
Flow:
Find Image
β
Create Container
β
Execute ENTRYPOINT
β
java -jar app.jar
β
Spring Boot Starts
β
Application Running
25. What is the purpose of ENTRYPOINT?
ENTRYPOINT defines the default command that runs when the container starts.
Example:
ENTRYPOINT ["java","-jar","app.jar"]
Whenever the container starts, Docker automatically executes this command.
26. What is the purpose of COPY in a Dockerfile?
COPY copies files from the host machine into the Docker image.
Example:
COPY target/app.jar app.jar
This copies the generated Spring Boot JAR into the image.
27. What is the purpose of FROM?
FROM specifies the base image.
Example:
FROM eclipse-temurin:21-jdk
Docker builds the new image on top of this base image.
28. What are Docker layers?
Each instruction in a Dockerfile creates a new layer.
Example:
FROM
β
Layer 1
COPY
β
Layer 2
ENTRYPOINT
β
Layer 3
Layers are cached and reused, making future builds much faster.
29. Why is Docker build faster the second time?
Because Docker caches unchanged layers.
If only the application JAR changes, Docker reuses the base image and other unchanged layers instead of rebuilding everything.
30. Explain the complete flow from code to running application.
Write Java Code
β
mvn clean install
β
Creates JAR
β
docker build
β
Creates Docker Image
β
docker run
β
Creates Container
β
Runs java -jar app.jar
β
Spring Boot Starts
β
Application Available
31. What is the difference between a JAR and a Docker Image?
| JAR | Docker Image |
|---|---|
| Contains the application | Contains the application plus runtime and environment |
| Requires Java to be installed | Includes the required runtime/environment |
Runs with java -jar | Runs with docker run |
32. Why is a Docker Image immutable?
A Docker image cannot be modified after it is built.
If changes are required, a new image is created.
This ensures consistency across environments.
33. Explain Docker Image vs Container using Java.
Java:
class Car {}
Car c = new Car();
Docker equivalent:
Car class
β
Docker Image
Car Object
β
Docker Container
The image is the blueprint, and the container is the running instance.
34. In a production environment, do we run mvn clean install on the server?
Typically, no.
A CI/CD pipeline builds the application and creates a Docker image.
The image is pushed to a container registry (such as Docker Hub or a private registry).
Production servers pull the image and run it.
Running Maven builds directly on production servers is generally avoided.
35. What is the role of Docker in CI/CD?
Typical CI/CD pipeline:
Developer Pushes Code
β
CI Server
β
mvn clean install
β
docker build
β
Push Docker Image
β
Production Pulls Image
β
docker run
β
Application Running
This approach ensures that the exact same image tested in CI is deployed to production.
36. Interview Scenario: “You changed only one Java class. Will Docker rebuild everything?”
No.
Docker uses layer caching.
If the base image and Dockerfile instructions haven’t changed, Docker rebuilds only the layer affected by the updated application JAR, while reusing the cached layers for the operating system, Java runtime, and other unchanged instructions.
β Frequently Asked Follow-up Questions
Q. Why do we need both Maven and Docker?
Answer:
Because they solve different problems.
- Maven builds the application (source code β executable JAR).
- Docker packages that application together with its runtime and environment (JAR β Docker image β container).
Q. Can Docker build a JAR?
Answer:
Generally, no. Docker doesn’t compile Java source code by itself. In the common workflow, Maven first creates the JAR, and Docker packages it.
Note: With a multi-stage Docker build, Maven can run inside a Docker build container to compile the application, but it’s still Maven doing the compilationβnot Docker itself.
Q. Can we run a JAR without Docker?
Answer:
Yes.
java -jar app.jar
Docker is optional for running a Spring Boot application. It mainly provides portability, isolation, and consistency across environments.
Q. Can we run Docker without a JAR?
Answer:
For a typical Spring Boot deployment, no. The Docker image needs the application artifact (JAR/WAR) or another build process (such as a multi-stage build or buildpacks) that produces it before the application can run.
β One-Liner Revision (Very Useful Before Interviews)
| Question | One-Line Answer |
|---|---|
| What is Maven? | A build and dependency management tool for Java. |
What does mvn clean do? | Deletes the target/ directory. |
What does mvn compile do? | Compiles .java files into .class files. |
What does mvn package do? | Creates the executable JAR/WAR. |
What does mvn install do? | Installs the packaged artifact into the local Maven repository. |
What does mvn spring-boot:run do? | Runs the application directly from source code. |
| What is Docker? | A containerization platform for packaging and running applications consistently. |
What does docker build do? | Builds a Docker image from a Dockerfile. |
| What is a Docker image? | An immutable package containing the application and everything needed to run it. |
What does docker run do? | Creates a container from an image and starts the application. |
| Image vs Container | Image is the blueprint; container is the running instance. |
| Why Docker? | To ensure the application behaves consistently across development, testing, and production environments. |