原文链接:https://www.cnblogs.com/zhenghengbin/p/11619915.html
本博客转载内容均为个人学习和记录,如有侵犯请联系博主删除。
编写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
这里我自己用的运行命令:docker run --name postman -d -p 8091:8091 postman