This article introduces how to build a docker image for the springboot program. Docker is an open source application container engine based on Go language and Apache 2.0 protocol. Docker allows developers to package their applications and dependencies into a lightweight, portable container, and then publish them to any popular Linux machine. It also enables virtualization. Containers use sandbox mechanism completely, and there is no interface between them (like iPhone apps). More importantly, the performance overhead of containers is very low. Spring Cloud large enterprise distributed microservice Cloud Architecture source code Please add 1791743380
Preparation
Environmental Science:
linux environment or mac, do not use windows
jdk 8
maven 3.0
docker
I don't know anything about docker.
Create a springboot project
Introduce the starting dependency of web and create a controller:
@SpringBootApplication @RestController public class SpringbootWithDockerApplication { @RequestMapping("/") public String home() { return "Hello Docker World"; } public static void main(String[] args) { SpringApplication.run(SpringbootWithDockerApplication.class, args); } }
Container the springboot project
Docker has a simple dockerFile file as the layer for the specified image. Let's create a dockerFile file first:
src/main/docker/Dockerfile:
FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp ADD springboot-with-docker-0.0.1-SNAPSHOT.jar app.jar RUN sh -c 'touch /app.jar' ENV JAVA_OPTS="" ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
We build docker image through maven.
Add the plug-in built by docker image to the pom directory of maven
<properties> <docker.image.prefix>springio</docker.image.prefix> </properties> <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.4.11</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>
With the maven command:
Step 1: mvn clean
Step 2: MVN package docker: built, as follows:
Step 2/6 : VOLUME /tmp —> Running in a98be3878053 —> 8286e98b54c5 Removing intermediate container a98be3878053 Step 3/6 : ADD springboot-with-docker-0.0.1-SNAPSHOT.jar app.jar —> c6ce13e50bbd Removing intermediate container a303a3058869 Step 4/6 : RUN sh -c 'touch /app.jar' —> Running in cf231afe700e —> 9a0ec8936c00 Removing intermediate container cf231afe700e Step 5/6 : ENV JAVA_OPTS "" —> Running in e192597fc881 —> 2cb0d73bbdb0 Removing intermediate container e192597fc881 Step 6/6 : ENTRYPOINT sh -c java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar —> Running in ab85f53fcdd8 —> 60fdb5c61692 Removing intermediate container ab85f53fcdd8 Successfully built 60fdb5c61692 [INFO] Built forezp/springboot-with-docker [INFO] ------------ [INFO] BUILD SUCCESS [INFO] ------------ [INFO] Total time: 01:45 min [INFO] Finished at: 2017-04-19T05:37:44-07:00 [INFO] Final Memory: 19M/48M [INFO] ------------