[Dockerfile in root directory] IDEA integrates docker. When packaging, the image is automatically built and pushed to the remote docker server

Posted by stuffradio on Tue, 09 Nov 2021 02:39:50 +0100

Write in front

IDEA integrates docker. Port 2375 of docker needs to be opened

Docker opens 2375 port to realize remote access: https://blog.csdn.net/single_0910/article/details/121201709

environment

Linux Environmental Science: Ubuntu18.04 LTS
IDEA Version: 2019.3.5

Install Docker plug-in

slightly

IDEA 2019 Version and above, installed by default Docker plug-in unit

Configure Docker environment

You can see that docker little whale has appeared in the service module.

Configuring the docker Maven plug-in

The Docker environment has been configured. To deploy the project in the IDEA, you also need to install the Maven plug-in.

The docker Maven plugin can automatically generate images and push them to the warehouse through simple configuration in Maven project.

Configuring the docker Maven plug-in

<build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <!--Bind a plug-in to a phase implement-->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <!--Bind plug-ins to package Stage, in other words, as long as it is executed mvn package ,It will be executed automatically mvn docker:build-->
                        <!--Equivalent to command: mvn clean package docker:build -Dmaven.test.skip=true	-->
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--Specifies the name of the generated image-->
                    <imageName>docker-${project.artifactId}</imageName>
                    <!--Specify label-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!-- appoint Dockerfile route ${project.basedir}:pom.xml Directory where-->
                    <dockerDirectory>${project.basedir}</dockerDirectory>
                    <!--Specify remote docker Environment address-->
                    <dockerHost>http://192.168.211.129:2375</dockerHost>
                </configuration>
            </plugin>
        </plugins>
    </build>

After static Maven loading for a moment, you can see that the docker plug-in has been installed.

When running mvn:package, docker:build will be executed automatically to generate the corresponding image

Right click on the generated image to select Create container.

After the container is built, it can run.

Simple example (packaged online)

First create a simple spring boot project

The contents are as follows:

UserController.java

package com.layman.water.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ningyang.han
 * @description: Test docker one click deployment
 * @date 2021/11/8
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/test")
    public String test(){
        return "hello,docker!";
    }
}

Test it and you can access it normally

Take this simple project as an example to implement the one click deployment of docker.

pom.xml

<build>
        <plugins>
        	<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <!--Bind a plug-in to a phase implement-->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <!--Bind plug-ins to package Stage, in other words, as long as it is executed mvn package ,It will be executed automatically mvn docker:build-->
                        <!--Equivalent to command: mvn clean package docker:build -Dmaven.test.skip=true	-->
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--Specifies the name of the generated image-->
                    <imageName>docker-${project.artifactId}</imageName>
                    <!--Specify label-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!-- appoint Dockerfile route ${project.basedir}:pom.xml Directory where-->
                    <dockerDirectory>${project.basedir}</dockerDirectory>
                    <!--Specify remote docker Environment address-->
                    <dockerHost>http://192.168.211.129:2375</dockerHost>
                </configuration>
            </plugin>
        </plugins>
    </build>

What does the plug-in above mean

First, I bind the plug-in with the mvn: package command. In other words, as long as mvn: package is executed, docker: build will be executed automatically

When docker: build is executed, it will search for the build image command (the command can be written in the plug-in or separately in the Dockerfile)

I wrote it in a separate Dockerfile, which will significantly reduce both readability and later maintenance cost.

Therefore, I assigned a dockerDirectory attribute to the plug-in and told it to find the corresponding Dockerfile in the root directory.

If it is not found, an error will be reported. If it is found, it will automatically execute the command in Dockerfile to build a new image. The name and version number of the image are specified by imageName and imageTag.

After the image is built, I also specified the dockerHost attribute, which means that after the image is built, please push it to the specified docker remote environment.

Dockerfile

# base image 
FROM openjdk:8-jdk-alpine
# author
MAINTAINER layman<15373883283@qq.com>
# Mount directory (when container starts)
VOLUME /usr/local/water
# Create directory (this command is only executed when the container is running)
RUN mkdir -p /usr/local/water
# Specify path
WORKDIR /usr/local/water
# Copy the jar package under the target directory of the current path to the image and rename it to water.jar
COPY ./target/water-1.1.jar /usr/local/water/water.jar
ENTRYPOINT ["java","-jar","/usr/local/water/water.jar"]

As I said before, Dockerfile must be in the same directory as pom.xml. In other words, Dockerfile must be in the root directory

During the docker build image, there is a docker context environment, which can only read all files in the same directory, not to the parent directory.

If Dockerfile and target are not in the same level directory, it cannot read the jar package generated by mvn:package in target

Then, it cannot execute the command copy. / target / water-1.1.jar / usr / local / water / water.jar.

results of enforcement

Execute the mvn:package command.


You can see that the image has been generated successfully.


Run successfully

Topics: Docker server intellij-idea