Spring Boot Part 24: Springboot integrates docker

This article describes how to build a docker image for the springboot program. Docker is an open source application container engine, based on the Go language and open sourced under the Apache2.0 protocol. Docker allows developers to package their applications and dependencies into a lightweight, portable container, which can then be distributed to any popular Linux machine, and can also be virtualized. The container completely uses the sandbox mechanism, and there will not be any interface between each other (similar to the iPhone app), and more importantly, the performance overhead of the container is extremely low.

Ready to work

environment:

  • Linux environment or mac, do not use windows
  • jdk 8
  • maven 3.0
  • docker

If you don't know anything about docker, see the docker tutorial.

Create a springboot project

Introduce the starting dependencies of the web 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);
    }
}

Containerize the springboot project

Docker has a simple dockerfile as a layer for specifying an image. Let's first create a dockerFile file:

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 images through maven.

In the pom directory of maven, add the plugin built by the docker image

<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>

Note: ${docker.image.prefix} is your username in the official docker repository. If you don't need to upload an image, just fill it in.

Via the maven command:

Step 1: mvn clean

The second step: mvn package docker:bulid , as follows:

The image build was successful. Check out the mirror:

docker images

show:

forezp/springboot-with-docker latest 60fdb5c61692 About a minute ago 195 MB

Boot image:

$ docker run -p 8080:8080 -t forezp/springboot-with-docker

Open a browser to visit localhost:8080; the browser displays: Hello Docker World.
It means that the springboot project of docker has been deployed.

Stop mirroring:

docker stop 60fdb5c61692

Delete mirror:

docker rm 60fdb5c61692

References

https://docs.docker.com/engine/reference/builder/))

http://www.runoob.com/docker/docker-tutorial.html

Source code download

https://github.com/forezp/SpringBootLearning

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324602682&siteId=291194637