Skip to content

Commit d0a87ec

Browse files
committed
Java 애플리케이션 Dockerize 샘플
1 parent 49f8e81 commit d0a87ec

18 files changed

Lines changed: 616 additions & 0 deletions

File tree

20240504/java/.dockerignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Docker
2+
Dockerfile
3+
.dockerignore
4+
5+
# Git
6+
.git
7+
.gitignore
8+
.gitattributes
9+
10+
# Gradle
11+
.gradle
12+
gradle.properties
13+
gradlew.bat
14+
15+
# IntelliJ IDEA
16+
.idea
17+
18+
# Ignore temporary files
19+
/tmp/

20240504/java/.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+

20240504/java/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
6+
7+
# Ignore temporary files
8+
/tmp/

20240504/java/.sdkmanrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Enable auto-env through the sdkman_auto_env config
2+
# Add key=value pairs of SDKs to use below
3+
java=21.0.3-tem
4+
gradle=8.7

20240504/java/Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM gradle:8.7-jdk21-alpine AS builder
2+
3+
WORKDIR /u/app
4+
5+
COPY gradle ./gradle
6+
COPY gradlew .
7+
COPY settings.gradle.kts .
8+
COPY build.gradle.kts .
9+
10+
RUN ./gradlew getDeps --no-daemon --info
11+
12+
COPY . .
13+
14+
RUN ./gradlew shadowJar --no-daemon --info
15+
16+
###############################################################################
17+
18+
FROM eclipse-temurin:21-jdk-alpine
19+
20+
WORKDIR /u/app
21+
22+
COPY --from=builder /u/app/build/libs/app-all.jar app.jar
23+
24+
CMD java -jar app.jar

20240504/java/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Java Sample
2+
3+
## SDKMAN! 세팅
4+
5+
- <https://sdkman.io/usage>
6+
- <https://github.com/ahastudio/til/blob/main/java/sdkman.md>
7+
8+
```bash
9+
sdk selfupdate
10+
11+
sdk env install
12+
13+
sdk current
14+
15+
sdk env
16+
```
17+
18+
## 프로젝트 생성
19+
20+
```bash
21+
gradle init
22+
```
23+
24+
여기선 `app` 디렉터리를 사용하지 않도록 구조 조정함.
25+
26+
## 의존성 확인
27+
28+
```bash
29+
./gradlew dependencies
30+
```
31+
32+
## 의존성 다운로드 테스트
33+
34+
- <https://docs.gradle.org/current/userguide/more_about_tasks.html>
35+
- <https://docs.gradle.org/current/userguide/working_with_files.html>
36+
- <https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Copy.html>
37+
38+
`build.gradle.kts` 파일에 `getDeps` 태스크 추가.
39+
40+
```kt
41+
tasks.register<Copy>("getDeps") {
42+
from(configurations.runtimeClasspath)
43+
from(configurations.compileClasspath)
44+
into("tmp/libs/")
45+
}
46+
```
47+
48+
```bash
49+
./gradlew getDeps
50+
```
51+
52+
## 빌드
53+
54+
```bash
55+
./gradlew assemble
56+
```
57+
58+
## Fat Jar 빌드
59+
60+
- <https://plugins.gradle.org/plugin/io.github.goooler.shadow>
61+
62+
`build.gradle.kts` 파일에 플러그인 추가.
63+
64+
```kt
65+
plugins {
66+
// ...(중략)...
67+
68+
id("com.github.johnrengelman.plugin-shadow") version "2.0.3"
69+
}
70+
```
71+
72+
```bash
73+
./gradlew shadowJar
74+
```
75+
76+
## Gradle Docker 이미지 확인
77+
78+
<https://hub.docker.com/_/gradle/>
79+
80+
```bash
81+
docker run -it --rm gradle:8.7-jdk21-alpine sh
82+
```
83+
84+
## Docker 이미지 빌드
85+
86+
```bash
87+
docker build --progress=plain --platform linux/amd64 -t java-sample .
88+
```
89+
90+
## Docker 컨테이너 실행
91+
92+
```bash
93+
docker run -it --rm --platform linux/amd64 java-sample
94+
```

20240504/java/build.gradle.kts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.7/userguide/building_java_projects.html in the Gradle documentation.
6+
* This project uses @Incubating APIs which are subject to change.
7+
*/
8+
9+
plugins {
10+
id("io.github.goooler.shadow") version "8.1.7"
11+
12+
// Apply the application plugin to add support for building a CLI application in Java.
13+
application
14+
}
15+
16+
repositories {
17+
// Use Maven Central for resolving dependencies.
18+
mavenCentral()
19+
}
20+
21+
dependencies {
22+
// This dependency is used by the application.
23+
implementation(libs.guava)
24+
25+
implementation("org.apache.hadoop:hadoop-common:3.3.0")
26+
}
27+
28+
testing {
29+
suites {
30+
// Configure the built-in test suite
31+
val test by getting(JvmTestSuite::class) {
32+
// Use JUnit Jupiter test framework
33+
useJUnitJupiter("5.10.1")
34+
}
35+
}
36+
}
37+
38+
// Apply a specific Java toolchain to ease working on different environments.
39+
java {
40+
toolchain {
41+
languageVersion = JavaLanguageVersion.of(21)
42+
}
43+
}
44+
45+
application {
46+
// Define the main class for the application.
47+
mainClass = "org.example.App"
48+
}
49+
50+
tasks.register<Copy>("getDeps") {
51+
from(configurations.runtimeClasspath)
52+
from(configurations.compileClasspath)
53+
into("tmp/libs/")
54+
}

20240504/java/gradle.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file was generated by the Gradle 'init' task.
2+
# https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties
3+
4+
org.gradle.parallel=true
5+
org.gradle.caching=true
6+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file was generated by the Gradle 'init' task.
2+
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
3+
4+
[versions]
5+
guava = "32.1.3-jre"
6+
7+
[libraries]
8+
guava = { module = "com.google.guava:guava", version.ref = "guava" }
42.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)