1

I am trying to create a Docker container with the Docker Remote API using a Python script to do the Post opertion. This is my Python Script:-

import requests
import json
url = "http://localhost:4243/containers/create"
payload = {'Hostname':'','User':'','Memory':'0','MemorySwap':'0','AttachStdin':'false','AttachStdout':'t    rue','AttachStderr':'true','PortSpecs':'null','Privileged':    'false','Tty':'false','OpenStdin':'false','StdinOnce':'false','Env':'null','Cmd':['date'],'Dns':'null','Image':'ubuntu','Volumes':{},'VolumesFrom':'','WorkingDir':''}
headers = {'content-type': 'application/json', 'Accept': 'text/plain'}
print requests.post(url, data = json.dumps(payload), headers=headers).text

But when I run the script it shows this error

json: cannot unmarshal string into Go value of type bool

What is wrong with my script? I use Requests HTTP library for Python v2.7.5 and Ubuntu 13.10. I am new to docker and python scripting. Any help would be Appreciated.

2
  • Try using True and False instead of the strings "true" and "false" in your payload dict. Commented Oct 8, 2013 at 9:46
  • I tried using True and False. Now it shows this error- json: cannot unmarshal string into Go value of type []string Commented Oct 8, 2013 at 10:33

1 Answer 1

3

As pointed in the comments, you aren't using the right types.

Specifically:

  • boolean values must be True or False, instead of "true" or "false"
  • Dns, Env, and PortSpecs must be None instead of "null"
  • Memory and MemorySwap must be 0 instead of "0"

You can see all the type definitions in the API docs for the create command.

Here is a payload that works:

payload={
 'AttachStderr': True,
 'AttachStdin': False,
 'AttachStdout': True,
 'Cmd': ['date'],
 'Dns': None,
 'Env': None,
 'Hostname': '',
 'Image': 'ubuntu',
 'Memory': 0,
 'MemorySwap': 0,
 'OpenStdin': False,
 'PortSpecs': None,
 'Privileged': False,
 'StdinOnce': False,
 'Tty': False,
 'User': '',
 'Volumes': {},
 'VolumesFrom': '',
 'WorkingDir': '',
}

But somehow, it would be nice if the parser could tell exactly which field could not be parsed :-)

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

1 Comment

Yes :) I used the right types and its working Perfectly now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.