本文主要講的是使用docker運行springboot項目java
這裏我沒有從新構建,用的以前寫的一個項目,直接從github上下載下來,地址:https://github.com/FunCodingOfWe/kevin-boot
注意:我如今已經將dockerfile上傳git
# 基礎鏡像使用java FROM java:8 # 做者 MAINTAINER kevin <923604825@qq.com> # VOLUME 指定了臨時文件目錄爲/tmp。 # 其效果是在主機 /var/lib/docker 目錄下建立了一個臨時文件,並連接到容器的/tmp VOLUME /tmp # 將jar包添加到容器中並改名爲app.jar ADD kevin-admin-0.0.1-SNAPSHOT.jar app.jar # 運行jar包 RUN bash -c 'touch /app.jar' ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
整理的目錄結構以下
github
一、運行mvn install 將項目打包成jar包
二、複製jar包和Dockerfile文件到一個文件夾下
三、進入該文件夾,而後運行 docker build -t kevin-boot .
spring
$ docker build -t kevin-boot . Sending build context to Docker daemon 37.17MB Step 1/6 : FROM java:8 8: Pulling from library/java 5040bd298390: Pull complete fce5728aad85: Pull complete 76610ec20bf5: Pull complete 60170fec2151: Pull complete e98f73de8f0d: Pull complete 11f7af24ed9c: Pull complete 49e2d6393f32: Pull complete bb9cdec9c7f3: Pull complete Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d Status: Downloaded newer image for java:8 ---> d23bdf5b1b1b Step 2/6 : MAINTAINER kevin <923604825@qq.com> ---> Running in 5221f818f2b5 Removing intermediate container 5221f818f2b5 ---> 87c87989d791 Step 3/6 : VOLUME /tmp ---> Running in 6c23e4541df0 Removing intermediate container 6c23e4541df0 ---> 62ff9644cf44 Step 4/6 : ADD kevin-admin-0.0.1-SNAPSHOT.jar app.jar ---> a3362f0f3ce0 Step 5/6 : RUN bash -c 'touch /app.jar' ---> Running in a40ec77f3c91 Removing intermediate container a40ec77f3c91 ---> 4e3417d31bd4 Step 6/6 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] ---> Running in b8f66a99abc3 Removing intermediate container b8f66a99abc3 ---> 9c7986106945 Successfully built 9c7986106945 Successfully tagged kevin-boot:latest
4.檢查是否已經存在鏡像 docker imagesdocker
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE kevin-boot latest 9c7986106945 40 seconds ago 718MB java 8 d23bdf5b1b1b 2 years ago 643MB
咱們能夠看到已經存在springboot
docker run -d -p 8099:8080 kevin-boot
驗證一下bash
輸入http://localhost:8099/hello
app
好了,玩的開心dom