본문 바로가기

리눅스

[Spring Boot] 도커 빌드 및 도커 컨테이너 실행

728x90

Spring Boot 프로젝트 도커 빌드 및 도커 컨테이너 실행

Dockerfile 파일 생성

#프로젝트 폴더
$ cd demo

$ vim Dockerfile
FROM openjdk:18-jdk-alpine AS builder
COPY gradlew .
COPY gradle gradle
COPY build.gradle .
COPY settings.gradle .
COPY src src
RUN chmod +x ./gradlew
RUN ./gradlew bootJAR

FROM openjdk:18-jdk-alpine
COPY --from=builder build/libs/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app.jar"]

**로컬 PC에 docker 엔진이 구성한 상태에서 docker build를 실행합니다.

도커 이미지 빌드

docker build -t deploy-demo:0.0.1 .

$ docker build -t deploy-demo:0.0.1 .
[+] Building 105.9s (15/15) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                   0.0s
 => => transferring dockerfile: 342B                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                      0.0s
 => => transferring context: 2B                                                                                                                        0.0s
 => [internal] load metadata for docker.io/library/openjdk:18-jdk-alpine                                                                               2.4s
 => [auth] library/openjdk:pull token for registry-1.docker.io                                                                                         0.0s
 => CACHED [builder 1/8] FROM docker.io/library/openjdk:18-jdk-alpine@sha256:e5c5b35b831a4f655074a25604130ce53e33567b82c8a7204f0e5641b66d477e          0.0s
 => [internal] load build context                                                                                                                      0.0s
 => => transferring context: 72.09kB                                                                                                                   0.0s
 => [builder 2/8] COPY gradlew .                                                                                                                       0.0s
 => [builder 3/8] COPY gradle gradle                                                                                                                   0.0s
 => [builder 4/8] COPY build.gradle .                                                                                                                  0.0s
 => [builder 5/8] COPY settings.gradle .                                                                                                               0.0s
 => [builder 6/8] COPY src src                                                                                                                         0.0s
 => [builder 7/8] RUN chmod +x ./gradlew                                                                                                               0.4s
 => [builder 8/8] RUN ./gradlew bootJAR                                                                                                              102.2s
 => [stage-1 2/2] COPY --from=builder build/libs/*.jar app.jar                                                                                         0.1s
 => exporting to image                                                                                                                                 0.1s
 => => exporting layers                                                                                                                                0.1s
 => => writing image sha256:992f1de38c475e47ad76598ef86bedda8e29ad9b7b76853f6daecaa2a8c1d5dd                                                           0.0s
 => => naming to docker.io/library/deploy-demo:0.0.1                                                                                                   0.0s

도커 이미지 확인(deploy-test:0.0.1)

$ docker images --filter=reference='deploy-test:0.0.1'
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
deploy-test   0.0.1     5e5ebd4fe080   20 hours ago   346MB

도커 컨테이너 실행

docker run -d -p 8080:8080 --name deploy-demo deploy-demo:0.0.1

$ docker run -d -p 8080:8080 --name deploy-demo deploy-demo:0.0.1
6aa584edba49a0283540cf403b48c41c036e3ece3306207d98483559c69a77c9

도커 컨테이너 로그

docker logs -f deploy-demo

$ docker logs -f deploy-demo

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.2)

2021-12-27 09:34:45.125  INFO 1 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 18-ea on 6aa584edba49 with PID 1 (/app.jar started by root in /)
2021-12-27 09:34:45.164  INFO 1 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2021-12-27 09:34:49.237  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-12-27 09:34:49.282  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-12-27 09:34:49.284  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2021-12-27 09:34:49.427  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-12-27 09:34:49.427  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3932 ms
2021-12-27 09:34:50.447  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-12-27 09:34:50.476  INFO 1 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 6.633 seconds (JVM running for 7.509)

CLI로 웹 페이지 테스트

curl -s localhost:8080
Hello, World!

 

728x90