如何通过docker hub api创建docker私有存储库?

时间:2015-04-24 09:56:09

标签: docker dockerhub docker-registry

我可以通过docker hub api创建公共存储库。我怎样才能私有存储库?

url='http://index.docker.io/v1/repositories/(username)/(new_repo_name)/'
header = {'content-type': 'application/json','Accept': 'application/json','X-Docker-Token': 'true'}
data = [{"id": "1020903f808f"}]
auth=HTTPBasicAuth(username,password)
requests.put(url,headers=header, auth=auth,data=data)

我的形象是:

(username)/(new_repo_name)       latest             1020903f808f 

2 个答案:

答案 0 :(得分:1)

这在任何地方都没有记录,但通过检查浏览器中的请求,我设法让下面的代码工作。它需要一个JWT令牌。快速而肮脏的方法是使用Chrome登录并从调试视图中复制粘贴(ctrl + shift + i)。

class DockerHub(object):
def __init__(self, url=None, version='v2', headers=None, jwt_token=None):
    self.version = version
    self.url = '{0}/{1}'.format(url or 'https://hub.docker.com', self.version)
    self.headers = headers or {}
    if jwt_token:
        self.headers['Authorization'] = 'JWT ' + jwt_token

def create_private_docker_hub_repo(self, reponame, orgname, jwt_token, summary=None, description=None):
    payload = {
        'description': summary or '',
        'full_description': description or '',
        'is_private': 'true',
        'name': reponame,
        'namespace': orgname
    }
    resp = requests.post(
        self.url + '/repositories/',
        data=payload,
        headers=self.headers,
    )
    return resp.json()

def set_group_permission_for_repo(self, repo, orgname, groupname, permission='read'):
    group_id = {it['name']: it['id'] for it in self.get_org_groups(orgname)}[groupname]
    if not permission in ('write', 'read'):
        raise Exception('permission must be write or read')
    resp = requests.post(
        'https://hub.docker.com/v2/repositories/{org}/{repo}/groups/'.format(
            org=orgname,
            repo=repo
        ),
        data={'group_id': group_id, 'permission': permission},
        headers=self.headers
    )
    return resp.json()

def get_org_groups(self, orgname):
    resp = requests.get(
        'https://hub.docker.com/v2/orgs/{org}/groups/?page_size=100'.format(org=orgname),
        headers=self.headers,
    )
    return resp.json()['results']

答案 1 :(得分:0)

使用curl(在shell脚本中有用):

TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${USER}'", "password": "'${PASS}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)

curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/" \
 --data 'description=test' \
 --data 'full_description=full-description' \
 --data 'is_private=false' \
 --data 'name=test' \
 --data "namespace=${USER}"