使用Nexus Repository Manager搭建私有自建 pip 源

为什么要自建 pip 源?
在企业或内网环境中,常见需求:

❌ 无法访问公网 PyPI(如安全隔离网络)
⏳ 安装包太慢(pip 下载慢)
🔐 需要统一管理第三方包或发布内部私有库
📦 缓存常用包,避免重复下载
👉 解决方案:搭建私有 pip 源

而 Nexus 是目前最流行的通用仓库管理工具,支持:

✅ Python (PyPI)
✅ npm
✅ Maven
✅ Docker
✅ Helm 等

官网下载:https://www.sonatype.com/download-oss-sonatype

  1. 使用Docker Nexus搭建私有pip源完整指南## 标题
  2. 使用Docker部署Nexus
# 创建数据卷(持久化存储)
docker volume create nexus-data

# 启动Nexus容器
docker run -d \
  --name nexus3 \
  --restart=always \
  -p 38081:8081 \
  -v nexus-data:/nexus-data \
  sonatype/nexus3

1.2 使用docker-compose部署(推荐)

version: '3'
services:
  nexus:
    image: sonatype/nexus3:latest
    container_name: nexus3
    restart: always
    ports:
      - "38081:8081"
    volumes:
      - nexus-data:/nexus-data
    environment:
      - INSTALL4J_ADD_VM_PARAMS=-Xms2g -Xmx2g -XX:MaxDirectMemorySize=3g

volumes:
  nexus-data:

启动服务:docker-compose up -d

查看初始密码

# 查看初始密码
docker exec nexus3 cat /nexus-data/admin.password

# 或者
docker-compose exec nexus cat /nexus-data/admin.password
  1. 配置Nexus PyPI仓库
    2.1 登录Nexus
    访问:http://localhost:38081
    使用admin和初始密码登录
    按提示修改密码

2.2 创建PyPI仓库
创建代理仓库(缓存公共包)
点击 “设置” → “Repository” → “Repositories”
点击 “Create repository” → 选择 “pypi (proxy)”
配置:

Name: pypi-proxy
Remote storage: https://pypi.org/

创建托管仓库(存储私有包)
点击 “Create repository” → 选择 “pypi (hosted)”
配置:

Name: pypi-hosted
Deployment policy: Allow redeploy

创建仓库组(统一入口)
点击 “Create repository” → 选择 “pypi (group)”
配置:

Name: pypi-all
Member repositories: 
  - pypi-hosted (优先级高)
  - pypi-proxy
  1. 配置pip客户端## 标题

3.1 临时使用

# 下载包示例
pip install requests -i http://localhost:8081/repository/pypi-all/simple --trusted-host localhost

3.2 永久配置

# 创建配置目录
mkdir -p ~/.pip

# 创建配置文件
cat > ~/.pip/pip.conf << EOF
[global]
index-url = http://localhost:8081/repository/pypi-all/simple
trusted-host = localhost
EOF

3.3 验证配置

# 查看当前配置
pip config list

# 测试下载
pip install requests -v
  1. 上传私有包到Nexus
    4.1 创建上传凭证
    创建 ~/.pypirc 文件:
[distutils]
index-servers = nexus

[nexus]
repository: http://localhost:8081/repository/pypi-hosted/
username: admin
password: your-admin-password

4.2 准备测试包
创建测试项目结构:

mkdir mypackage && cd mypackage

创建 setup.py:

from setuptools import setup, find_packages

setup(
    name="mypackage",
    version="0.1.0",
    author="Your Name",
    author_email="your.email@example.com",
    description="A test package",
    packages=find_packages(),
    python_requires=">=3.6",
)

创建包文件 mypackage/init.py:

def hello():
    return "Hello from mypackage!"

4.3 构建并上传包

# 安装构建工具
pip install build twine
# 构建包
python -m build
# 上传到Nexus
python -m twine upload --repository nexus dist/*

# 或者直接指定URL上传:
twine upload --repository-url http://localhost:8081/repository/pypi-hosted/ \
  -u admin -p your-admin-password \
  dist/*
  1. 完整使用示例
    5.1 下载公共包
# 从Nexus下载(会自动缓存)
pip install numpy pandas requests

# 查看已安装包
pip list

5.2 下载私有包

# 安装之前上传的私有包
pip install mypackage

# 使用私有包
python -c "from mypackage import hello; print(hello())"

5.3 更新私有包

# 修改版本号 setup.py
setup(
    name="mypackage",
    version="0.2.0",  # 更新版本
    ...
)
# 重新构建和上传
python -m build
twine upload --repository nexus dist/*

# 更新安装
pip install --upgrade mypackage
  1. 高级配置
    6.1 配置HTTPS(生产环境推荐)
    使用nginx反向代理:
# docker-compose.yml 添加nginx
version: '3'
services:
  nexus:
    image: sonatype/nexus3:latest
    container_name: nexus3
    restart: always
    expose:
      - "8081"
    volumes:
      - nexus-data:/nexus-data
    environment:
      - INSTALL4J_ADD_VM_PARAMS=-Xms2g -Xmx2g

  nginx:
    image: nginx:alpine
    container_name: nexus-nginx
    restart: always
    ports:
      - "443:443"
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./ssl:/etc/nginx/ssl:ro
    depends_on:
      - nexus

volumes:
  nexus-data:

6.2 批量迁移包
从官方源迁移常用包到私有源:

# migrate_packages.py
import subprocess
import sys

packages = [
    "requests",
    "numpy",
    "pandas",
    "flask",
    "django",
    # 添加更多需要的包
]

for package in packages:
    print(f"Downloading {package}...")
    subprocess.check_call([
        sys.executable, "-m", "pip", "download", 
        "--no-deps", package
    ])
    
print("All packages downloaded to current directory")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值