Giriş
SpringBoot uygulamasını Docker image haline dönüştürmek için yöntemler şöyle
1. spring-boot-maven-plugin kullanmak. Bu plugin ile Dockerfile yazmaya gerek yok
2. Jib kullanmak
3. Dockerfile yazmak
1. Jib Plugin kullanmak
Maven için jib plugin yazısına taşıdım
Gradle için jib plugin yazısına taşıdım
2. Dockerfile yazmak
Açıklaması şöyle
This is the traditional approach where a fat uber jar containing the java artifacts (spring boot libraries, dependency libraries, class files) are packaged into a single jar file using the mvn package command. The developer needs to create the Dockerfile.
Örnek - JDK
Şöyle yaparız
FROM adoptopenjdk/openjdk11 EXPOSE 8080 ARG JAR_FILE=target/rest-service-0.0.1-SNAPSHOT.jar ADD ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"]
Açıklaması şöyle. Üretilen Docker image biraz büyük oluyor
The developer must first run the mvn package command to create the reference jar file target/rest-service-0.0.1-SNAPSHOT.jar that is the uber jar fille containing everything to run the microservice.
To build the Docker image run the command:docker build -t rest-server-dockerfile:0.0.1 .
Command to run the image:docker run -it -p8080:8080 rest-server-dockerfile:0.0.1
This approach is not the most efficient. The image size is relatively big at 455 MB. The reason why it is big is because it uses the adoptopenjdk/openjdk11 base image. A jre image as opposed to a jdk image will result in a smaller container image size. Another reason why this approach is not efficient is because the resulting Docker image has only 4 layers, where one of the layers consists of the uber jar. The line in the Dockerfile that adds the uber jar file layer to the Docker image is ADD ${JAR_FILE} app.jar. Therefore, when a single java file changes, the java application needs to be rebuilt (all spring boot libraries, dependencies, app source files), a new fat jar needs to be created and then a new image needs to be created with the new fat jar layer.
Eğer JDK yerine JRE kullanmak istesek şöyle yaparız
FROM adoptopenjdk:11-jre-hotspot EXPOSE 8080 ARG JAR_FILE=target/rest-service-0.0.1-SNAPSHOT.jar ADD ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"]
Örnek - JDK Yöntemi
Şöyle yaparız. Burada bir fat jar var ve sunucumuzun application.properties dosyasında 8443 numaralı portu kullanacağı yazıyor. Böylece farklı container'lar bize bu porttan ulaşabilir.
From openjdk:12-jdk-alpineCOPY build/libs/my-spring-boot-app-0.1.0.jar /usr/appWORKDIR /usr/appRUN sh -c 'touch my-spring-boot-app-0.1.0.jar'EXPOSE 8443ENTRYPOINT ["java","-jar","my-spring-boot-app-0.1.0.jar"]
Docker imajı yaratmak ve çalıştırmak için şöyle yaparız
Uygulamamızın loglarını görmek için şöyle yaparızdocker build . -t mysecurespringbootappdocker run -d --name secureapp -p 8443:8443 mysecurespringbootapp:latest
docker logs -f secureapp
3. Multi-staged Docker
Multi-staged Docker + layertools ve SpringBoot yazısına taşıdım
Hiç yorum yok:
Yorum Gönder