I'm working on a project with Python(3.6) in which I need to implement Docker Hub API to create a repository on user's docker hub account and push his docker image.
Real Scenario: User will provide his source code along with Dockerfile, his username and apikey of docker cloud, then I have to create a repository on his account and build a Docker image of his source code and push to his docker hub account.
Here's what I have tried:
tempdir = tempfile.mkdtemp()
saved_unmask = os.umask(0o077)
temp_dir_path = os.path.join(tempdir)
archive_path = os.path.join(settings.MEDIA_ROOT, 'archives/', deployment.sourceFile.name)
print(archive_path)
# Extract uploaded archive inside temporary dir
patoolib.extract_archive(archive_path, outdir=temp_dir_path)
# Try to retrieve path of dockerfile directory
docker_glob = os.path.join(temp_dir_path, "*", "Dockerfile")
print(docker_glob)
docker_file = glob.glob(docker_glob)[0]
docker_folder = os.path.dirname(docker_file)
print(docker_folder)
docker_client = docker.from_env()
print("Start Building your docker image...")
docker_client.login(username='USERNAME', password='PASSWORD')
docker_client.images.build(path=docker_folder, gzip=False, tag=deployment.deploymentName)
image = docker_client.images.get(deployment.deploymentName)
shutil.rmtree(tempdir)
img_id = image.short_id
print(img_id)
As you can see currently I'm pushing the image to my own default/repo but I want to create a repo on user's account and then push the image to his own account. AND also, can I use apikey
instead of password
in login()
method?
How can I achieve this thing?
Help me, please!
Thank You, Abdul