docker使用nginx搭建本地文件系统仓库

78 阅读3分钟

Maven私服方案参考

docker 搭建一个Maven 私服可使用方案

  1. 使用sonatype/nexus3
  2. 使用Apache Archiva
  3. 使用本地文件系统仓库
  4. 使用使用 MinIO 或 S3 存储
  5. 使用使用 JFrog Artifactory OSS

需求说明

在甲方内网环境无网络离线开发,有部分模块的需要deploy到仓库给其他模块使用,由于开发工作只是暂时的,不是长久驻场开发,且是两人合作开发,所以这里采用了第三种方案

搭建-本地文件系统仓库

使用 本地文件系统仓库 是一种非常简单且轻量级的 Maven 私服解决方案。它不需要额外的服务器软件(如 Nexus 或 Archiva),而是直接将 Maven 仓库存储在本地文件系统中,并通过 HTTP 服务器(如 Nginx 或 Python 的 HTTP 模块)提供访问。

我有个 nginx:1.27.1-alpine Docker镜像,制作一个新镜像来实现本地文件系统仓库

思路:

  1. 将这个容器启动 然后进入到容器里
  2. 修改nginx配置文件,将/data/tidu/warehouse文件夹代理
  3. 开启浏览器地址访问文件夹设置
  4. 然后基于这个容器,保存成一个新的镜像 local_nginx_maven:1.0.0

启动 Nginx 容器

首先,启动一个基于 nginx:1.27.1-alpine 的容器:

 docker run -d --name nginx_temp -p 8080:80 nginx:1.27.1-alpine

进入容器并修改配置

 docker exec -it nginx_temp /bin/sh

修改 Nginx 配置文件

编辑 Nginx 的默认配置文件

 vi /etc/nginx/conf.d/default.conf

将文件内容替换

 server {
     listen       80;
     listen  [::]:80;
     server_name  localhost;
 ​
     #access_log  /var/log/nginx/host.access.log  main;
 ​
     add_header Access-Control-Allow-Origin *;
     add_header Access-Control-Allow-Headers content-type,X-Requested-With,x-ui-request,lang,API-TOKEN,X-Auth-Key,X-Auth-ActionId,X-Auth-Signature,X-Auth-Timestamp;
     add_header Access-Control-Allow-Methods GET,POST,PUT,OPTIONS;
     add_header Access-Control-Expose-Headers *;
     add_header X-Frame-Options SAMEORIGIN ;
     
     # 服务器限制文件上传的最大大小 => 不设置的话默认 100mb
     client_max_body_size 900m;
 ​
     location / {
         root   /data/tidu/warehouse;
         autoindex on;  # 开启目录列表功能
 ​
         # 允许所有 HTTP 方法
         # satisfy any;
         # allow all;
         # deny all;
 ​
         # 允许 PUT 方法
         dav_methods PUT;
         create_full_put_path on; # 允许创建目录
         dav_access user:rw group:rw all:r; #设置权限
     }
 ​
     error_page   500 502 503 504  /50x.html;
     location = /50x.html {
         root   /usr/share/nginx/html;
     }
 }

创建仓库目录

在容器中创建 /data/tidu/warehouse 目录

 mkdir -p /data/tidu/warehouse
 # 设置文件夹权限
 chmod -R 777 /data/tidu/warehouse

退出并保存为新镜像

退出容器

 exit

将修改后的容器保存为新镜像

 docker commit nginx_temp local_nginx_maven:1.0.0

这将基于当前容器的状态创建一个新镜像,命名为 local_nginx_maven:1.0.0

停止并删除临时容器

 docker stop nginx_temp
 docker rm nginx_temp

使用新镜像启动容器

使用新镜像启动容器,并将本地目录挂载到 /data/tidu/warehouse

 mkdir -p /home/docker_volume/maven/repo
 ​
 docker run -d \
   --name local_nginx_maven \
   -p 8088:80 \
   -v /home/docker_volume/maven/repo:/data/tidu/warehouse \
   local_nginx_maven:1.0.0
  • -v /path/to/your/local/repo:/data/tidu/warehouse:将本地的 Maven 仓库目录挂载到容器内的 /data/tidu/warehouse
  • -p 8088:80:将容器的 80 端口映射到主机的 8088 端口。

访问仓库

在浏览器中访问 http://localhost:8088,你将看到挂载的本地 Maven 仓库目录内容。

配置 Maven 使用该仓库

在其他开发者的 settings.xml 中配置你的仓库地址

参考配置文件

 <?xml version="1.0" encoding="UTF-8"?>
 <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
 ​
 <pluginGroups>
      <!-- 插件组配置 -->
 </pluginGroups>
 ​
 <proxies>
      <!-- 代理配置 -->
 </proxies>
 ​
 <servers>
      <!-- 服务器配置 -->
 </servers>
 ​
 <mirrors>
      <!-- 云仓库的 mirror 配置 -->
      <mirror>
          <id>local-nginx-repo</id>
         <mirrorOf>*</mirrorOf>  
         <name>local-nginx-repo</name>
         <url>http://172.16.1.80:8088</url>
     </mirror> 
 </mirrors>
 ​
 <profiles>
      <!-- 添加本地仓库配置 -->
      <profile>
          <id>local-nginx-repo</id>
          <repositories>
              <repository>
                  <id>local-nginx-repo</id>
                  <url>http://172.16.1.80:8088</url>
              </repository>
          </repositories>
          <pluginRepositories>
              <pluginRepository>
                  <id>local-nginx-repo</id>
                  <url>http://172.16.1.80:8088</url>
              </pluginRepository>
          </pluginRepositories>
      </profile>
 </profiles>
 ​
 <porject>
     <distributionManagement>
          <repositories>
              <repository>
                  <id>local-nginx-repo</id>
                  <url>http://172.16.1.80:8088</url>
              </repository>
          </repositories>
     </distributionManagement>
 </porject>
 ​
 <activeProfiles>
      <!-- 激活本地仓库配置 -->
      <activeProfile>local-nginx-repo</activeProfile>
 </activeProfiles>
 </settings>

部署和下载依赖

spring代码的pom.xml配置本地私地址(示例如下)

 <?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">
     ......
     
     <distributionManagement>
         <!--Maven本地私服-->
          <repository> 
              <id>local-nginx-releases</id> 
              <name>Local Nginx Repository</name> 
              <url>http://172.16.1.80:8088/</url> 
          </repository> 
          <snapshotRepository> 
              <id>local-nginx-snapshots</id> 
              <name>Local Nginx Repository</name> 
              <url>http://172.16.1.80:8088/</url> 
          </snapshotRepository> 
     </distributionManagement>
 </project>
  • 部署构件:使用 mvn deploy 将构件部署到本地仓库。
  • 下载依赖:Maven 会自动从你的 Nginx 仓库下载依赖。