SpringCloud 实战|SpringCloud 实现docker 镜像部署-提升项目的生产力

2022-09-19 11:48:14 浏览数 (1)

本地仓库配置

安装本地仓库

代码语言:javascript复制
docker run -itd -v /home/registry:/var/lib/registry -p 5000:5000 --restart=always --privileged=true --name registry registry:latest

配置私有仓库

代码语言:javascript复制
vim /etc/docker/daemon.json
{
  "insecure-registries": ["192.168.64.6:5000"]
}
systemctl daemon-reload
systemctl restart docker

http://192.168.64.6:5000/v2/_catalog

开启docker 远程连接

代码语言:javascript复制
vim /lib/systemd/system/docker.service

# 修改ExecStart为:
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H 0.0.0.0:2375

配置项目

定义变量

在父级项目的pom->properties 添加如下配置 docker.host:docker安装的主机ip docker.registry.repository:上面搭建的本地仓库主机ip 端口号 docker.maven.plugin.version:插件版本

代码语言:javascript复制
  <docker.host>http://192.168.64.2:2375</docker.host>
  <docker.registry.repository>192.168.64.2:5000</docker.registry.repository>
  <docker.maven.plugin.version>1.0.0</docker.maven.plugin.version>

添加编译插件

在父级项目的pom->build->pluginManagement->plugins添加如下配置

代码语言:javascript复制
     <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>${docker.maven.plugin.version}</version>
                    <executions>
                        <execution>
                            <id>build-image</id>
                            <phase>package</phase>
                            <goals>
                                <goal>build</goal>
                                <goal>push</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <imageName>${docker.registry.repository}/${project.artifactId}:${project.version}</imageName>
                        <dockerHost>${docker.host}</dockerHost>
                        <baseImage>java:8</baseImage>
                        <entryPoint>["java", "-jar","-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]
                        </entryPoint>
                        <resources>
                            <resource>
                                <targetPath>/</targetPath>
                                <directory>${project.build.directory}</directory>
                                <include>${project.build.finalName}.jar</include>
                            </resource>
                        </resources>
                    </configuration>
                </plugin>

完整配置

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ams</groupId>
    <artifactId>ams-cloud</artifactId>
    <version>1.0.0</version>
    <modules>
        <module>ams-auth</module>
        <module>ams-gateway</module>
        <module>ams-common</module>
        <module>ams-admin</module>
        <module>ams-demo</module>
    </modules>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <spring-boot.version>2.5.4</spring-boot.version>
        <spring-cloud.version>2020.0.3</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
        <ams.version>1.0.0</ams.version>
        <hutool.version>5.5.8</hutool.version>
        <mysql.version>8.0.19</mysql.version>
        <druid.version>1.2.4</druid.version>
        <docker.host>http://192.168.64.2:2375</docker.host>
        <docker.registry.repository>192.168.64.2:5000</docker.registry.repository>
        <docker.maven.plugin.version>1.0.0</docker.maven.plugin.version>
        <dockerfile.maven.version>1.3.7</dockerfile.maven.version>
        <mybatis-plus.version>3.4.3</mybatis-plus.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool.version}</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <!--Spring Cloud 相关依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--Spring Cloud & Alibaba 相关依赖-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <pluginManagement>
            <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>${docker.maven.plugin.version}</version>
                    <executions>
                        <execution>
                            <id>build-image</id>
                            <phase>package</phase>
                            <goals>
                                <goal>build</goal>
                                <goal>push</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <imageName>${docker.registry.repository}/${project.artifactId}:${project.version}</imageName>
                        <dockerHost>${docker.host}</dockerHost>
                        <baseImage>java:8</baseImage>
                        <entryPoint>["java", "-jar","-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]
                        </entryPoint>
                        <resources>
                            <resource>
                                <targetPath>/</targetPath>
                                <directory>${project.build.directory}</directory>
                                <include>${project.build.finalName}.jar</include>
                            </resource>
                        </resources>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

添加打包方式到每个子模块中

将以下加入到每个子模块的pom中

代码语言:javascript复制
    <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>
            </plugin>
        </plugins>
    </build>

打包验证

  • 1.执行package
  • 2.完成推送
  • 3.打开浏览器访问 ttp://192.168.64.6:5000/v2/_catalog
  • 4.如果返回了我们项目的镜像,则说明docker打包成功
  • 5.在docker 主机上 执行命令 docker run 192.168.64.2:5000/ams-gateway(以网关作为验证)
  • 6.如果执行成功,说明镜像有效

docker 常见错误解决

错误1

rquest to {}->unix://localhost:80: No such file or directory 解决方案:1.确认imageName 是否包含仓库地址(需要包含)

关注公众号领取2021最新面试题一套和项目源码

0 人点赞