Skip to content

Commit

Permalink
feat: optimizing docker image (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
HwanGonJang authored Jan 18, 2025
1 parent ca50fa5 commit 7a891fe
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added AWS/.DS_Store
Binary file not shown.
Binary file added AWS/image/Pasted image 20231113162741.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AWS/image/Pasted image 20240719110709.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AWS/image/Pasted image 20240719110718.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AWS/image/Pasted image 20240719110721.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AWS/image/Pasted image 20240719110733.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AWS/image/Pasted image 20240719110933.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AWS/image/Pasted image 20240719110958.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AWS/image/Pasted image 20240719142307.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
## ์š”์•ฝ
์˜ค๋Š˜๋‚  ๋งŽ์€ ๊ธฐ์—…์ด ์ œํ’ˆ์„ ๋ฐฐํฌํ•  ๋•Œ ์ปจํ…Œ์ด๋„ˆ ํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋” ๋งŽ์€ ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•จ์— ๋”ฐ๋ผ, ์ด๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ๋” ๋งŽ์€ ์„œ๋ฒ„๊ฐ€ ํ•„์š”ํ•˜๊ฒŒ ๋˜๋ฉฐ, ์ด๋Š” ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์•„ํ‚คํ…์ฒ˜(MSA)๋ฅผ ์ฑ„ํƒํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋™์‹œ์— ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์šฉ์ด ์ฆ๊ฐ€ํ•จ์— ๋”ฐ๋ผ Docker ์ด๋ฏธ์ง€๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ปจํ…Œ์ด๋„ˆ ๊ธฐ๋ฐ˜ ์ธํ”„๋ผ๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•˜๋‹ค. ๋ณธ ๊ธ€์—์„œ๋Š” Docker ์ด๋ฏธ์ง€์˜ ๋ณด์•ˆ์„ฑ๊ณผ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ๋‹ค.

---

### Docker ์ด๋ฏธ์ง€ ์ทจ์•ฝ์  ํ•ด๊ฒฐ
Docker ์ด๋ฏธ์ง€๋Š” ๊ณต์‹์ ์œผ๋กœ ์ธ์ฆ๋œ ์ด๋ฏธ์ง€๋ผ ํ•˜๋”๋ผ๋„ ์ทจ์•ฝ์ ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ทจ์•ฝ์ ์ด ์—†๋Š” ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, ๋ฒ„์ „์„ ์—…๋ฐ์ดํŠธํ•˜๊ฑฐ๋‚˜, ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

```dockerfile
FROM openjdk:17

EXPOSE 8080

COPY /font/NanumGothic.ttf /font/NanumGothic.ttf
COPY /hospital/hospital-application/build/libs/hospital-application-0.0.1-SNAPSHOT.jar /app.jar

ENV JAVA_TOOL_OPTIONS -Djava.security.properties=/java/security/enableLegacyTLS.security -Dfile.encoding=UTF8
ENV MODULE_NAME hospital
ENV TZ Asia/Seoul

ENTRYPOINT ["java", "-Dspring.profiles.active=develop", "-jar", "/app.jar"]
```

์œ„ ์˜ˆ์‹œ๋Š” ์ดˆ๊ธฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ OpenJDK 17 ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€๋ฅผ ๋ณด์—ฌ์ค€๋‹ค. ํ•ด๋‹น ์ด๋ฏธ์ง€๋Š” ์ง€์›์ด ์ข…๋ฃŒ๋˜์—ˆ๊ณ  ์—ฌ๋Ÿฌ ์ทจ์•ฝ์ ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์—ˆ๋‹ค.
<img src="./image/Pasted image 20240719110709.png">
๊ทธ๋ž˜์„œ ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€๋ฅผ `amazoncorretto:17`๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค.
<img src="./image/Pasted image 20240719110721.png">
๊ทธ๋Ÿฌ๋‚˜ `amazoncorretto:17`๋กœ ๋ณ€๊ฒฝํ•œ ํ›„์—๋„ ๋„คํŠธ์›Œํฌ ๋ณด์•ˆ๊ณผ ๊ด€๋ จ๋œ ์ทจ์•ฝ์ ์ด ์—ฌ์ „ํžˆ ๋‚จ์•„ ์žˆ์—ˆ๋‹ค. ์ด์— ๋”ฐ๋ผ `amazonlinux:2023`์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ `amazoncorretto:17.0.10-al2023` ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค([Docker](https://hub.docker.com/layers/library/amazoncorretto/17.0.10-al2023/images/sha256-788e2e45f885e4ddeb881d5616e8b6976898c475ae3311e65263f64ec46f103c?context=explore)).
<img src="./image/Pasted image 20240719110733.png">
๋˜ํ•œ [Ubuntu Packages Search](https://packages.ubuntu.com/) ์‚ฌ์ดํŠธ์—์„œ OS ์ด๋ฏธ์ง€์˜ ์ •๋ณด์™€ ์ทจ์•ฝ์ ์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
<img src="./image/Pasted image 20240719142307.png">

---

### ๊ฒฝ๋Ÿ‰ Docker ์ด๋ฏธ์ง€
<img src="./image/Pasted image 20240719110958.png">
Docker ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์€ Docker ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๋ฐฐํฌํ•˜๊ธฐ ์œ„ํ•ด ์ค‘์š”ํ•˜๋‹ค. Docker ์ด๋ฏธ์ง€ ํฌ๊ธฐ๊ฐ€ ํฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค:
- ์ด๋ฏธ์ง€ ๋นŒ๋“œ ์‹œ๊ฐ„์ด ์ฆ๊ฐ€ํ•œ๋‹ค.
- ์ด๋ฏธ์ง€๋ฅผ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ์— ํ‘ธ์‹œํ•˜๋Š” ์‹œ๊ฐ„์ด ์ฆ๊ฐ€ํ•œ๋‹ค.
- ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ํ’€๋งํ•˜๋Š” ์‹œ๊ฐ„์ด ์ฆ๊ฐ€ํ•œ๋‹ค.

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋Š” CI/CD ํ”„๋กœ์„ธ์Šค์™€ ์Šค์ผ€์ผ ์•„์›ƒ ์‹œ๊ฐ„์„ ์ฆ๊ฐ€์‹œํ‚ค๋ฉฐ, ์„œ๋ฒ„ ์Šคํ† ๋ฆฌ์ง€๋ฅผ ๋‚ญ๋น„ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. Docker ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•œ๋‹ค.

#### Distroless ์‚ฌ์šฉ
์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ Distroless ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Google์ด ๊ฐœ๋ฐœํ•œ Distroless ์ด๋ฏธ์ง€๋Š” ๊ฐ€์žฅ ์ž‘์€ ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€ ์ค‘ ํ•˜๋‚˜๋‹ค. Distroless ์ด๋ฏธ์ง€๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ์‹คํ–‰์— ํ•„์š”ํ•œ ๋Ÿฐํƒ€์ž„ ์ข…์†์„ฑ๋งŒ ํฌํ•จํ•œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฐ€์žฅ ์ž‘์€ Distroless ์ด๋ฏธ์ง€์ธ `gcr.io/distroless/static-debian11`๋Š” ์•ฝ 2 MiB ํฌ๊ธฐ์ด๋ฉฐ, ์ด๋Š” `alpine`(~5 MiB)์˜ ์•ฝ 50%์ด๊ณ , `debian`(124 MiB)์˜ 2% ๋ฏธ๋งŒ์ด๋‹ค. ์•„๋ž˜๋Š” Distroless ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ๋‹ค.

```dockerfile
# ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋นŒ๋“œ
FROM golang:1.18 as build

WORKDIR /go/src/app
COPY . .

RUN go mod download
RUN CGO_ENABLED=0 go build -o /go/bin/app

# ๋นŒ๋“œ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ Distroless ์ด๋ฏธ์ง€์— ๋ณต์‚ฌ
FROM gcr.io/distroless/static-debian11
COPY --from=build /go/bin/app /
CMD ["/app"]
```

Distroless ์ด๋ฏธ์ง€์™€ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ์ด๋ฏธ์ง€๋Š” [GitHub ๋งํฌ](https://github.com/GoogleContainerTools/distroless)์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. Distroless ์ด๋ฏธ์ง€๋ฅผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฏธ์ง€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์„ ํฌ๊ฒŒ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

#### ๋ ˆ์ด์–ด ์ตœ์†Œํ™”
Docker ๋ช…๋ น์–ด(์˜ˆ: `RUN`)๋Š” ์‚ฌ์šฉ ์‹œ๋งˆ๋‹ค ๋ ˆ์ด์–ด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ ๋ช…๋ น์–ด๋ฅผ ํ•œ ๋ฒˆ์— ์‹คํ–‰ํ•˜๋„๋ก `RUN` ๋ช…๋ น์–ด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

```dockerfile
FROM alpine:3.14
LABEL maintainer="FastCampus Park <fastcampus@fastcampus.com>"
LABEL description="Simple utility to send slack message easily."

# ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ์„ค์น˜
RUN \
apk add --no-cache bash curl git && \
git clone https://github.com/course-hero/slacktee /slacktee && \
apk del --no-cache git
RUN chmod 755 /slacktee/slacktee.sh

# ์‹คํ–‰
WORKDIR /slacktee
ENTRYPOINT ["/bin/bash", "-c", "./slacktee.sh"]
```

`&&` ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ช…๋ น์–ด๋ฅผ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ `--no-cache` ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฏธ์ง€ ๋นŒ๋“œ ๊ณผ์ •์—์„œ ๋ถˆํ•„์š”ํ•œ ์บ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.

---

#### Docker ๋ฉ€ํ‹ฐ ์Šคํ…Œ์ด์ง€ ๋นŒ๋“œ
<img src="./image/Pasted image 20240719110933.png">
Docker ๋ฉ€ํ‹ฐ ์Šคํ…Œ์ด์ง€ ๋นŒ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Dockerfile์—์„œ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํ•„์š”ํ•œ ๊ตฌ์„ฑ ์š”์†Œ๋งŒ ํฌํ•จ๋œ ์ตœ์†Œํ•œ์˜ ์ตœ์ข… ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
์•„๋ž˜๋Š” Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์œ„ํ•œ ๋ฉ€ํ‹ฐ ์Šคํ…Œ์ด์ง€ ๋นŒ๋“œ์˜ ์˜ˆ์ œ๋‹ค.

```dockerfile
FROM amazoncorretto:17.0.10-al2023 AS base
WORKDIR /app
COPY /docker/docker-application/build/libs/docker-application-0.0.1-SNAPSHOT.jar /app.jar
RUN java -Djarmode=layertools -jar app.jar extract

FROM amazoncorretto:17.0.10-al2023
WORKDIR /app
EXPOSE 8080
COPY --from=base /app/dependencies/ ./
COPY --from=base /app/spring-boot-loader/ ./
COPY --from=base /app/snapshot-dependencies/ ./
COPY --from=base /app/application/ ./
ENV MODULE_NAME docker
ENV TZ Asia/Seoul

ENTRYPOINT ["java", "-Dspring.profiles.active=develop", "org.springframework.boot.loader.JarLauncher"]
```

์œ„ Dockerfile์€ ๋นŒ๋“œ ๋‹จ๊ณ„์—์„œ JAR ํŒŒ์ผ์„ ์ถ”์ถœํ•˜๊ณ  ์ด๋ฅผ ์—ฌ๋Ÿฌ ๋ ˆ์ด์–ด๋กœ ๋‚˜๋ˆˆ๋‹ค. ์‹คํ–‰ ๋‹จ๊ณ„์—์„œ๋Š” ํ•„์š”ํ•œ ๋ชจ๋“  ์ข…์†์„ฑ์„ ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€์—์„œ ๊ฐ€์ ธ์™€ `JarLauncher`๋ฅผ ์‚ฌ์šฉํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•œ๋‹ค. ์ด ๋ฐฉ์‹์€ JAR ํŒŒ์ผ์˜ ์••์ถ• ํ•ด์ œ ์ž‘์—…์„ ์ƒ๋žตํ•˜์—ฌ ํšจ์œจ์„ฑ์„ ๋†’์ด๋ฉฐ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ ˆ์ด์–ด๋ฅผ ์บ์‹ฑํ•˜์—ฌ ๋นŒ๋“œ ์†๋„๋ฅผ ๊ฐœ์„ ํ•œ๋‹ค.

---

### ๊ฒฐ๋ก 
<img src="./image/Pasted image 20231113162741.png">
๋ณธ ๊ธ€์—์„œ๋Š” Docker ์ด๋ฏธ์ง€๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋…ผ์˜ํ–ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ ๊ธฐ๋ฐ˜ ๋ฐฐํฌ๋Š” ํ˜„๋Œ€ ๋ฐฑ์—”๋“œ ์‹œ์Šคํ…œ์—์„œ ์ง€๋ฐฐ์ ์ธ ์•„ํ‚คํ…์ฒ˜๋‹ค. ๋ณด์•ˆ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  Docker ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์€ ์ธํ”„๋ผ๋ฅผ ์œ ์ง€ํ•˜๊ณ  ์„œ๋น„์Šค๋ฅผ ์šด์˜ํ•˜๋Š” ๋ฐ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค.

0 comments on commit 7a891fe

Please sign in to comment.