Building a Docker image for a Spring Boot application starts with a Dockerfile describing the base image and build steps, and the resulting image is typically stored in a container registry such as Docker Hub or a private cloud registry.
Key Points: • A Dockerfile specifies a base image, copies the application artifact, and defines the startup command. • docker build -t image-name:tag . builds the image from the Dockerfile in the current directory. • docker run -p 8080:8080 image-name runs a container locally from that image. • docker push publishes the image to a registry after tagging it with the registry's address. • Private registries like AWS ECR or Azure Container Registry offer access control that public registries like Docker Hub lack.
Example: After writing a Dockerfile that copies app.jar and runs java -jar app.jar, the team builds the image with docker build -t myapp:1.0 ., tags it for their registry, and pushes it so it can be pulled during deployment.
Code Example:
FROM eclipse-temurin:17-jre-alpine
COPY target/app.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]docker build -t myapp:1.0 .
docker tag myapp:1.0 myregistry.azurecr.io/myapp:1.0
docker push myregistry.azurecr.io/myapp:1.0Interview Tip: A concise interview answer is:
"I write a Dockerfile that defines the base image and startup command, build it with docker build -t, and store the resulting image in a registry, usually a private one like ECR or ACR for internal apps, or Docker Hub for public images. From there the image gets pulled during deployment."