首先使用SpringBoot创建一个Web项目,我这里创建的是一个简单的helloworld项目。
保证能正常运行。
package com.gao.hello.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("world")
public String world(){
return "hello world";
}
}
然后我们使用Maven plugin对我们的helloWorld项目进行打包,hello-0.0.1-SNAPSHOT.jar;然后创建一个Dockerfile文件,编辑内容为:
FROM java:8
VOLUME /tmp
ADD hello-0.0.1-SNAPSHOT.jar /hello.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/hello.jar"]
将jar文件和Docker文件放在同一个文件夹下,然后打开PowerShell 进入对应的文件目录。
输入下面的命令进行构建镜像
docker build -t hello .
等待完成后,输入:docker images 查看当前存在哪些镜像;便可以看到我们刚才构建的hello镜像。
到这里说明我们已经创建好了hello镜像,然后开始启动我们的镜像,使用命令
docker run -p 8080:8080 -t hello
然后我们可以查看到启动日志
通过浏览器访问http://localhost:8080/hello 验证。