1

I am able to build docker image using python sdk. if the dockerfile is available on my local machine.

client = docker.from_env()
image, build_log = client.images.build(path = "./", tag=image_name,rm=True)   

Now, My docker files will be maintained in github repository, I should pull them and build the images. python sdk doc says build method accepts path or file objects.

I was able to read the content of the docker file from github using pyGithub (API3) repository

g = Github(base_url=url, login_or_token=accessToken, verify=False)
dmc = g.get_organization(org_name)
repo = dmc.get_repo(repoName)
contents = repo.get_contents(dockerfile_name, "master")

I am not sure how to convert above contents object(ContentFile.ContentFile) to a python file object so that I can use it to build the image as below

client = docker.from_env()
image, build_log = client.images.build(fileobj = contents_file_obj, tag=image_name,rm=True)
karas27
  • 335
  • 1
  • 5
  • 15

1 Answers1

0

Below working piece of code can be used to bring out Python file obj from ContentFile.ContentFile:

import io
from github import Github 
import docker

# docker client
client = docker.from_env()

# github repo
g = Github(base_url=url, login_or_token=accessToken, verify=False)
dmc = g.get_organization(org_name)
repo = dmc.get_repo(repoName)
contents = repo.get_contents(dockerfile_name, "master")

# decoding contents of file and creating file obj out of it
decoded_content_str = contents.decoded_content.decode('utf-8')
contents_file_obj = io.StringIO(decoded_content_str)

# building image out of contents file obj
client = docker.from_env()
image, build_log = client.images.build(
                       fileobj=contents_file_obj,
                       tag=image_name,
                       rm=True)

In above code,
method decoded_content of contents (obj of class ContentFile.ContentFile) returns byte string content of file. which decoded into string by .decode('utf-8').
then decoded_content_str is used to create IO object (same as file object in python) using io.StringIO

  • okay. I've modified answer for better visibility of issue. – Roshan Chauhan Apr 08 '21 at 07:07
  • @Tomerikoo can you please remove downvote from my answer as it banned my account from answering and questioning. – Roshan Chauhan Apr 08 '21 at 07:14
  • I didn't downvote... Please see [Why shouldn't I assume I know who downvoted my post?](https://meta.stackoverflow.com/questions/388686/why-shouldnt-i-assume-i-know-who-downvoted-my-post). Please also note that your answer is still a bit lacking... Try to show a bit of context, how exactly to use this `.content()` property – Tomerikoo Apr 08 '21 at 07:22
  • hi @Tomerikoo, firstly, thanks for helping out what need to be shown in ideal answer. I've added tested code which answers the question and this time I've added some more research and tested locally before re-editing my answer. – Roshan Chauhan May 03 '21 at 07:46
  • I'm happy to see you took the bother to improve your answer as needed. By the way, if you have some link to a resource that helped you write this answer, it is ok to post it - the problem before was that you completely relied on that link. You can post a link as a reference for further reading to support your answer – Tomerikoo May 03 '21 at 07:51
  • yes. I'll make sure to be more resourceful and descriptive before contributing. Thanks again. :) – Roshan Chauhan May 03 '21 at 08:01