docker和springboot的组合案例

Owen Jia 2019年03月25日 620次浏览

重点是docker和springboot组合使用。

maven-plugins

dockerfile

Dockerfile文件需要放在项目的根目录,和pom.xml在同一个目录中。

FROM openjdk:8

MAINTAINER "Owen Jia"

LABEL email="xxxx@outlook.com"

ARG JAR_FILE

ENV ENVS="qa"

VOLUME /tmp

COPY ${JAR_FILE} /data/app.jar

RUN echo 'docker image is building'

WORKDIR /data/

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "-Xmx350MB -Xms150MB", "./app.jar", "-Denv=$ENVS"]

maven pom

dockerfile-maven-plugin是docker-maven-plugin的改进版,老版本的Dockerfile文件不是必须有的。推荐集成前者到项目中使用,最新版已经到“1.4.10”。

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.8</version>
    <executions>
        <execution>
            <id>default</id>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <repository>sample/${project.artifactId}</repository>
        <tag>${project.version}</tag>
        <buildArgs>
            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
        <buildDirectory>${project.basedir}/target</buildDirectory>
    </configuration>
</plugin>

maven build

docker build通过命令行方式运行是本质。

docker build --build-arg JAR_FILE="target/sample-1.0.0.jar -t sample/cnlink .

集成到项目中,通过maven插件方式运行

mvn dockerfile:build or mvn package 会在target目录下生成 sample-1.0.0-SANPSHOT-docker-info.jar,同时在docker本地镜像库中会生成对于的image,可以用docker image ls查看。

Gradle中docker plugin使用

上面描述都是在maven基础上build image案例,我还写了个demo是基于gradle的,已经放在github上可以关注下载直接使用,项目如下地址:

案例:gradle-test

部分代码

buildscript {
    ext{
        springBootVersion = '2.0.6.RELEASE'
    }
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath('org.springframework.boot:spring-boot-gradle-plugin:2.0.6.RELEASE')
        classpath("se.transmode.gradle:gradle-docker:1.2")
    }
}
// .....
//自定义task构建image
task dockerBuilder(type: Docker) {
    push = false
    baseImage 'openjdk:8'
    maintainer 'Owen Jia'
    applicationName = jar.baseName
    tagVersion = jar.version
    volume('/tmp')
    addFile("${jar.baseName}-${jar.version}.jar", "/data/app.jar")
    workingDir('/data/')
    entryPoint(["java", "-Denv=qa", "-jar", './app.jar'])
    exposePort(8080)
    doFirst {
        copy {
            from jar
            into stageDir
        }
    }
}

dockerBuild-info

image-ls

友情链接

maven插件地址:dockerfile-maven-plugin

作者博客网站:Owen Blog