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'>
change198
  • 1,647
  • 3
  • 21
  • 60

1 Answers1

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?

404pio
  • 1,080
  • 1
  • 12
  • 32
  • 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)so, don't know why in editor it works. – change198 Dec 18 '19 at 12:17
  • `tag=localtag|{localtag}` - this construction is very very strange – 404pio Dec 18 '19 at 12:18
  • 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. – change198 Dec 18 '19 at 12:20
  • 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. – change198 Dec 18 '19 at 13:08
  • 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` : https://github.com/docker/docker-py/issues/978 – 404pio Dec 19 '19 at 07:51
  • At this moment, we are kind of bound with IT system so, no version 3 at this moment. :-( however this is docker python package version >>> print(docker.__version__) 4.1.0 >>> – change198 Dec 20 '19 at 12:54