手动打包运行

  1. 前提是服务器上已经安装好了docker
[root@VM-4-7-centos docker]# docker -v
Docker version 20.10.16, build aa7e414

如果没安装docker,参考文档进行安装:linux下docker的安装

编写Dockerfile文件,参考样例:

FROM openjdk:8
ADD miniprogram.jar /app.jar
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone
EXPOSE 17788
ENTRYPOINT ["java","-jar","/app.jar"]
  1. 把Dockerfile和jar包上传到服务器上,进行docker镜像打包
[root@VM-4-7-centos docker]# docker build -t miniprogram:1 .
Sending build context to Docker daemon  55.71MB
Step 1/6 : FROM openjdk:8
8: Pulling from library/openjdk
001c52e26ad5: Pull complete 
d9d4b9b6e964: Pull complete 
2068746827ec: Pull complete 
9daef329d350: Pull complete 
d85151f15b66: Pull complete 
52a8c426d30b: Pull complete 
8754a66e0050: Pull complete 
Digest: sha256:86e863cc57215cfb181bd319736d0baf625fe8f150577f9eb58bd937f5452cb8
Status: Downloaded newer image for openjdk:8
 ---> b273004037cc
Step 2/6 : ADD miniprogram.jar /app.jar
 ---> a41af6ae574f
Step 3/6 : RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 ---> Running in c490d72d3d50
Removing intermediate container c490d72d3d50
 ---> 0940555f45f4
Step 4/6 : RUN echo 'Asia/Shanghai' > /etc/timezone
 ---> Running in b08c7cb6d124
Removing intermediate container b08c7cb6d124
 ---> e36b5edb8807
Step 5/6 : EXPOSE 17788
 ---> Running in 34c250f7f2ee
Removing intermediate container 34c250f7f2ee
 ---> 62f9371f5f49
Step 6/6 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in 150eb938d837
Removing intermediate container 150eb938d837
 ---> f35842cde0d1
Successfully built f35842cde0d1

注意:当前dcoker目录下有:miniprogram.jar Dcokerfile 两个文件

上面Dockerfile中FROM openjdk:8 ,如果用java:8 会出现错误: manifest for java:8-jdk-alpine not found: manifest unknown

  1. 验证打包好的镜像
[root@VM-4-7-centos docker]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
miniprogram             1         f35842cde0d1   24 seconds ago   582MB
  1. 运行打包好的镜像
[root@VM-4-7-centos docker]# docker run -d -p 17788:17788 miniprogram:1
f9049ae0ebe4b79e7eebdde369f5e390a8e5bb4c95d27e240620f2ee98106cd7