How to Build pypi Private Warehouse with Docker
- 2021-09-16 08:36:15
- OfStack
STEP 1 Build
1. Prepare the htpasswd. txt file
This file contains the username and password verified when uploading the package to the warehouse
pip install htpasswd
htpasswd -sc htpasswd.txt < username >
2. Start the container
docker run --name pypiserver --restart=always -v /data/pypi/packages:/data/packages -v /root/htpasswd.txt:/data/htpasswd.txt -p 8080:8080 -d pypiserver/pypiserver -P htpasswd.txt packages
# data directory and htpasswd. txt file should be established in advance on the host machine
3. Set up nginx reverse proxy
cat /usr/local/nginx/conf/exten/pypi.conf
upstream pypi {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name pypi.local.me;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://pypi;
}
}
STEP 2 Use
1. Establish test items
# Establish a project directory
mkdir -p linode_example/linode_example
# Establish setup.py
cat linode_example/setup.py
from setuptools import setup
setup(
name='linode_example',
packages=['linode_example'], # Directory after uploading to warehouse, such as http://pypi.local.me/linode_example
description='Hello world enterprise edition',
version='0.1', # Version number
url='http://github.com/example/linode_example',
author='Linode',
keywords=['pip','linode','example']
)
# The content of this file is descriptive and can be set according to the situation of your own package
# Establish __init__.py Main program
cat linode_example/linode_example/__init__.py
def hello_word():
print("hello world")
# Package and upload
python3.7 setup.py sdist # Packaging, after execution, will be in dist There is a tar Bag
twine upload --repository-url http://pypi.local.me dist/* # User name and password are required when uploading: admin/admin123
2. Use packages uploaded to the warehouse
pip install -i http://pypi.local.me --trusted-host pypi.local.me linode_example
Packing considerations:
1. The directory structure of all projects that need to be packaged in git warehouse must be 1, which is convenient for jenkinsfile automatic integration;
2. The setup. py files of all projects to be packaged must be located in the root directory of the project;
3. python uses version 1, and the version of each project needs to be fixed to facilitate iteration.