首先使用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 驗證。