2

I have this script:

import docker
import os

localtag = "mypersonaltag"

client = docker.from_env()
dockerfile_path = os.path.dirname(os.getcwd())
print(dockerfile_path)

fname = dockerfile_path + "/Dockerfile"
path = os.path.dirname(fname)
image, build_log  = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag='localtag', rm=True)
print(image)

If I execute this script, it creates and build local docker image with tag name as localtag, but not as mypersonaltag. I have tried all other possible way like below, have a look at tag parameter. and it's throwing error.

image, build_log  = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag=localtag, rm=True)

or
image, build_log = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag={localtag}, rm=True)

but the strange thing, this [image, build_log = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag=localtag, rm=True)] command works perfectly thru python editor while using python script it gives error. No idea why? But as soon as I give quotes around the tags the python script which was breaking now working fine.

>>> tag  = "test02"
>>> image, build_log  = client.images.build(path=path, dockerfile='Dockerfile', tag=tag, rm=True)
>>> print(image)
<Image: 'local_tag:latest', 'localtag:latest', 'test01:latest', 'test02:latest'>

1 Answer 1

1
image, build_log  = client.images.build(path=dockerfile_path, dockerfile='Dockerfile', tag=localtag, rm=True)

It's problem with passing variable to function, remove quotes around localtag

If you pass variable in such way tag={localtag}, then you are telling to python: "Hey take may variable, put it inside set, and pass this set to function". Probably you would try f"{localtag}" which uses fstring construction (requires python3.6).

--edit--

Please show us, how do you invoke your script? And what is the error?

Sign up to request clarification or add additional context in comments.

6 Comments

if you look at my question, I tried this one in my script, it doesn't work or doesn't like quotes around my tag. without quotes it works fine with quotes it complains "requests.exceptions.ConnectionError: ('Connection aborted.', error(104, 'Connection reset by peer'))" but odd enough, if I run the command in python editor it works like charm, >>> tag = "test02" >>> image, build_log = client.images.build(path=path, dockerfile='Dockerfile', tag=tag, rm=True)>>> print(image)<Image: 'local_tag:latest', 'localtag:latest', 'test01:latest', 'test02:latest'>so, don't know why in editor it works.
tag=localtag|{localtag} - this construction is very very strange
now I understand, your confusion "|" this means I tried with both options without quotes and with curly braces. But more strange thing is it works fine in python editor but not via script. I will update the question.
it's not working either. Please see below, I'm invoking python script using python scriptname.py and error I get with out quotes are File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 498, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', error(104, 'Connection reset by peer')) and my python version is 2.7 so no fstring options. so something tells me tag with the quotes is giving me the problem in the script.
First of all, I strongly suggest you to switch to python3 (python2 is deprecated). Second show me version of your docker-py package - it may looks like error in your version docker-py : github.com/docker/docker-py/issues/978
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.