본문 바로가기
docker

Gradle, 도커(docker)를 통한 springboot3 with mysql 배포(docker-compose)

by khds 2023. 5. 7.

 

build.gradle 에 도커 관련 추가 부분

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.2.3'
	id 'io.spring.dependency-management' version '1.1.4'
	id 'com.palantir.docker' version '0.35.0'
}

...


docker {
	println(tasks.bootJar.outputs.files)
	name rootProject.name + ":" + version
	dockerfile file('Dockerfile')
	files tasks.bootJar.outputs.files
	buildArgs(['JAR_FILE': tasks.bootJar.outputs.files.singleFile.name])
}

 

위 두부분만 추가해 주면 된다. 

이후 ./gradlew docker를 입력하면 빌드되며 도커 이미지가 생성된다. 

 

아래는 Dockerfile이다. 자바는 17 버전이다. 

FROM openjdk:17
EXPOSE 8080
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

 

아래는 docker-compose.yaml 파일이다.

version: '3'
services:
  mysql:
    image: mysql:8.0.34
    networks:
      - khds_network
    volumes:
      - ./db/conf.d:/etc/mysql/conf.d
      - ./db/data:/var/lib/mysql
      - ./db/initdb.d:/docker-entrypoint-initdb.d
    env_file: .env
    ports:
      - "3306:3306"
  e-commerce:
    image: e-commerce:0.0.1-SNAPSHOT
    networks:
      - khds_network
    ports:
      - "8081:8080"
    depends_on:
      - mysql
    environment:
      - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/khds_pay?useSSL=false&allowPublicKeyRetrieval=true
      - SPRING_DATASOURCE_USERNAME=mysqluser
      - SPRING_DATASOURCE_PASSWORD=mysqlpw
      - SPRING_JPA_PROPERTIES_HIBERNATE_DIALECT=org.hibernate.dialect.MySQL8Dialect
      - SPRING_JPA_HIBERNATE_DDL_AUTO=validate
networks:
  khds_network:
    driver: bridge

 

mysql 이미지는 컨테이너로 실행할 때 환경변수(environment)를 .env파일에 담아서 실행했다.

e-commerce 서비스의 environment 옵션으로 사용해도 상관없다.

아래는 .env 파일이다. 

TZ=Asia/Seoul
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=rootpassword
MYSQL_DATABASE=khds_pay
MYSQL_USER=mysqluser
MYSQL_PASSWORD=mysqlpw

 

 

 

아래의 두 명령어를 입력하면 웹과 DB서버가 실행된다. 

./gradlew docker
docker-compose up

 

mysql 볼륨으로 컨테이너 외부에 저장소를 지정해 주었기 때문에 컨테이너를 종료하고 다시 실행해도 DB에 데이터는 유효하다. 

 

종료할 때는 아래의 명령어를 입력한다.

docker-compose down

 

 

트러블 슈팅 

인텔리제이에서 테스트 시 ./gradlew docker 실행 시 아래와 같이 에러가 발생

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'e-commerce'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve org.springframework.boot:spring-boot-gradle-plugin:3.2.3.
     Required by:
         project : > org.springframework.boot:org.springframework.boot.gradle.plugin:3.2.3
      > No matching variant of org.springframework.boot:spring-boot-gradle-plugin:3.2.3 was found. The consumer was configured to find a library for use during runtime, compatible with Java 11, packaged as a jar, and its depende
ncies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.5' but:
          - Variant 'apiElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.3 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'javadocElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.3 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'mavenOptionalApiElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.2.3 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 17 and the consumer needed a component for use during runtime, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'mavenOptionalRuntimeElements' capability org.springframework.boot:spring-boot-gradle-plugin-maven-optional:3.2.3 declares a library for use during runtime, packaged as a jar, and its dependencies declared ex
ternally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'runtimeElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.3 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 11
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')
          - Variant 'sourcesElements' capability org.springframework.boot:spring-boot-gradle-plugin:3.2.3 declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.5')

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 1s

 

알고 보니 스프링부트에서 gradlew와 java의 버전이 매칭이 안된 문제.

아래와 같이 gradle.properties  파일을 생성하여 해결

 

 

org.gradle.java.home=C:\\Program Files\\Java\\jdk-17