Docker + Spring Boot Interview Guide
1. Docker Basics
Docker is a platform to package, distribute, and run applications in lightweight containers.
- Image: Blueprint of the container.
- Container: Instance of an image.
- Dockerfile: Script that defines how to build the image.
- Docker Hub: Public registry of Docker images.
2. Common Docker Commands
- docker --version : Check Docker version
- docker ps : List running containers
- docker ps -a : List all containers
- docker start/stop/restart ID : Manage container lifecycle
- docker run -d -p 8080:80 --name mycontainer nginx : Run container in background
- docker rm / rmi ID : Remove container/image
- docker build -t name:tag . : Build image from Dockerfile
- docker exec -it ID /bin/bash : Access running container shell
3. Dockerfile Example
FROM ubuntu
WORKDIR /app
COPY . .
RUN apt-get update
CMD ["node", "app.js"]
EXPOSE 3000
Docker + Spring Boot Interview Guide
4. Docker Compose
A tool for defining and running multi-container Docker apps.
docker-compose.yml example:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
Commands:
- docker-compose up
- docker-compose down
5. Docker Volumes and Networks
- docker volume create myvol
- docker run -v myvol:/data image
- docker network create mynet
- docker network ls
Docker + Spring Boot Interview Guide
6. Docker Best Practices
- Use small base images (e.g., Alpine)
- One process per container
- Use .dockerignore
- Tag images (e.g., myapp:1.0.1)
7. Dockerizing a Spring Boot Project
Steps:
1. Build your project:
mvn clean install
2. Create Dockerfile:
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY target/myapp.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
3. Build Docker Image:
docker build -t my-springboot-app .
4. Run Docker Container:
docker run -d -p 8080:8080 --name springapp my-springboot-app
5. Test: http://localhost:8080
Docker + Spring Boot Interview Guide
8. Spring Boot + MySQL using Docker Compose
docker-compose.yml:
version: '3'
services:
app:
build: .
ports:
- "8080:8080"
depends_on:
- db
db:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mydb
Command:
docker-compose up --build