Unferth
Unferth

Reputation: 4678

Python script for creating docker container using Remote API

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.

Upvotes: 1

Views: 1607

Answers (1)

jpetazzo
jpetazzo

Reputation: 15501

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 :-)

Upvotes: 3

Related Questions