One-click deployment to a remote Docker container Spring Boot

1. Install docker (I am here is centos7 environment)

  Docker first need to modify the configuration, open to allow remote access Docker's functions, open the way is very simple to modify  the file, add the following: /usr/lib/systemd/system/docker.service

  

 

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

  systemctl daemon-reload
  service docker restart

2. Create springBoot project (I use here is IDEA)

  

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

3. Next, in the root directory of the project, I created a Dockerfile, as my image build files, the specific location as shown below:

 

 Document reads as follows:

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

File Comments:

  1. Run Spring Boot project dependencies Java environment, so my own mirror to build Java-based mirroring.

  2. Considering the slow download Docker official mirror, I used here Docker image provided by NetEase.

  3. Spring Boot tmp directory because of the need to run time, a configuration where the data volume the / tmp directory out.

  4. Local target directory will be packaged in good .jar files are copied to a new /app.jar.

  5. The final step is to configure it to start command, because I have become app.jar packaged jar, so start command also starting app.jar.

4. Next at pom.xml file, add the following plug-ins:

<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://远程IP: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>

Plug Notes:

  1. First node configuration in execution when executing mvn package, by the way also perform at docker: build

  2. Are then arranged in the configuration of the Docker host address, name of the mirror, the mirror Tags, wherein dockerDirectory represents the position of the specified Dockerfile.

  3. The last resource re-allocation jar node location and name.

5. Next, the packaged items, the package is completed, the items are automatically constructed as a mirror image, and upload Docker container packaging follows:

 

 After the success of the project package, we can see that we just packed into a mirror in Docker containers, as follows:

 

 At this point, we can create like an ordinary container, like create Linux directly on the image of the container, and then start, execute the following command:

 

 After the project is running successfully in your browser and enter the address of the remote server, you can visit:

 

Guess you like

Origin www.cnblogs.com/aizj/p/11433454.html