1、将项目打包成 jar 包
tomkluas-0.0.1-SNAPSHOT.jar
2、复制 jar 文件至 docker 镜像制作机器,创建Dockerfile 文件
FROM openjdk:8-jdk-alpine VOLUME /tmp ADD tomkluas-0.0.1-SNAPSHOT.jar app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
文件目录如下:
[root@localhost]# ls tomkluas-0.0.1-SNAPSHOT.jar Dockerfile
3、创建镜像
# -t tag 给镜像打标签 # . 为当前目录 [root@localhost air]# docker build -t tomkluas:v1 . Sending build context to Docker daemon 39.8MB Step 1/4 : FROM openjdk:8-jdk-alpine 8-jdk-alpine: Pulling from library/openjdk e7c96db7181b: Pull complete f910a506b6cb: Pull complete c2274a1a0e27: Pull complete Digest: sha256:94792824df2df33402f201713f932b58cb9de94a0cd524164a0f2283343547b3 Status: Downloaded newer image for openjdk:8-jdk-alpine ---> a3562aa0b991 tep 2/4 : VOLUME /tmp ---> Running in af5ab6d97079 Removing intermediate container af5ab6d97079 ---> 450bb894696d Step 3/4 : ADD tomkluas-0.0.1-SNAPSHOT.jar app.jar ---> 642cefdc3239 Step 4/4 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] ---> Running in 2941b6eceb00 Removing intermediate container 2941b6eceb00 ---> 8849b2bd77e7 Successfully built 8849b2bd77e7 Successfully tagged tomkluas:v1
4、查看镜像
[root@localhost ~]# docker images tomkluas REPOSITORY TAG IMAGE ID CREATED SIZE tomkluas v1 8849b2bd77e7 31 minutes ago 145MB
5、创建容器,测试是否能正确运行
docker run -d --name tomkluasV1 -p 8090:8080 tomkluas:v1
测试访问正常
6、登录仓库
docker login https://hub.mysite.com
依次输入账号密码登录
7、将镜像打标签
docker tag 8849b2bd77e7 hub.mysite.com/tom/tomkluas:v1
8、推送标签到hub
docker push hub.mysite.com/tom/tomkluas:v1
至此,镜像已经推送至hub,后续可在部署的机器上拉取镜像、创建实例。