E-commerce social platform source code please add penguin beg: three five six two forty-seven fifty-nine. This article explains how to build a Docker image for a SpringBoot program. Docker is an open source application container engine, which is based on the Go language and complies with the Apache2.0 protocol. Docker allows developers to package their applications and dependencies into a lightweight, portable container that can then be distributed to any popular Linux machine, as well as virtualization. Containers are completely sandboxed, have no interfaces with each other (like iPhone apps), and most importantly, have very low performance overhead.

The preparatory work

Environment:

  • Linux or MAC, not Windows
  • jdk 8
  • Maven 3.0
  • docker

If you don’t know anything about Docker, read docker tutorials.

Create a Springboot project

Import the Web startup dependency and create a Controler:

@SpringBootApplication
@RestController
public class SpringbootWithDockerApplication {
 
    @RequestMapping("/")
    public String home() {
        return "Hello Docker World"; } public static void main(String[] args) { SpringApplication.run(SpringbootWithDockerApplication.class, args); }}Copy the code

Containerize the Springboot project

Docker has a simple dockerfile file as the layer to specify the image. Let’s create a dockerFile:

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" ]Copy the code

We use Maven to build the Docker image.

In Maven’s POM directory, add the plugin built by the Docker image

<properties> <docker.image.prefix>springio</docker.image.prefix> </properties> <build> <plugins> <plugin> < the groupId > com. The company < / 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>Copy the code

Using the maven command:

Step 1: MVN Clean

Step 2: MVN package docker:bulid as follows:

Step 2/6: VOLUME/TMP - > RunninginA98be3878053 -- > 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' -- > RunninginCf231afe700e - > 9A0EC8936C00 Removing Intermediate Container CF231AFE700E Step 5/6: ENV JAVA_OPTS - > Runningin e192597fc881 - > 2CB0D73BBDB0 Removing Intermediate Container E192597fc881
Step 6/6 : ENTRYPOINT sh -c java $JAVA_OPTSEgd =file:/dev/./urandom -jar /app.jar -- > RunninginAb85f53fcdd8 -- > 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: 19 m / 48 m [INFO] -- -- -- -- -- -- -- -- -- -- -- --Copy the code

E-commerce social platform source code please add penguin beg: three five six two forty-seven fifty-nine