How do you deploy Spring Boot in your production environment? Throw war into Tomcat container to run? However, according to Songo, containerized deployment is the current mainstream solution.

Different from traditional single applications, microservices are more likely to have problems during deployment due to the large number of services. At this time, combined with Docker deployment, this problem can be well solved, which is also one of the most commonly used solutions at present.

There are many different ways to package A Spring Boot project into a remote Docker container. Today Songo is going to talk about how to package a Spring Boot project into a remote Docker container. Then start a Spring Boot project by running a mirror.

As for the usage of other Spring Boot and Docker, don’t worry. In the following articles, Songko will talk with you slowly.

1. Preparation

1.1 prepare Docker

I’ll use CentOS7 as an example.

First of all, you need to install Docker on CentOS7, this installation method is a lot of online, I will not say, I wrote a Docker introductory tutorial last year, you can reply Docker in the public background to obtain the tutorial download address.

Docker after the installation is successful, the first thing we need to modify the Docker configuration, open to allow remote access to the function of the Docker, open way is very simple, modify/usr/lib/systemd/system/Docker. Service file, add the following content:

- H TCP: / / 0.0.0.0:2375 - H Unix: / / / var/run/docker. The sockCopy the code

The diagram below:

After the configuration is complete, save and exit, and restart Docker:

systemctl daemon-reload    
service docker restart 
Copy the code

After Docker restarts successfully, Docker is ready to work.

1.2 prepare the IDEA

To prepare your IDEA, install a New Docker plug-in, go to File->Settings->Plugins->Browse Repositories:

Click the green Install button on the right to complete the installation. After the installation is complete, restart IDEA.

IDEA to restart after success, we in order to open the File – > Settings – > Build, Execution, Deployment – > Docker, then configure the Docker remote connection address:

Docker is successfully connected to the Docker.

After that, our preparations are OK.

2. Prepare projects

Next we will create a simple Spring Boot project (just import the spring-boot-starter-Web dependency). Once the project is created, we will create a normal HelloDockerController to test as follows:

@RestController
public class HelloDockerController {
    @GetMapping("/hello")
    public String hello(a) {
        return "hello docker!"; }}Copy the code

This is a very simple interface, needless to say.

3. The configuration Dockerfile

Next, in the root directory of the project, I create a Dockerfile as the build file for my image, as shown below:

The content of the document is as follows:

FROM hub.c.163.com/library/java:latest
VOLUME /tmp
ADD target/docker-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java"."-jar"."/app.jar"]
Copy the code

There are only four simple lines, and I’ll say them:

  1. Spring Boot projects rely on the Java environment to run, so I built my own image based on the Java image.
  2. Considering the slow downloading of the official Docker image, I used the Docker image provided by netease.
  3. Because Spring Boot requires the TMP directory, the data volume is configured with a/TMP directory.
  4. Make a new copy of the.jar file packaged in the local target directory to /app.jar.
  5. Finally, configure the startup command. Since the jar I packaged has become app.jar, the startup command also starts app.jar.

This is a simple Dockerfile that we configured.

4. Configure the Maven plug-in

Next, in the POM.xml file, add the following plug-in:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.0</version>
    <executions>
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <dockerHost>http://192.168.66.131:2375</dockerHost>
        <imageName>javaboy/${project.artifactId}</imageName>
        <imageTags>
            <imageTag>${project.version}</imageTag>
        </imageTags>
        <forceTags>true</forceTags>
        <dockerDirectory>${project.basedir}</dockerDirectory>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>
Copy the code

The configuration of this plug-in is not difficult to understand:

  1. First configure the EXECUTION node to execute the DOCker :build as well when executing the MVN package
  2. Then configure the host address of the Docker, the name of the image, and the tags of the image respectively in the Configuration. DockerDirectory indicates the location of the specified Dockerfile.
  3. Finally, configure the location and name of the JAR in the Resource node.

OK, we’re done with this and we’re done.

5. Pack and run

Next, the project is packaged. After the package is completed, the project will be automatically constructed into an image and uploaded to the Docker container. The packaging method is as follows:

The packaging process is a bit older because it also involves building the image, especially the first packaging, which requires downloading the base image, which is slower.

Part of the package log is as follows (project build process) :

After the project is successfully packaged, we can see the image we just packaged in the Docker container as follows:

5.1 Operating Mode 1

At this point, we can directly create the mirror container on Linux as a normal container, and then start, execute the following command:

docker run -d--name javaboy -p 8080:8080 Javaboy /docker:0.0.1Copy the code

After successful startup, we can access the interfaces in the container.

But this operation is obviously still a bit troublesome, combined with the Docker plug-in we installed at the beginning, this operation step can be further simplified.

5.2 Operating Mode 2

Notice that there is an option in our IDEA, docker, as follows:

Click the green start button on the left to connect the Docker container. After successful connection, we can see all containers and images in the current Docker, including the Docker image we just created, as follows:

At this point, we select the image, right click to create a container based on the image, as shown below:

Select Create container and fill in the required information about the container, Specify the container name, the image ID will be automatically filled in, Specify Specify port, and write the port mapping:

Once the configuration is complete, click the Run button below to start running. Run logs are as follows:

Note that this log is printed in the Docker window.

After the project runs successfully, enter the address of the remote server in the browser to access:

After that, our Spring Boot project was successfully published into the remote Docker container.

Was it fun? Try it!

I have uploaded this case to GitHub for your reference: github.com/lenve/javab…