docker-py
docker-py copied to clipboard
"json: cannot unmarshal object into Go value of type []string"
I am trying to create a docker service form docker.py, I have a small function to generate a task_template. Am I doing anything wrong here ? , I can create servers if I dont pass the "env" dict ,
def task_template(image,password):
task_template = {
"ContainerSpec": {
"Image":image,
"env" : {"MYSQL_ROOT_PASSWORD":password}
},
}
return task_template
out put looks like this
task_template("mysql","mypassword")
{'ContainerSpec': {'Image': 'mysql',
'env': {'MYSQL_ROOT_PASSWORD': 'mypassword'}}}
I am trying this form Ipython, If i remove the "env" : {"MYSQL_ROOT_PASSWORD":password}" it's creating the service (Service keep restarting because it's don't have the root password )
In [15]: import docker
In [16]: client = docker.APIClient(base_url='192.168.130.120:5555')
In [17]: client.version()
Out[17]:
{'ApiVersion': '1.26',
'Arch': 'amd64',
'BuildTime': '2017-02-23T10:54:03.848297239+00:00',
'Experimental': True,
'GitCommit': '60ccb22',
'GoVersion': 'go1.7.5',
'KernelVersion': '3.10.0-514.10.2.el7.x86_64',
'MinAPIVersion': '1.12',
'Os': 'linux',
'Version': '17.03.0-ce'}
In [18]: from beacon_base import task_template
In [19]: client.create_service(task_template("mysql","mypassword"))
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
/home/msreddy/env_py3/lib/python3.5/site-packages/docker/api/client.py in _raise_for_status(self, response)
205 try:
--> 206 response.raise_for_status()
207 except requests.exceptions.HTTPError as e:
/home/msreddy/env_py3/lib/python3.5/site-packages/requests/models.py in raise_for_status(self)
908 if http_error_msg:
--> 909 raise HTTPError(http_error_msg, response=self)
910
HTTPError: 500 Server Error: Internal Server Error for url: http://192.168.130.120:5555/v1.24/services/create
During handling of the above exception, another exception occurred:
APIError Traceback (most recent call last)
<ipython-input-19-184378390795> in <module>()
----> 1 client.create_service(task_template("mysql","mypassword"))
/home/msreddy/env_py3/lib/python3.5/site-packages/docker/utils/decorators.py in wrapper(self, *args, **kwargs)
33 )
34 )
---> 35 return f(self, *args, **kwargs)
36 return wrapper
37 return decorator
/home/msreddy/env_py3/lib/python3.5/site-packages/docker/api/service.py in create_service(self, task_template, name, labels, mode, update_config, networks, endpoint_config, endpoint_spec)
64 }
65 return self._result(
---> 66 self._post_json(url, data=data, headers=headers), True
67 )
68
/home/msreddy/env_py3/lib/python3.5/site-packages/docker/api/client.py in _result(self, response, json, binary)
210 def _result(self, response, json=False, binary=False):
211 assert not (json and binary)
--> 212 self._raise_for_status(response)
213
214 if json:
/home/msreddy/env_py3/lib/python3.5/site-packages/docker/api/client.py in _raise_for_status(self, response)
206 response.raise_for_status()
207 except requests.exceptions.HTTPError as e:
--> 208 raise create_api_error_from_http_exception(e)
209
210 def _result(self, response, json=False, binary=False):
/home/msreddy/env_py3/lib/python3.5/site-packages/docker/errors.py in create_api_error_from_http_exception(e)
28 else:
29 cls = NotFound
---> 30 raise cls(e, response=response, explanation=explanation)
31
32
APIError: 500 Server Error: Internal Server Error for url: http://192.168.130.120:5555/v1.24/services/create ("json: cannot unmarshal object into Go value of type []string")
try this instead:
from docker.types import ContainerSpec, TaskTemplate
c_spec = ContainerSpec(image='mysql', env={'MYSQL_ROOT_PASSWORD': 'mypassword'})
task_tmpl = TaskTemplate(c_spec)
client.create_service(task_tmpl)
That's works like charm Thank you.