Docker 简介 Docker 是一个 Linux 容器管理工具包,具备“社交”方面,允许用户发布容器的 image (镜像),并使用别人发布的 image。Docker image 是用于运行容器化进程的方案,在本文中,我们将构建一个简单的 Spring Boot 应用程序。 有关 Docker 的详细介绍,可以移步至 《简述 Docker》 前置条件
创建目录结构 项目的目录结构因符合 Gradle 的约定。 在 *nix 系统下执行 mkdir -p src/main/java/docker_spring_boot ,生产如下结构 : └── src└── main └── java └── com └── waylau └── docker_spring_boot 创建 Gradle 构建文件build.gradle buildscript { repositories { mavenCentral() } dependencies { classpath('org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE') classpath('se.transmode.gradle:gradle-docker:1.2') } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'docker' group = 'gregturn' jar { baseName = 'docker-spring-boot-gradle' version = '1.0.0' } task buildDocker(type: Docker, dependsOn: build) { push = true applicationName = jar.baseName dockerfile = file('src/main/docker/Dockerfile') doFirst { copy { from jar into stageDir } } } repositories { mavenCentral() } sourceCompatibility = 1.8 targetCompatibility = 1.8 dependencies { compile("org.springframework.boot:spring-boot-starter-web") testCompile("org.springframework.boot:spring-boot-starter-test") } task wrapper(type: Wrapper) { gradleVersion = '2.3' } Spring Boot gradle plugin 提供了很多方便的功能: 它收集的类路径上所有 jar 文件,并构建成一个单一的、可运行的“über-jar”(德语,相关解释可以移步至 http://stackoverflow.com/questions/11947037/what-is-an-uber-jar),这使得它更方便地执行和传输服务。 编写 Spring Boot 应用编写一个简单的 Spring Boot 应用 : src/main/java/com/waylau/docker_spring_boot/Application.java:package com.waylau.docker_spring_boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class Application { @RequestMapping("/") public String home() { return "Hello Docker World." + "<br />Welcome to <a href='http://waylau.com'>waylau.com</a></li>"; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 解释下上面的代码: 类用 @SpringBootApplication @RestController 标识,可用 Spring MVC 来处理 Web 请求。 @RequestMapping 将 / 映射到 home() ,并将”Hello Docker World” 文本作为响应。 main() 方法使用 Spring Boot 的 SpringApplication.run() 方法来启动应用。 运行程序使用 Gradle gradle build编译: 运行: java -jar build/libs/docker-spring-boot-gradle-1.0.0.jar访问项目 如果程序正确运行,浏览器访问 http://localhost:8080/,可以看到页面 “Hello Docker World.” 字样。 将项目容器化Docker 使用 Dockerfile 文件格式来指定 image 层, 创建文件 src/main/docker/Dockerfile: FROM frolvlad/alpine-oraclejdk8:slimVOLUME /tmp ADD docker-spring-boot-gradle-1.0.0.jar app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] 解释下这个配置文件:
项目的 jar 文件作为 “app.jar” 添加到容器的 ENTRYPOINT 执行项目 app.jar。为了缩短 Tomcat 启动时间,添加一个系统属性指向 “/dev/urandom” 作为 Entropy Source 构建 Docker Image执行构建成为 docker image: gradle build buildDocker运行 docker run -p 8080:8080 -t waylau/docker-spring-boot-gradle运行 Docker Image [root@waylau spring-boot]# docker run -p 8080:8080 -t waylau/docker-spring-boot-gradle . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.3.RELEASE) 2016-03-20 08:45:51.276 INFO 1 2016-03-20 08:45:51.289 INFO 1 2016-03-20 08:45:51.722 INFO 1 2016-03-20 08:45:54.874 INFO 1 2016-03-20 08:45:57.893 INFO 1 2016-03-20 08:45:57.982 INFO 1 2016-03-20 08:45:57.984 INFO 1 2016-03-20 08:45:58.473 INFO 1 2016-03-20 08:45:58.473 INFO 1 2016-03-20 08:45:59.672 INFO 1 2016-03-20 08:45:59.695 INFO 1 2016-03-20 08:45:59.701 INFO 1 2016-03-20 08:45:59.703 INFO 1 2016-03-20 08:45:59.703 INFO 1 2016-03-20 08:46:00.862 INFO 1 2016-03-20 08:46:01.166 INFO 1 2016-03-20 08:46:01.189 INFO 1 2016-03-20 08:46:01.190 INFO 1 2016-03-20 08:46:01.302 INFO 1 2016-03-20 08:46:01.302 INFO 1 2016-03-20 08:46:01.438 INFO 1 2016-03-20 08:46:01.833 INFO 1 2016-03-20 08:46:02.332 INFO 1 2016-03-20 08:46:02.343 INFO 1 访问项目 如果程序正确运行,浏览器访问 http://localhost:8080/,可以看到页面 “Hello Docker World.” 字样。 推送 image 到 Docker Hub 首先,你在 Docker Hub 要有注册账号,且创建了相应的库; 其次,docker 推送前,先要登录,否则报unauthorized: access to the requested resource is not authorized的错误 执行: docker login输出为: [root@waylau spring-boot]Username: waylau Password: Email: waylau521@gmail.com WARNING: login credentials saved in /root/.docker/config.json Login Succeeded 执行推送 docker push waylau/docker-spring-boot-gradle[root@waylau spring-boot] The push refers to a repository [docker.io/waylau/docker-spring-boot-gradle] 751d29eef02e: Layer already exists 4da3741f39c7: Pushed 5f70bf18a086: Layer already exists 7e4d0cb13643: Layer already exists 8f045733649f: Layer already exists latest: digest: sha256:eb4d5308ba1bb27489d808279e74784bda6761b3574f4298d746abba59b35164 size: 9415 镜像加速器 Docker Hub 在国外,有时候拉取 Image 极其缓慢,可以使用国内的镜像来实现加速 阿里云echo "DOCKER_OPTS=\"--registry-mirror=https://yourlocation.mirror.aliyuncs.com\"" | sudo tee -a /etc/default/dockersudo service docker restart 其中 https://yourlocation.mirror.aliyuncs.com 是您在阿里云注册后的专属加速器地址: DaoCloudsudo echo “DOCKER_OPTS=\”\$DOCKER_OPTS –registry-mirror=http:sudo service docker restart 其中 http://your-id.m.daocloud.io 是您在 DaoCloud 注册后的专属加速器地址: 源码 获取项目源码, https://github.com/waylau/docker-demos 中的 samples/spring-boot-gradle 获取项目镜像, 执行 docker pull waylau/docker-spring-boot-gradle参考引用
![]() |
|免责声明|本站介绍|工控课堂
( 沪ICP备14007696号-3 )|网站地图
GMT+8, 2019-4-16 21:46 , Processed in 0.038997 second(s), 34 queries .
Powered by Discuz! X3.4
© 2001-2017 Comsenz Inc.