logo头像
Snippet 博客主题

使用docker部署spring boot/cloud项目

需要环境:docker

安装详见gitbook(貌似需要翻墙)
或者 国内镜像

添加docker插件

项目/模块中的pom文件添加:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <configuration>
        <imageName>${project.name}:${project.version}</imageName>
        <imageTags>
            <imageTag>${project.version}</imageTag>
        </imageTags>
        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
        <skipDockerBuild>false</skipDockerBuild>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <includes>
                    <include>${project.build.finalName}.jar</include>
                </includes>
            </resource>
        </resources>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
</plugin>

编写Dockerfile

在项目src/main/docker目录下

FROM java:8

VOLUME /tmp

RUN mkdir /app

COPY api.jar /app/app.jar

WORKDIR /app

EXPOSE 8080

CMD ["java","-jar","/app/app.jar"]

制作镜像

使用docker-maven-plugin,默认将镜像打包到本地。如果想打包到远程仓库,需要配置环境变量DOCKER_HOST为远程仓库地址。

执行下面命令

mvn package docker:build -DskipTests

输出:

$ mvn package docker:build -DskipTests
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.wekri.goma:api:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for com.spotify:docker-maven-plugin is missing. @ line 58, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building api 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ api ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 7 resources
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ api ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\gitee\goma\api\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ api ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ api ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ api ---
[INFO] Building jar: D:\gitee\goma\api\target\api.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) @ api ---
[INFO]
[INFO] --- docker-maven-plugin:1.0.0:build (default-cli) @ api ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying D:\gitee\goma\api\target\api.jar -> D:\gitee\goma\api\target\docker\api.jar
[INFO] Copying D:\gitee\goma\api\src\main\docker\Dockerfile -> D:\gitee\goma\api\target\docker\Dockerfile
[INFO] Building image api:0.0.1-SNAPSHOT
Step 1/7 : FROM java:8
 ---> d23bdf5b1b1b
Step 2/7 : VOLUME /tmp
 ---> Using cache
 ---> a6e6b621afc9
Step 3/7 : RUN mkdir /app
 ---> Using cache
 ---> 8ce5420be1d7
Step 4/7 : ADD api.jar /app/app.jar
 ---> 204a9a963e07
Step 5/7 : WORKDIR /app
 ---> e09fa1c2ec37
Removing intermediate container 4bf981aadc1c
Step 6/7 : EXPOSE 8082
 ---> Running in 5b3a619b3bd9
 ---> 48492337aadb
Removing intermediate container 5b3a619b3bd9
Step 7/7 : CMD java -jar /app/app.jar
 ---> Running in e8079e109c84
 ---> 289cec5d6e5a
Removing intermediate container e8079e109c84
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 289cec5d6e5a
Successfully tagged api:0.0.1-SNAPSHOT
[INFO] Built api:0.0.1-SNAPSHOT
[INFO] Tagging api:0.0.1-SNAPSHOT with 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.376 s
[INFO] Finished at: 2018-05-15T12:56:59+08:00
[INFO] Final Memory: 46M/482M
[INFO] ------------------------------------------------------------------------

运行镜像

看先一下镜像:

docker images

docker run -d -p 8080:8082 api.jar:0.0.1-SNAPSHOT

容器编排

  1. docker compose
  2. mesos
  3. kubernetes
微信打赏

赞赏是不耍流氓的鼓励