Springboot应用打包
使用
spring-boot-maven-plugin
插件,打包为可执行jar<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
新建Dockerfile文件
文件可以直接放在工程根目录下(和target同级目录)
FROM openjdk:8u212-jre LABEL author=Jason email=qianhao_cs@163.com COPY target/SpringbootDemo-1.0-SNAPSHOT.jar /SpringbootDemo-1.0-SNAPSHOT.jar ENTRYPOINT ["java", "-jar", "/SpringbootDemo-1.0-SNAPSHOT.jar"]
构建镜像
可以使用IDEA的docker插件直接构建
也可以使用命令进行构建
qianhao@qianhaodeMacBook-Pro.local:SpringbootDemo (main !*%) $ docker build -t jason/springbootdemo:test . [+] Building 83.3s (7/7) FINISHED docker:desktop-linux => [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 284B 0.0s => [internal] load .dockerignore 0.1s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/openjdk:8u212-jre 5.4s => [internal] load build context 0.8s => => transferring context: 20.83MB 0.8s => [1/2] FROM docker.io/library/openjdk:8u212-jre@sha256:11cbb99bc937a7cf5735857d418f98a9287b627f2caa01925f9ec677ad6c7abe 77.0s => => resolve docker.io/library/openjdk:8u212-jre@sha256:11cbb99bc937a7cf5735857d418f98a9287b627f2caa01925f9ec677ad6c7abe 0.0s => => sha256:11cbb99bc937a7cf5735857d418f98a9287b627f2caa01925f9ec677ad6c7abe 1.06kB / 1.06kB 0.0s => => sha256:a4d8138d0f6b5a441aaa533faf5fe0c3996a6ca42643c46f4402c7e8bda53742 45.34MB / 45.34MB 51.3s => => sha256:dbdc36973392a980d56b8fab63383ae44582f6502001d8bbdd543aa3bf1d746e 10.79MB / 10.79MB 18.0s => => sha256:1e32bd4ecaf8ff682367094f70dbc38158503280ec996e41fb904a32fb00fbe5 1.58kB / 1.58kB 0.0s => => sha256:0fc98012d4a8fd00e8fb9eeb1c10d9e7944db138a50879c5def9dcfd5b0ad61a 8.50kB / 8.50kB 0.0s => => sha256:f59d6d019dd5b8398eb8d794e3fafe31f9411cc99a71dabfa587bf732b4a7385 4.34MB / 4.34MB 19.4s => => sha256:5fee445fce940c37221a2a1de8588cda557eb4cbc3f7583931e384b137762539 5.12MB / 5.12MB 24.3s => => sha256:d270185e6f4dcd4cf5d00aec5a2b5f3888c1757ca73728f988198afc8303e3cc 223B / 223B 19.9s => => sha256:0ca0b0a3fc43fc2508d780f16173002056aabffcbeed244b5128f0653ff5756a 40.11MB / 40.11MB 74.7s => => extracting sha256:a4d8138d0f6b5a441aaa533faf5fe0c3996a6ca42643c46f4402c7e8bda53742 6.2s => => extracting sha256:dbdc36973392a980d56b8fab63383ae44582f6502001d8bbdd543aa3bf1d746e 1.0s => => extracting sha256:f59d6d019dd5b8398eb8d794e3fafe31f9411cc99a71dabfa587bf732b4a7385 0.2s => => extracting sha256:5fee445fce940c37221a2a1de8588cda557eb4cbc3f7583931e384b137762539 0.4s => => extracting sha256:d270185e6f4dcd4cf5d00aec5a2b5f3888c1757ca73728f988198afc8303e3cc 0.0s => => extracting sha256:0ca0b0a3fc43fc2508d780f16173002056aabffcbeed244b5128f0653ff5756a 2.1s => [2/2] COPY target/SpringbootDemo-1.0-SNAPSHOT.jar /SpringbootDemo-1.0-SNAPSHOT.jar 0.5s => exporting to image 0.2s => => exporting layers 0.2s => => writing image sha256:3a0fb5897eddb5746a6c0e34975c796c73a4809c3286623a075ae251acc56e30 0.0s => => naming to docker.io/jason/springbootdemo:test 0.0s What's Next? View summary of image vulnerabilities and recommendations → docker scout quickview qianhao@qianhaodeMacBook-Pro.local:SpringbootDemo (main !*%) $ ls Dockerfile pom.xml src target qianhao@qianhaodeMacBook-Pro.local:SpringbootDemo (main !*%) $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE jason/springbootdemo test 3a0fb5897edd 45 seconds ago 267MB nginx latest 89da1fb6dcb9 13 days ago 187MB docker/welcome-to-docker latest 912b66cfd46e 7 weeks ago 13.4MB
镜像上传镜像仓库
我这里上传到自己的阿里云仓库
qianhao@qianhaodeMacBook-Pro.local:SpringbootDemo (main !*%) $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE jason/springbootdemo test 3a0fb5897edd 45 seconds ago 267MB nginx latest 89da1fb6dcb9 13 days ago 187MB docker/welcome-to-docker latest 912b66cfd46e 7 weeks ago 13.4MB qianhao@qianhaodeMacBook-Pro.local:SpringbootDemo (main !*%) $ docker login --username=--- registry.cn-hangzhou.aliyuncs.com 1 Password: Login Succeeded qianhao@qianhaodeMacBook-Pro.local:SpringbootDemo (main !*%) $ docker tag 3a0fb5897edd registry.cn-hangzhou.aliyuncs.com/jason_aliyun_repo/test:v1.0 qianhao@qianhaodeMacBook-Pro.local:SpringbootDemo (main !*%) $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry.cn-hangzhou.aliyuncs.com/jason_aliyun_repo/test v1.0 3a0fb5897edd 14 minutes ago 267MB jason/springbootdemo test 3a0fb5897edd 14 minutes ago 267MB nginx latest 89da1fb6dcb9 13 days ago 187MB docker/welcome-to-docker latest 912b66cfd46e 7 weeks ago 13.4MB qianhao@qianhaodeMacBook-Pro.local:SpringbootDemo (main !*%) $ docker push registry.cn-hangzhou.aliyuncs.com/jason_aliyun_repo/test:v1.0 The push refers to repository [registry.cn-hangzhou.aliyuncs.com/jason_aliyun_repo/test] 336f15db5802: Pushed d23b61e01ad4: Pushed 95392ba99ec2: Pushed 189f0fcc0630: Pushed 392f356944ff: Pushed 15210a41d4ee: Pushed e2a8a00a83b2: Pushed v1.0: digest: sha256:27bbd9031e2c50ec6bf6195d4f52ff4379464311a6776a187151821b6998b811 size: 1794 qianhao@qianhaodeMacBook-Pro.local:SpringbootDemo (main !*%) $
容器运行
qianhao@qianhaodeMacBook-Pro.local:qianhao $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry.cn-hangzhou.aliyuncs.com/jason_aliyun_repo/test v1.0 3a0fb5897edd 37 minutes ago 267MB jason/springbootdemo test 3a0fb5897edd 37 minutes ago 267MB nginx latest 89da1fb6dcb9 13 days ago 187MB docker/welcome-to-docker latest 912b66cfd46e 7 weeks ago 13.4MB qianhao@qianhaodeMacBook-Pro.local:qianhao $ docker run -d --name springboottest -p 80:80 registry.cn-hangzhou.aliyuncs.com/jason_aliyun_repo/test:v1.0 3702af8645515de117bfd830fbdbb8d26bde1408898ba4c20c302b3e941caa0d qianhao@qianhaodeMacBook-Pro.local:qianhao $ docker ps 127 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3702af864551 registry.cn-hangzhou.aliyuncs.com/jason_aliyun_repo/test:v1.0 "java -jar /Springbo…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp springboottest qianhao@qianhaodeMacBook-Pro.local:qianhao $
我的demo是注解脱敏,成功运行在80端口
上一篇
系统高可用部署
2023-08-20
下一篇
Docker的安装和基本使用
2023-07-30