본문 바로가기

리눅스

[Spring Boot] 도커 이미지 빌드 및 도커 이미지 docker hub로 push

728x90

Spring Boot 프로젝트 도커 이미지 빌드 및 도커 이미지 docker hub로 push

jib 설정

 settings.gradle 파일 편집

id 'com.google.cloud.tools.jib' version '3.1.2'

bootJar {
    baseName = 'deploy-demo'
    version = '0.1.1'
}

jib {
    to {
        image = "sangchul/${bootJar.baseName}:${bootJar.version}"
        tags = ['latest']
    }
}
$ vim build.gradle
plugins {
	id 'org.springframework.boot' version '2.6.2'
	id 'io.spring.dependency-management' version '1.0.11.RELEASE'
	id 'java'
	id 'com.google.cloud.tools.jib' version '3.1.2'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

bootJar {
	baseName = 'deploy-demo'
    version = '0.1.1'
}

jib {
    to {
		image = "sangchul/${bootJar.baseName}:${bootJar.version}"
		tags = ['latest']
    }
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
	useJUnitPlatform()
}

gradle plugin : https://plugins.gradle.org/plugin/com.google.cloud.tools.jib

docker hub 로그인

$ docker login
Authenticating with existing credentials...
Login Succeeded

Logging in with your password grants your terminal complete access to your account.
For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/
728x90

 

도커 이미지 빌드 및 docker hub에 도커 이미지 PUSH

./gradlew jib 명령으로 도커 이미지 빌드 및 dockerhub에 도커 이미지 PUSH를 합니다.

$ ./gradlew clean jib

> Task :jib

Containerizing application to sangchul/deploy-demo:0.1.1, sangchul/deploy-demo...
Base image 'adoptopenjdk:11-jre' does not use a specific image digest - build may not be reproducible
The credential helper (docker-credential-osxkeychain) has nothing for server URL: registry-1.docker.io

Got output:

credentials not found in native keychain

The credential helper (docker-credential-osxkeychain) has nothing for server URL: registry.hub.docker.com

Got output:

credentials not found in native keychain

Using credentials from Docker config (/Users/sangchul/.docker/config.json) for sangchul/deploy-demo:0.1.1
The base image requires auth. Trying again for adoptopenjdk:11-jre...
The credential helper (docker-credential-osxkeychain) has nothing for server URL: registry-1.docker.io

Got output:

credentials not found in native keychain

The credential helper (docker-credential-osxkeychain) has nothing for server URL: registry.hub.docker.com

Got output:

credentials not found in native keychain

Using credentials from Docker config (/Users/sangchul/.docker/config.json) for adoptopenjdk:11-jre
Using base image with digest: sha256:ad6431b2e2a6f8016aa6d79c3f588783af9fdc06cafe131fd0d3faf560914b13

Container entrypoint set to [java, -cp, @/app/jib-classpath-file, com.example.demo.DemoApplication]

Built and pushed image as sangchul/deploy-demo:0.1.1, sangchul/deploy-demo

A new version of jib-gradle-plugin (3.1.4) is available (currently using 3.1.2). Update your build configuration to use the latest features and fixes!
https://github.com/GoogleContainerTools/jib/blob/master/jib-gradle-plugin/CHANGELOG.md

Please see https://github.com/GoogleContainerTools/jib/blob/master/docs/privacy.md for info on disabling this update check.

Executing tasks:
[==============================] 100.0% complete


Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.3.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 21s
4 actionable tasks: 4 executed

docker hub 사이트

도커 이미지 PULL 및 도커 컨테이너 실행

docker run -d -p 8080:8080 --name deploy-demo sangchul/deploy-demo
$ docker run -d -p 8080:8080 --name deploy-demo sangchul/deploy-demo
Unable to find image 'sangchul/deploy-demo:latest' locally
latest: Pulling from sangchul/deploy-demo
f3ef4ff62e0d: Already exists
706b9b9c1c44: Already exists
76205aac4d5a: Pull complete
d7a752adce1d: Pull complete
4e228e331107: Pull complete
507c54d66f4d: Pull complete
b8f9575f852c: Pull complete
Digest: sha256:caa85e25e7c16bfc802fa3e30b0fbbec4eb878acc345cf38580ce3b5fe18bfc3
Status: Downloaded newer image for sangchul/deploy-demo:latest
b1c370075dd87b2500d9b63ce6c3b29b866441ccaec55d7b376139a12c447ee3

 

docker images --filter=reference='sangchul/deploy-demo:latest'
$ docker images --filter=reference='sangchul/deploy-demo:latest'
REPOSITORY             TAG       IMAGE ID       CREATED        SIZE
sangchul/deploy-demo   latest    1ba09bae9f0f   52 years ago   262MB

 

docker ps
$ docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                    NAMES
b1c370075dd8   sangchul/deploy-demo   "java -cp @/app/jib-…"   6 minutes ago   Up 6 minutes   0.0.0.0:8080->8080/tcp   deploy-demo

 

curl -s localhost:8080
$ curl -s localhost:8080
Hello, World!

 

728x90