本文共 5844 字,大约阅读时间需要 19 分钟。
前面我们构建了jdk运行环境的镜像,这里我们只需要将springboot项目打包到该镜像中,形成一个新的镜像springboot-docker,到时候,利用这个镜像启动容器。再做端口映射,就可以访问我们部署在docker中的springboot项目了。
1、构建springboot项目,编写pom.xml文件。
4.0.0 com.xxx springboot-docker 0.0.1-SNAPSHOT jar springboot-docker http://maven.apache.org UTF-8 org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin com.spotify docker-maven-plugin 1.1.1 local/${project.artifactId} src/main/docker / ${project.build.directory} ${project.build.finalName}.jar
项目结构:
2、编写App.java启动类,该启动类还兼顾controller的角色。
package com.xxx.springbootwithdocker;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class App{ public static void main( String[] args ){ SpringApplication.run(App.class, args); } @GetMapping("/") public String index(){ return "{\"id\":\"1001\",\"content\":\"this is a docker\"}"; } }
3、准备打包的Dockerfile文件。src/main目录下新建docker文件夹,并新建Dockerfile文件,文件内容如下:
FROM local/centos7-jdk8VOLUME /tmpADD springboot-docker-0.0.1-SNAPSHOT.jar app.jarRUN sh -c 'touch /app.jar'ENV JAVA_OPTS=""ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
4、编译打包镜像。gitshell命令行切换到springboot-docker项目目录下,运行mvn clean package docker:build
$ mvn clean package docker:build[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building springboot-docker 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO][INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ springboot-docker ---[INFO] Deleting d:\git\GameWorld\Service\springboot-docker\target[INFO][INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ springboot-docker ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory d:\git\GameWorld\Service\springboot-docker\src\main\resources[INFO] skip non existing resourceDirectory d:\git\GameWorld\Service\springboot-docker\src\main\resources[INFO][INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ springboot-docker ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to d:\git\GameWorld\Service\springboot-docker\target\classes[INFO][INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ springboot-docker ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] skip non existing resourceDirectory d:\git\GameWorld\Service\springboot-docker\src\test\resources[INFO][INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ springboot-docker ---[INFO] No sources to compile[INFO][INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ springboot-docker ---[INFO] No tests to run.[INFO][INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ springboot-docker ---[INFO] Building jar: d:\git\GameWorld\Service\springboot-docker\target\springboot-docker-0.0.1-SNAPSHOT.jar[INFO][INFO] --- spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) @ springboot-docker ---[INFO][INFO] --- docker-maven-plugin:1.1.1:build (default-cli) @ springboot-docker ---[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier][INFO] Copying d:\git\GameWorld\Service\springboot-docker\target\springboot-docker-0.0.1-SNAPSHOT.jar -> d:\git\GameWorld\Service\springboot-docker\target\docker\springboot-docker-0.0.1-SNAPSHOT.jar[INFO] Copying src\main\docker\Dockerfile -> d:\git\GameWorld\Service\springboot-docker\target\docker\Dockerfile[INFO] Building image local/springboot-dockerStep 0 : FROM local/centos7-jdk8 ---> 526ac3c7d9bcStep 1 : VOLUME /tmp ---> Running in 3e280a0ada9a ---> a5413101d40cRemoving intermediate container 3e280a0ada9aStep 2 : ADD springboot-docker-0.0.1-SNAPSHOT.jar app.jar ---> fd445c67de28Removing intermediate container 6c88bd797017Step 3 : RUN sh -c 'touch /app.jar' ---> Running in 3848c1b364fe ---> 4a5ce0df766fRemoving intermediate container 3848c1b364feStep 4 : ENV JAVA_OPTS "" ---> Running in 7593b0bf0660 ---> e91748a27f6cRemoving intermediate container 7593b0bf0660Step 5 : ENTRYPOINT sh -c java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar ---> Running in 7f3b41d42b33 ---> f6ee77cee236Removing intermediate container 7f3b41d42b33Successfully built f6ee77cee236[INFO] Built local/springboot-docker[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 30.636 s[INFO] Finished at: 2018-09-26T10:58:38+08:00[INFO] Final Memory: 51M/458M[INFO] ------------------------------------------------------------------------
构建成功,我们可以查看镜像,启动容器。
启动成功,需要做一个端口映射,我们就可以通过windows本机上的浏览器访问springboot项目。
访问 http://localhost:18080/
至此,springboot与docker集成,已经全部完成,整个过程基本都是在配置docker镜像,构建docker镜像。