Docker+JAR

1 编写dockerfile文件

# 制作镜像
FROM java:8
# 拷贝镜像
COPY *.jar /app.jar
# 设置镜像
CMD ["--server.port=8080"]
# 暴露端口
EXPOSE 8080
# 执行
ENTRYPOINT ["java","-jar","/app.jar"]

2 上传服务器

[root@root idea]# ls
Dockerfile  hello-world.jar

3 构建docker镜像

# 构建docker镜像 并指定生成的镜像名称
[root@root idea]# docker build -t lei-hello .
Sending build context to Docker daemon  16.11MB
Step 1/5 : 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/5 : COPY *.jar /app.jar
 ---> 551ddfa4fb8c
Step 3/5 : CMD ["--server.port=8089"]
 ---> Running in d4a368614004
Removing intermediate container d4a368614004
 ---> 222a6e45de8d
Step 4/5 : EXPOSE 8089
 ---> Running in 924ecad52ffb
Removing intermediate container 924ecad52ffb
 ---> b13ea1730b74
Step 5/5 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in b8d60b44b116
Removing intermediate container b8d60b44b116
 ---> e6a00c632a4f
Successfully built e6a00c632a4f
Successfully tagged lei-hello:latest

4 查看镜像

[root@root /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
lei-hello           latest              e6a00c632a4f        About an hour ago   659MB
centos              latest              831691599b88        6 days ago          215MB
tomcat              9.0                 2eb5a120304e        13 days ago         647MB
nginx               latest              2622e6cca7eb        13 days ago         132MB
mysql               5.7                 9cfcce23593a        2 weeks ago         448MB
java                8                   d23bdf5b1b1b        3 years ago         643MB

5 运行镜像

docker run -d -P --name run_image_name image_name

6 测试请求

[root@root /]# curl localhost:32768
{"timestamp":"2020-06-23T07:12:56.846+0000","status":404,"error":"Not Found","message":"No message available","path":"/"}
原文地址:https://www.cnblogs.com/cosmosray/p/13268746.html