参考:https://juejin.im/post/5d0a25b76fb9a07ed524a438#heading-3

    1、文件

    Dockerfile

    1. # 该镜像需要依赖的基础镜像
    2. FROM java:8
    3. # 将当前目录下的jar包复制到docker容器的/目录下
    4. ADD ./target/springdocker-1.0-SNAPSHOT.jar /springdocker.jar
    5. # 运行过程中创建一个mall-tiny-docker-file.jar文件
    6. RUN bash -c 'touch /springdocker.jar'
    7. # 声明服务运行在8080端口
    8. EXPOSE 8080
    9. # 指定docker容器启动时运行jar包
    10. ENTRYPOINT ["java", "-jar","/springdocker.jar"]
    11. # 指定维护者的名字
    12. MAINTAINER tianyun

    2、打包到当前docker: springdocker docker build -t springdocker:0.0.1 .

    1. springdocker docker build -t springdocker:0.0.1 .
    2. Sending build context to Docker daemon 18.47MB
    3. Step 1/6 : FROM java:8
    4. 8: Pulling from library/java
    5. 5040bd298390: Pull complete
    6. fce5728aad85: Pull complete
    7. 76610ec20bf5: Pull complete
    8. 60170fec2151: Pull complete
    9. e98f73de8f0d: Pull complete
    10. 11f7af24ed9c: Pull complete
    11. 49e2d6393f32: Pull complete
    12. bb9cdec9c7f3: Pull complete
    13. Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
    14. Status: Downloaded newer image for java:8
    15. ---> d23bdf5b1b1b
    16. Step 2/6 : ADD ./target/springdocker-1.0-SNAPSHOT.jar /springdocker.jar
    17. ---> 244e8c7ea01e
    18. Step 3/6 : RUN bash -c 'touch /springdocker.jar'
    19. ---> Running in e0a02e06dc12
    20. Removing intermediate container e0a02e06dc12
    21. ---> 2ddb3c328a6b
    22. Step 4/6 : EXPOSE 8080
    23. ---> Running in 1cdb4722c64a
    24. Removing intermediate container 1cdb4722c64a
    25. ---> 55e8445c4ccc
    26. Step 5/6 : ENTRYPOINT ["java", "-jar","/springdocker.jar"]
    27. ---> Running in 1c8a413cc01b
    28. Removing intermediate container 1c8a413cc01b
    29. ---> 883649b9f826
    30. Step 6/6 : MAINTAINER tianyun
    31. ---> Running in bd70542bbfaf
    32. Removing intermediate container bd70542bbfaf
    33. ---> b242e6651ee4
    34. Successfully built b242e6651ee4
    35. Successfully tagged springdocker:0.0.1
    36. springdocker docker images
    37. REPOSITORY TAG IMAGE ID CREATED SIZE
    38. springdocker 0.0.1 b242e6651ee4 32 seconds ago 680MB
    39. centos/mysql-57-centos7 latest f83a2938370c 3 months ago 452MB
    40. java 8 d23bdf5b1b1b 3 years ago 643MB
    41. springdocker docker run -di \
    42. > -p 8000:8080 \
    43. > --name demo1 \
    44. > springdocker:0.0.1
    45. e68c45c6e8403e5a57c6ba51d4e15386de6c9ec6e3e9dcec062abc75afb2cfef
    46. springdocker docker ps
    47. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    48. e68c45c6e840 springdocker:0.0.1 "java -jar /springdo…" 19 seconds ago Up 18 seconds 0.0.0.0:8000->8080/tcp demo1