因公司使用Docker,最理想的状况是研发提测时给的就是镜像,这样可避免升级包到实施环境时是错误的。
研发环境基本都是Eclipse,且在windows操作系统上,而Docker是依赖Linux环境,那么首先需要做的就是在windows环境中安装docker,可参考。
- 基础配置 windows 中的Docker使用
- Eclipse中安装Gradle
- java代码
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class Application {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
- build.gradle
buildscript {
repositories {
maven { url "http://ip:port/repository/jcenter/"}
maven { url "http://ip:port/repository/maven-public/"}
maven { url "http://ip:port/repository/spring-public/"}
maven { url "http://ip:port/repository/maven-central/"}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'docker'
// This is used as the docker image prefix (org)
group = 'gregturn'
sourceSets {
main {
java.srcDir "src"
}
}
jar {
baseName = 'gs-spring-boot-docker'
version = '0.1.0'
}
// tag::task[]
task buildDocker(type: Docker, dependsOn: build) {
push = true
applicationName = jar.baseName
dockerfile = file('src/Dockerfile')
doFirst {
copy {
from jar
into stageDir
}
}
}
// end::task[]
bootRepackage {
mainClass = 'Application'
}
repositories {
maven { url "http://ip:port/repository/jcenter/"}
maven { url "http://ip:port/repository/maven-public/"}
maven { url "http://ip:port/repository/spring-public/"}
maven { url "http://ip:port/repository/maven-central/"}
}
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
dependencies {
// tag::jetty[]
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
// end::jetty[]
// tag::actuator[]
compile("org.springframework.boot:spring-boot-starter-actuator")
// end::actuator[]
testCompile("junit:junit")
}
- 编写Dockerfile
在src目录下
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
- 设置运行时环境
在windows环境中需设置docker的环境变量
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=C:\Users\lixr.docker\machine\machines\default
SET DOCKER_MACHINE_NAME=default
不设置环境变量会报如下错误
Caused by: org.gradle.api.GradleException: Docker execution failed
Command line [docker build -t gregturn/gs-spring-boot-docker:latest D:\workspace\SpringBoot\build\docker] returned:
An error occurred trying to connect:
Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/build?buildargs=%7B%7D&cgroupparent=&cpuperiod=0&cpuquota=0&
cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&rm=1&shmsize=0&
t=gregturn%2Fgs-spring-boot-docker%3Alatest&ulimits=null: open //./pipe/docker_engine: The system cannot find the file specified.
- gradle build
运行前设置- task 为 buildDocker
运行--gradle build
执行后结果如下:
[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks:
[sts] buildDocker
[sts] -----------------------------------------------------
Spring Boot plugin's support for Gradle 2.2.1 is deprecated. Please upgrade to Gradle 2.9 or later.
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:jar
:bootRepackage
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
:buildDocker
Sending build context to Docker daemon 557.1 kB
Sending build context to Docker daemon 1.114 MB
Sending build context to Docker daemon 1.671 MB
Sending build context to Docker daemon 2.228 MB
Sending build context to Docker daemon 2.785 MB
Sending build context to Docker daemon 3.342 MB
Sending build context to Docker daemon 3.899 MB
Sending build context to Docker daemon 4.456 MB
Sending build context to Docker daemon 5.014 MB
Sending build context to Docker daemon 5.571 MB
Sending build context to Docker daemon 6.128 MB
Sending build context to Docker daemon 6.685 MB
Sending build context to Docker daemon 7.242 MB
Sending build context to Docker daemon 7.799 MB
Sending build context to Docker daemon 8.356 MB
Sending build context to Docker daemon 8.913 MB
Sending build context to Docker daemon 9.47 MB
Sending build context to Docker daemon 10.03 MB
Sending build context to Docker daemon 10.58 MB
Sending build context to Docker daemon 11.14 MB
Sending build context to Docker daemon 11.7 MB
Sending build context to Docker daemon 12.26 MB
Sending build context to Docker daemon 12.81 MB
Sending build context to Docker daemon 13.37 MB
Sending build context to Docker daemon 13.93 MB
Sending build context to Docker daemon 14.08 MB
Step 1 : FROM frolvlad/alpine-oraclejdk8:slim
---> a290f8607aef
Step 2 : VOLUME /tmp
---> Using cache
---> 940076062032
Step 3 : ADD gs-spring-boot-docker-0.1.0.jar app.jar
---> e73954cc8bc1
Removing intermediate container 45e68b00fb97
Step 4 : RUN sh -c 'touch /app.jar'
---> Running in 3feab29e4de4
---> 51be10b87108
Removing intermediate container 3feab29e4de4
Step 5 : ENV JAVA_OPTS ""
---> Running in bcf859208dd7
---> fdb1998eae9c
Removing intermediate container bcf859208dd7
Step 6 : ENTRYPOINT sh -c java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar
---> Running in 408a0f6b60f8
---> 497ace0d0f8d
Removing intermediate container 408a0f6b60f8
Successfully built 497ace0d0f8d
BUILD SUCCESSFUL
Total time: 30.579 secs
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 0 min, 31 sec
[sts] -----------------------------------------------------
- 运行镜像
docker images
docker@default:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gregturn/gs-spring-boot-docker latest 497ace0d0f8d 12 minutes ago 195.3 MB
python latest 7045ed20ac61 2 weeks ago 682.7 MB
frolvlad/alpine-oraclejdk8 slim a290f8607aef 5 weeks ago 167.2 MB
docker run -ti -p 8080:8080 497ac
- 访问
http://192.168.99.100:8080/
显示Hello World!