本文主要讲的是使用docker运行springboot项目
获取一个springboot项目
这里我没有重新构建,用的之前写的一个项目,直接从github上下载下来,地址:https://github.com/FunCodingOfWe/kevin-boot
注意:我现在已经将dockerfile上传
编写Dockerfile
# 基础镜像使用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"]
整理的目录结构如下
生成docker镜像
1、运行mvn install 将项目打包成jar包
2、复制jar包和Dockerfile文件到一个文件夹下
3、进入该文件夹,然后运行 docker build -t kevin-boot .
$ 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 images
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kevin-boot latest 9c7986106945 40 seconds ago 718MB
java 8 d23bdf5b1b1b 2 years ago 643MB
我们可以看到已经存在
运行kevin-boot镜像
docker run -d -p 8099:8080 kevin-boot
验证一下
输入http://localhost:8099/hello
好了,玩的开心