Detailed explanation of installation and configuration of docker python api
Detailed explanation of installation and configuration of docker python api
1.docker host configuration file modification
$vim / etc/default/docker # add DOCKER_OPTS= "- H tcp://0.0.0.0:2375-H unix://var/run/docker.sock" to the existing OPTS so that you can connect to the docker daemon through port 2375 of tcp, and the second-H and later contents can be omitted
two。 Install docker-py
$sudo pip install docker-py
3. Write api scripts
Reference documentation
Http://docker-py.readthedocs.org/en/latest/
From docker import Clientd=Client (base_url='tcp://10.109.252.221:2375',version='auto',timeout=10) # Note to fill in the url port version number and timeout def containerCreate (* * command): container=d.create_containter (* * command) print container#. Here the non-keyword variable length parameter * * command is used, and the parameters you need can be transmitted in dictionary form. And the function will automatically identify the parameter containerCreate in the dictionary (* * {'name':' test1', 'command':' / bin/bash','image':'ubuntu'}) # here, please use * * double asterisk to pass the argument, otherwise an error will occur.
4. Port binding, disk mount, and link operation
Def containerCreate (port, volume, link, * * command): # create container command ['host_config'] = d.create_host_config (port_bindings=port, binds= [volume], links=link) container= d.create_container (* * command) d.start (container=container.get (' Id')) print containercontainerCreate (* * {'name':' test1','stdin_open':True,'tty':True) 'command':' / bin/bash', 'image':' 10.109.252.221 ports': 5000 pictups, 'ports': [8008],' port': {8008 ports': 9995}, 'volume':'/home/ubuntu/test:/test','link': {' mysql':'db'}}) # of which Ports must declare that port and volume are written by myself and are used to pass the parameter # ports declares the open port of the container. In port, the first is the container port, and the second is the host port, which is the opposite of dokcer run-p. # link operation requires passing dictionaries or tuples. If I have failed to use tuples, I can use a dictionary.
If you have any questions, please leave a message or go to the community to exchange and discuss, thank you for reading, hope to help you, thank you for your support!