我爱编程

Docker 发布Spring Boot应用

2018-05-21  本文已影响165人  沧海一粟谦
Pride & Prejudice

创建工程

修改pom.xml文件,添加docker-maven-plugin:

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <docker.image.prefix>springboot</docker.image.prefix>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 打包部署到tomcat上面时,不需要打包tmocat相关的jar包,否则会引起jar包冲突-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- Docker maven plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <!-- Docker maven plugin -->
        </plugins>
    </build>
</project>

docker-maven-plugin插件用于将Spring Boot工程构建为Docker镜像:

编译并运行

在项目的根目录执行如下命令

mvn package && java -jar target/thymeleaf-0.0.1-SNAPSHOT.jar

打开浏览器,访问http://localhost:8080 运行成功。

容器化项目

首先要确保你的机器上安装了Docker,然后将上面的Spring Boot工程上传到安装了Docker的服务器上。

在目录src/main/docker下创建 Dockerfile 文件

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD thymeleaf-0.0.1-SNAPSHOT.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" ]

安装MAVEN

下载:http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz

## 解压
tar vxf apache-maven-3.5.3-bin.tar.gz
## 移动
mv apache-maven-3.5.3 /usr/local/maven3

修改环境变量, 在/etc/profile中添加以下几行

MAVEN_HOME=/usr/local/maven3/apache-maven-3.5.3
export MAVEN_HOME
export PATH=${PATH}:${MAVEN_HOME}/bin

使环境变量生效:source /etc/profile
查看Maven信息:mvn -version

将项目 spring-boot-thymeleaf 拷贝服务器中,进入项目路径下进行打包

#打包
mvn package
#启动
java -jar target/spring-boot-docker-1.0.jar

看到 Spring Boot 的启动日志后表明环境配置没有问题,接下来我们使用 DockerFile 构建镜像

mvn package docker:build

运行Docker镜像

sudo docker run -p 8080:8080 -t springboot/thymeleaf

查看正在运行的Docker容器

sudo docker ps

参考
Spring Boot with Docker
Spring Boot应用发布到Docker

上一篇下一篇

猜你喜欢

热点阅读