SpringBoot微服务打包docker镜像
创建一个boot工程
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编写一个controller
@RestController
public class IndexController {
@GetMapping("/index")
public String index(){
return "success";
}
}
- 安装Docker插件
编写dockerfile
FROM java:8
COPY *.jar /app.jar
CMD [“—server.port=8080”]
EXPOSE 8080
ENTRYPOINT [“java”,”-jar”,”/app.jar”]
打包服务
- 将jar包和Dockerfile拷贝到/home/myboot目录下
构建镜像
docker build -f Dockerfile -t demo:1.0 .
运行这个镜像,并映射端口
docker run -d -it -p8080:8080 —name demo01 demo:1.0
- 访问 虚拟机ip加8080/index
构建成功!
还没有评论,来说两句吧...