Create project
pom.xml
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.1.9.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.topcheer</groupid> <artifactid>docker</artifactid> <version>0.0.1-snapshot</version> <name>docker</name> <description>demo project for spring boot</description> <properties> <java.version>1.8</java.version> <docker.image.prefix>topcheer</docker.image.prefix> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> <plugin> <groupid>com.spotify</groupid> <artifactid>docker-maven-plugin</artifactid> <version>1.0.0</version> <configuration> <imagename>${docker.image.prefix}/${project.artifactid}</imagename> <dockerdirectory>src/main/docker</dockerdirectory> <resources> <resource> <targetpath>/</targetpath> <directory>${project.build.directory}</directory> <include>${project.build.finalname}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> </project>
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(){ map<string, string > map = 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!