環境:html
對docker一無所知的看docker教程。java
引入web的起步依賴,建立一個 Controler:linux
@SpringBootApplication @RestController public class SpringbootWithDockerApplication { @RequestMapping("/") public String home() { return "Hello Docker World"; } public static void main(String[] args) { SpringApplication.run(SpringbootWithDockerApplication.class, args); } }
Docker有一個簡單的dockerfile文件做爲指定鏡像的圖層。讓咱們先建立一個 dockerFile文件:web
src/main/docker/Dockerfile:spring
FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp ADD springboot-with-docker-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 構建docker鏡像。docker
在maven的pom目錄,加上docker鏡像構建的插件windows
<properties> <docker.image.prefix>springio</docker.image.prefix> </properties> <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.11</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> </plugins> </build>
注意:${docker.image.prefix} 爲你在 docker官方倉庫的用戶名,若是你不須要上傳鏡像,隨便填。瀏覽器
經過maven 命令:springboot
第一步:mvn cleanoracle
第二步: mvn package docker:bulid ,以下:
Step 2/6 : VOLUME /tmp —> Running in a98be3878053 —> 8286e98b54c5 Removing intermediate container a98be3878053 Step 3/6 : ADD springboot-with-docker-0.0.1-SNAPSHOT.jar app.jar —> c6ce13e50bbd Removing intermediate container a303a3058869 Step 4/6 : RUN sh -c ‘touch /app.jar’ —> Running in cf231afe700e —> 9a0ec8936c00 Removing intermediate container cf231afe700e Step 5/6 : ENV JAVA_OPTS 「」 —> Running in e192597fc881 —> 2cb0d73bbdb0 Removing intermediate container e192597fc881 Step 6/6 : ENTRYPOINT sh -c java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar —> Running in ab85f53fcdd8 —> 60fdb5c61692 Removing intermediate container ab85f53fcdd8 Successfully built 60fdb5c61692 [INFO] Built forezp/springboot-with-docker [INFO] ———————————————————————— [INFO] BUILD SUCCESS [INFO] ———————————————————————— [INFO] Total time: 01:45 min [INFO] Finished at: 2017-04-19T05:37:44-07:00 [INFO] Final Memory: 19M/48M [INFO] ————————————————————————
鏡像構建成功。查看鏡像:
docker images
顯示:
forezp/springboot-with-docker latest 60fdb5c61692 About a minute ago 195 MB
啓動鏡像:
$ docker run -p 8080:8080 -t forezp/springboot-with-docker
打開瀏覽器訪問 localhost:8080;瀏覽器顯示:Hello Docker World。
說明docker 的springboot工程已部署。
中止鏡像:
docker stop 60fdb5c61692
刪除鏡像:
docker rm 60fdb5c61692