win10 docker部署springboot项目

docker安装和maven安装忽略

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>deploy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>deploy</name>
    <description>Demo project for Spring Boot</description>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.5.RELEASE</version>
    </parent>


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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR7是版本号 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <mainClass>com.example.deploy.DeployApplication</mainClass>
                </configuration>
            </plugin>

            <!--加入maven插件 docker-maven-plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.13</version>
                <configuration>
                    <!--镜像仓库源-->
                    <imageName>${docker.prefix}/${project.artifactId}</imageName>
                    <!--docker配置文件的路径-->
                    <dockerDirectory>src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>

        </plugins>
    </build>


</project>
View Code

 

项目结构:

在main 目录下创建 docker 文件夹, 再在里面创建一个Dockerfile文件,文件内容为:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD deploy-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
View Code
  1. 基础镜像信息 
  2. 数据持久化的方式
  3. 重命名为app.jar
  4. 启动运行 java -jar app.jar

 

Application 文件

@SpringBootApplication
@RestController
public class DeployApplication {

    @RequestMapping("/")
    public String demo() {
        return "第一次使用 docker 部署 springboot ";
    }

    public static void main(String[] args) {
        SpringApplication.run(DeployApplication.class, args);
    }

}
View Code

application.properties

server.port=8080
View Code

 勾选这个选项

 

命令行进入项目目录输入:mvn clean

再执行:mvn package

 

部署上docker

 在命令行输入:mvn package docker:build

运行没有报错就没有问题了。

命令行运行:docker images 查看当前所有镜像

 

再输入:docker run -p 8080:8080 -t -i springboot/deploy    运行镜像  (-p 指定端口映射, -t 容器重新分配一个伪输入终端 ,-i 以交互模式运行容器)

 

再输入:docker ps 查看我们正在运行的容器

 

在浏览器输入:http://localhost:8080/

 

 

posted @ 2019-07-23 09:48  零落星尘  阅读(1336)  评论(0编辑  收藏  举报