Category: java
Docker Networking
Published on 13 Aug 2026
Explanation
Docker networks allow containers to communicate with each other using container names instead of localhost. This is important when running Spring Boot, MySQL, Redis, and other services together.
Code:
docker network create app-network docker run --network app-network student-api
Explanation
Docker volumes provide persistent storage outside the container's writable layer. They are commonly used for databases because database data should survive container restarts or removal.
Code:
docker volume create mysql-data docker run -v mysql-data:/var/lib/mysql mysql:8
Explanation
Docker Compose allows multiple containers to be defined and managed using a single YAML file. It is useful for running a Spring Boot application together with MySQL, Redis, and other dependencies.
Code:
services:
app:
image: student-api:1.0
ports:
- "8080:8080"
mysql:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
Explanation
Docker Compose creates a shared network between services. The Spring Boot application can connect to MySQL using the service name as the database hostname instead of localhost.
Code:
spring.datasource.url=jdbc:mysql://mysql:3306/studentdb spring.datasource.username=root spring.datasource.password=root
Explanation
Multi-stage builds separate the application build environment from the runtime environment. Maven and JDK tools can be used in the first stage, while the final image contains only the required JRE and application JAR, reducing image size.
Code:
FROM maven:3.9-eclipse-temurin-21 AS build WORKDIR /app COPY . . RUN mvn clean package -DskipTests FROM eclipse-temurin:21-jre COPY --from=build /app/target/*.jar app.jar ENTRYPOINT ["java", "-jar", "app.jar"]