Docker deploys SpringBoot project

CentOS Docker installation

Modify docker.servicethe configuration and open port 2375

vi /usr/lib/systemd/system/docker.service
-H tcp://0.0.0.0:2375  -H unix:///var/run/docker.sock

1

Restart docker

# 重新加载服务的配置文件
systemctl daemon-reload
# 重启 docker
service docker restart
# 开放端口
firewall-cmd --zone=public --permanent --add-port=2375/tcp
# 重启防火墙
systemctl restart firewalld

Insert image description here

Configuration

Dockerfile

FROM hub.c.163.com/library/java:8-jdk
VOLUME /tmp
ADD target/docker_deploy-0.0.1-SNAPSHOT.war app.war
ENTRYPOINT ["java","-jar","/app.war"]
  1. The operation of the SpringBoot project relies on the Java environment, so the custom image must be built based on the Java image. The Docker image provided by NetEase is used here.
  2. SpringBoot requires a tmp directory when running. Here, the data volume is configured with a /tmp directory.
  3. Copy a new copy of the packaged .war file in the local target directory to /app.war.
  4. Configure the startup command.

pom.xml

Solve the problem that there is no /src/main/webapp/WEB-INF/web.xml file when generating a war package

<properties>
    <!-- 如果打包方式为 war 包,并且没有 /src/main/webapp/WEB-INF/web.xml 文件 -->
    <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

Add new 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.179.128:2375</dockerHost>
        <imageName>yueyazhui/${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}.war</include>
            </resource>
        </resources>
    </configuration>
</plugin>
  1. Configure in the execution node that when executing mvn package, docker:build will be executed.
  2. Configure Docker's host address, image name, and image tags in the configuration node, where dockerDirectory represents the location of the specified Dockerfile.
  3. Just configure the location and name of the war in the resource node.

Controller

@RestController
public class IndexController {
    
    

    @GetMapping
    public String index() {
    
    
        return "Docker Deploy";
    }
}

Insert image description here

Package build image

Insert image description here

run

Option One:

docker run -d --name docker_deploy_demo -p 8080:8080 yueyazhui/docker_deploy:0.0.1-SNAPSHOT

Option 2: (Recommended, run the image through IDEA)

Insert image description here

Insert image description here

Insert image description here

Insert image description here
Finish!

Excerpt: Jiangnan Yidianyu IDEA Docker deployment

Guess you like

Origin blog.csdn.net/qq_45594962/article/details/127588719