스프링 부트에서 도커 컨테이너 이미지 생성하기
참고 사이트 : https://spring.io/guides/gs/spring-boot-docker/
예제 코드 생성
@SpringBootApplication
@RestController
public class DockerTestApplication {
@RequestMapping("/")
public String sayHello() {
return "Hello CI";
}
public static void main(String[] args) {
SpringApplication.run(DockerTestApplication.class, args);
}
}
- maven 을 활용하여 jar 파일 생성 및 실행
$ mvn package && java -jar target/*.jar
$ curl localhost:8080
Hello CI
Dockerfile 생성
- maven package 로 생성된 jar 파일을 컨테이너 이미지 안에 넣고 엔트리 포인트를 지정한다.
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Docker 이미지 생성
- 실행 명령어 :
docker build -t ci/hello-ci .
$docker build -t ci/hello-ci . SIGINT(2) ↵
Sending build context to Docker daemon 16.75MB
Step 1/4 : FROM openjdk:8-jdk-alpine
8-jdk-alpine: Pulling from library/openjdk
e7c96db7181b: Pull complete
f910a506b6cb: Pull complete
c2274a1a0e27: Pull complete
Digest: sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3
Status: Downloaded newer image for openjdk:8-jdk-alpine
---> a3562aa0b991
Step 2/4 : ARG JAR_FILE=target/*.jar
---> Running in a50bdf4f6a76
Removing intermediate container a50bdf4f6a76
---> a44d7bc3b35f
Step 3/4 : COPY ${JAR_FILE} app.jar
---> ca072fc923ee
Step 4/4 : ENTRYPOINT ["java","-jar","/app.jar"]
---> Running in d5fb7258f922
Removing intermediate container d5fb7258f922
---> 336f8ce8cdc3
Successfully built 336f8ce8cdc3
Successfully tagged ci/hello-ci:latest
Docker 이미지 실행
- 실행 명령어 :
docker run -p 8080:8080 ci/hello-ci
$ docker run -p 8080:8080 ci/hello-ci
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-08 00:46:10.959 INFO 1 --- [ main] c.e.dockertest.DockerTestApplication : Starting DockerTestApplication v0.0.1-SNAPSHOT on 145d1d9f806a with PID 1 (/app.jar started by root in /)
2020-07-08 00:46:10.968 INFO 1 --- [ main] c.e.dockertest.DockerTestApplication : No active profile set, falling back to default profiles: default
2020-07-08 00:46:13.175 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-07-08 00:46:13.211 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-07-08 00:46:13.212 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-08 00:46:13.399 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-07-08 00:46:13.400 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2305 ms
2020-07-08 00:46:14.013 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-08 00:46:14.453 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-07-08 00:46:14.480 INFO 1 --- [ main] c.e.dockertest.DockerTestApplication : Started DockerTestApplication in 4.277 seconds (JVM running for 5.158)