springboot docker镜像到私有仓库

343 阅读1分钟

安装私有仓库nexus

1. docker开启2375端口,提供外部访问docker

# 修改docker.service
sudo vim /usr/lib/systemd/system/docker.service
# 修改ExecStart
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
#开放端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
#重启防火墙
systemctl restart firewalld.service

image.png

#加载docker守护线程 
systemctl daemon-reload 
#重启docker
systemctl restart docker 

#查看是否设置成功
curl http://localhost:2375/version

2. 创建springboot项目

用spring脚手架创建项目 start.spring.io

image.png

导入idea

image.png

controller测试

package com.plat.web.controller;

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

@RestController
@RequestMapping("/user")
public class UserController {
  @GetMapping("/info")
  public Object info() {
    return "ok";
  }
}

配置settign.xml

<server>
    <id>nexus-registry</id>       
    <username>admin</username>
    <password>admin</password>
</server>

配置pom.xml

注意修改成自己IP。端口是根据 安装私有仓库nexus

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.7.3</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.plat</groupId>
   <artifactId>web</artifactId>
   <version>8.0.0-SNAPSHOT</version>
   <name>web</name>
   <description>Demo project for Spring Boot</description>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
         <version>RELEASE</version>
         <scope>compile</scope>
      </dependency>
   </dependencies>

   <properties>
      <java.version>1.8</java.version>
      <docker.repostory>192.168.202.130:8082</docker.repostory>

      <docker.plugin.version>0.32.0</docker.plugin.version>
      <docker.host>http://192.168.202.130:2375</docker.host>
      <docker.registry>192.168.202.130:8082</docker.registry>
      <docker.namespace>library</docker.namespace>
      <docker.username>admin</docker.username>
      <docker.password>admin</docker.password>
   </properties>


   <!--指定仓库地址推送地址-->
   <distributionManagement>
      <snapshotRepository>
         <!--此名称要和.m2/settings.xml中设置的ID一致-->
         <id>nexus-registry</id>
         <url>http://192.168.202.130:8081/repository/maven-snapshots/</url>
      </snapshotRepository>

      <repository>
         <!--此名称要和.m2/settings.xml中设置的ID一致-->
         <id>nexus-registry</id>
         <url>http://192.168.202.130:8081/repository/maven-releases/</url>
      </repository>
   </distributionManagement>

   <!--指定仓库地址拉取地址-->
   <repositories>
      <repository>
         <id>web-public</id>
         <name>public</name>
         <url>http://192.168.202.130:8081/repository/maven-public/</url>
      </repository>

      <repository>
         <id>web-snapshots</id>
         <name>snapshots</name>
         <url>http://192.168.202.130:8081/repository/maven-snapshots/</url>
         <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
         </snapshots>
      </repository>
   </repositories>


   <build>
      <finalName>${project.artifactId}</finalName>
      <plugins>
        <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
           <!-- 发现io.fabric8更好用,就换了一个 -->
           <configuration>
              <image>
                 <!--配置镜像名称-->
                 <name>${docker.repostory}/boot/${project.name}:${project.version}</name>
                 <!--镜像打包完成后自动推送到镜像仓库-->
                 <publish>true</publish>
              </image>
              <docker>
                 <!--Docker远程管理地址-->
                 <host>${docker.host}</host>
                 <!--不使用TLS访问-->
                 <tlsVerify>false</tlsVerify>
                 <!--Docker推送镜像仓库配置-->
                 <publishRegistry>
                    <!--推送镜像仓库用户名-->
                    <username>${docker.username}</username>
                    <!--推送镜像仓库密码-->
                    <password>${docker.password}</password>
                    <!--推送镜像仓库地址-->
                    <url>${docker.registry}</url>
                 </publishRegistry>
              </docker>
           </configuration>
           <executions>
              <execution>
                 <id>docker-exec</id>
                 <phase>install</phase>
                 <goals>
                    <goal>build-image</goal>
                 </goals>
              </execution>
           </executions>
        </plugin>
      </plugins>
   </build>
</project>

3. push jar --> 私有仓库

#maven命令
mvn clean deploy

image.png

nexus查看jar

image.png

可以看到推送到私有仓库了

4. push docker image --> 私有仓库

#maven命令
mvn spring-boot:build-image

docker pull 地址

192.168.202.130:8082/web:4.0.0-SNAPSHOT

image.png

nexus查看docker镜像

image.png

5. docker启动jar

#拉取镜像
docker pull 192.168.202.130:8082/web:4.0.0-SNAPSHOT
#启动,springboot没有配置端口,默认8080,我8080端口占用了。对外 8050
docker run -d --name my-web  -p 8050:8080 192.168.202.130:8082/web:4.0.0-SNAPSHOT

游览器访问springboot 中的controller接口

192.168.202.128:8050/user/info

image.png

Github 代码