参考:https://juejin.im/post/5d0a25b76fb9a07ed524a438#heading-3
1、文件
Dockerfile
# 该镜像需要依赖的基础镜像
FROM java:8
# 将当前目录下的jar包复制到docker容器的/目录下
ADD ./target/springdocker-1.0-SNAPSHOT.jar /springdocker.jar
# 运行过程中创建一个mall-tiny-docker-file.jar文件
RUN bash -c 'touch /springdocker.jar'
# 声明服务运行在8080端口
EXPOSE 8080
# 指定docker容器启动时运行jar包
ENTRYPOINT ["java", "-jar","/springdocker.jar"]
# 指定维护者的名字
MAINTAINER tianyun
2、打包到当前docker: springdocker docker build -t springdocker:0.0.1 .
➜ springdocker docker build -t springdocker:0.0.1 .
Sending build context to Docker daemon 18.47MB
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 : ADD ./target/springdocker-1.0-SNAPSHOT.jar /springdocker.jar
---> 244e8c7ea01e
Step 3/6 : RUN bash -c 'touch /springdocker.jar'
---> Running in e0a02e06dc12
Removing intermediate container e0a02e06dc12
---> 2ddb3c328a6b
Step 4/6 : EXPOSE 8080
---> Running in 1cdb4722c64a
Removing intermediate container 1cdb4722c64a
---> 55e8445c4ccc
Step 5/6 : ENTRYPOINT ["java", "-jar","/springdocker.jar"]
---> Running in 1c8a413cc01b
Removing intermediate container 1c8a413cc01b
---> 883649b9f826
Step 6/6 : MAINTAINER tianyun
---> Running in bd70542bbfaf
Removing intermediate container bd70542bbfaf
---> b242e6651ee4
Successfully built b242e6651ee4
Successfully tagged springdocker:0.0.1
➜ springdocker docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springdocker 0.0.1 b242e6651ee4 32 seconds ago 680MB
centos/mysql-57-centos7 latest f83a2938370c 3 months ago 452MB
java 8 d23bdf5b1b1b 3 years ago 643MB
➜ springdocker docker run -di \
> -p 8000:8080 \
> --name demo1 \
> springdocker:0.0.1
e68c45c6e8403e5a57c6ba51d4e15386de6c9ec6e3e9dcec062abc75afb2cfef
➜ springdocker docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e68c45c6e840 springdocker:0.0.1 "java -jar /springdo…" 19 seconds ago Up 18 seconds 0.0.0.0:8000->8080/tcp demo1