Create project
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.9.release com.topcheer docker 0.0.1-snapshot docker demo project for spring boot 1.8 topcheer org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin com.spotify docker-maven-plugin 1.0.0 ${docker.image.prefix}/${project.artifactid} src/main/docker / ${project.build.directory} ${project.build.finalname}.jar
Startup class
@springbootapplication @controller public class dockerapplication { public static void main(string[] args) { springapplication.run(dockerapplication.class, args); } @requestmapping("/user/find") @responsebody public object finduser(){ mapmap = new hashmap<>(); map.put("name", "xdclass.net"); map.put("age","28"); return map; } }
Because harbor is not installed, The image cannot be pushed to the private image repository, so execute it manually first.
mvn install, then put the dockerfile and jar package together
[root@topcheer docker]# ll 总用量 16452 -rw-r--r-- 1 root root 168 10月 28 14:24 dockerfile -rw-r--r-- 1 root root 16842487 10月 28 14:17 docker.jar [root@topcheer docker]# cat dockerfile from java:8 volume /tmp add docker.jar app.jar run bash -c 'touch /app.jar' expose 8080 entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] [root@topcheer docker]#
Compile into an image and start
[root@topcheer docker]# docker build -f dockerfile -t boot . sending build context to docker daemon 16.85 mb step 1/6 : from java:8 ---> d23bdf5b1b1b step 2/6 : volume /tmp ---> running in 35037b5a7791 ---> e96b96457c78 removing intermediate container 35037b5a7791 step 3/6 : add docker.jar app.jar ---> 06bcfdcff437 removing intermediate container 2e2e5e559ae4 step 4/6 : run bash -c 'touch /app.jar' ---> running in 16441febc271 ---> 1779caa23f77 removing intermediate container 16441febc271 step 5/6 : expose 8080 ---> running in 14dd752ce247 ---> 505044f5cdf8 removing intermediate container 14dd752ce247 step 6/6 : entrypoint java -djava.security.egd=file:/dev/./urandom -jar /app.jar ---> running in 832e4ca95dd2 ---> 1cb7bd139478 removing intermediate container 832e4ca95dd2 successfully built 1cb7bd139478 [root@topcheer docker]# docker images repository tag image id created size boot latest 1cb7bd139478 about a minute ago 677 mb registry.cn-hangzhou.aliyuncs.com/dalianpai/topcheer zipkin 17c2bb09f482 6 days ago 154 mb docker.io/mysql latest c8ee894bd2bd 11 days ago 456 mb elasticsearch latest 874179f19603 4 weeks ago 771 mb docker.io/nacos/nacos-server latest a4229ac5cc19 4 weeks ago 710 mb springbootdemo4docker latest cd13bc7f56a0 5 weeks ago 678 mb docker.io/tomcat latest ee48881b3e82 6 weeks ago 506 mb docker.io/rabbitmq latest a00bc560660a 6 weeks ago 147 mb docker.io/centos latest 67fa590cfc1c 2 months ago 202 mb docker.io/redis latest f7302e4ab3a8 2 months ago 98.2 mb docker.io/rabbitmq 3.7.16-management 3f92e6354d11 3 months ago 177 mb docker.io/elasticsearch 6.8.0 d0b291d7093b 5 months ago 895 mb docker.io/hello-world latest fce289e99eb9 10 months ago 1.84 kb docker.io/java 8 d23bdf5b1b1b 2 years ago 643 mb [root@topcheer docker]# docker run -d -p 8080:8080 boot 882ff5209aa2f40972a914b901750a50320faea65100b33e57b9c8a41533ca0b
Test
The above is the detailed content of How to deploy springboot project with Docker. For more information, please follow other related articles on the PHP Chinese website!