spring boot与docker结合

spring boot可以直接将程序打包成image


新建docker registry

在180.xxx.xx.147服务器

在/etc/docker/daemon.json
添加"insecure-registries":["180.xx.xx.147:5000"] 

本地的docker也要设置

"insecure-registries":["180.xx.xx.147:5000"] 

好像因为https原因.

启动

docker run -d -p 5000:5000 --name registry registry:2

私有化docker仓库建立成功

具体可以查官方示例


新建Dockerfile文件

spring boot与docker结合_第1张图片

FROM java:openjdk-8-jre-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar

ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Duser.timezone=GMT+08","-jar","/app.jar"]

修改maven



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.examplegroupId>
    <artifactId>testartifactId>
    <version>1.0version>
    <packaging>jarpackaging>

    <name>testname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.9.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <docker.image.prefix>180.xxx.xx.147:5000docker.image.prefix>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>com.spotifygroupId>
                <artifactId>dockerfile-maven-pluginartifactId>
                <version>1.3.6version>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}repository>
                    <tag>${project.version}tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jarJAR_FILE>
                    buildArgs>
                configuration>
                <executions>
                    <execution>
                        <id>defaultid>
                        <phase>packagephase>
                        <goals>
                            <goal>buildgoal>
                            <goal>pushgoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>

project>

其中docker.image.prefix 中”180.xxx.xx.147:5000”为私有化docker仓库地址

执行命令

mvn clean package -DskipTests

会发现先在本地生成180.xxx.xx.147:5000/test:1.0 镜像,再提交到私有仓库

换个服务器,从远程仓库拉取镜像

docker pull 180.xxx.xx.xxx:5000/test:1.0 

你可能感兴趣的:(SpringBoot,spring,spring-boot,docker)