使用三种方式实现从docker hub下载镜像,并推送到私有Registry
Docker client
1. docker pull hello-world
2. docker tag hello-world harbor.docker-plus.xyz/app/hello-world
3. docker login harbor.docker-plus.xyz
4. docker push harbor.docker-plus.xyz/app/hello-world
Python SDK
#!/usr/bin/python
import sys
import json
import docker
client = docker.from_env()
try:
client.ping()
except docker.errors.APIError:
print("docker connect failed")
sys.exit(1)
# PULL
for line in client.api.pull('hello-world', stream=True, decode=True):
print(json.dumps(line, indent=4))
# TAG
repository = 'harbor.docker-plus.xyz/app/hello-world'
try:
client.api.tag('hello-world', repository, 'latest', force=True)
except docker.errors.APIError:
print("tag failed")
sys.exit(1)
auth_config = {
"username": "admin",
"password": "Harbor12345"
}
# PUSH
for line in client.api.push(repository, stream=True, decode=True, auth_config=auth_config):
print(json.dumps(line, indent=4))
Remote API
# PULL
curl -XPOST http://192.168.0.19:2375/images/create?fromImage=hello-world
# TAG
curl -XPOST http://192.168.0.19:2375/images/hello-world/tag?repo=harbor.docker-plus.xyz/app/hello-world
# PUSH
XRA=`echo '{"username": "admin","password": "Harbor12345", "serveraddress": "harbor.docker-plus.xyz"}' | base64 --wrap=0`
curl -XPOST -H "X-Registry-Auth: $XRA" \
http://192.168.0.19:2375/images/harbor.docker-plus.xyz/app/hello-world/push
网友评论