我尝试用Jenkins中的docker py建立一个docker形象。在
# Let's build the toolchain-base-image
from io import BytesIO
from docker import Client
from pprint import pprint
import sys
cli = Client(base_url="tcp://127.0.0.1:4243")
#cli = Client(base_url='unix://var/run/docker.sock')
GCC_VERSION_TAG="4_9"
with open("/path/to/docker-" + GCC_VERSION_TAG + "-gcc-base-image-dockerfile", mode="r") as dockerfile:
f = BytesIO(dockerfile.read().encode('utf-8'))
try:
response = [pprint(line["stream"]) for line in cli.build(fileobj=f, nocache=False, rm=True, tag='gcc49/toolchain-base_'+GCC_VERSION_TAG, decode=True, pull=True)]
except:
raise IOError("Invalid Dockerfile!")
if response != "None":
pprint(response[0])
print "Create container"
container = cli.create_container(image='gcc49/toolchain-base_' + GCC_VERSION_TAG + ':latest',stdin_open=True, tty=True, volumes=['/ssd', '/opt', '/nfs/'], host_config=cli.create_host_config(binds=['/ssd:/ssd:rw','/opt/:/opt:ro','/nfs:/nfs:rw']))
print "Start container"
cli.start(container=container.get('Id'))
log_stream_list = []
[log_stream_list.append(l) for l in cli.logs(container, stream=True)]
print "".join(log_stream_list)
15:37:27 [docker-test_job] $ python /tmp/hudson5834793409651253293.py
15:37:29 u'Step 1 : FROM ubuntu:16.04\n'
15:37:29 Traceback (most recent call last):
15:37:29 File "/tmp/hudson5834793409651253293.py", line 15, in <module>
15:37:29 raise IOError("Invalid Dockerfile!")
15:37:29 IOError: Invalid Dockerfile!
15:37:29 Build step 'Execute Python script' marked build as failure
15:37:29 Stopping all containers
15:37:29 Finished: FAILURE
当我试着用乌班图14.04:it效果很好。有人有主意吗? 谢谢你的帮助!在
更新:一些附加信息:Docker1.5+Ubuntu12.04 LTS。 当我在控制台上运行时
docker build -t name/tag -f dockerfilename .
它也起作用了,docker按照预期构建了图像。在
它现在起作用了。我发现我不能使用
pull=True
。我想返回代码>;0,Jenkins会将其解释为错误。谢谢你的帮助。:)下面是工作的代码。在更新
我错了。在上排,我尝试匹配键“stream”。但是如果docker把图像拉出来。没有这样的钥匙。在
^{pr2}$现在一切都清楚了。:)
我猜您需要提供Docker文件的路径(包括Dockerfile的名称),而在python脚本中,您似乎只提供Dockerfile所在的目录。试着换零件
打开(“/path/to/docker-”+GCC_VERSION_TAG+“-GCC base image”,mode=“r”)'
到
打开(“/path/to/docker-”+GCC_VERSION_TAG+“-GCC base image/dockerfilename”,mode=“r”)
看看能不能用
相关问题 更多 >
编程相关推荐