溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

docker python api 安裝配置的詳解

發(fā)布時間:2020-09-05 07:34:51 來源:腳本之家 閱讀:133 作者:l6807718 欄目:服務(wù)器

docker python api 安裝配置的詳解

1.docker宿主機配置文件修改

$vim /etc/default/docker #再已有OPTS中添加
DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock"
使得可以通過tcp的2375端口連接到docker守護進程中,第二個-H及之后的內(nèi)容可以省略

2.安裝docker-py

$sudo pip install docker-py

3.編寫api腳本

參考文檔

http://docker-py.readthedocs.org/en/latest/

from docker import Client
d=Client(base_url='tcp://10.109.252.221:2375',version='auto',timeout=10)
#注意填寫url端口版本號和超時時間
def containerCreate(**command):
  container=d.create_containter(**command)
  print container
#這里使用非關(guān)鍵字可變長參數(shù)**command,可以將需要使用的參數(shù)以字典形式傳輸,并且函數(shù)會自動識別字典內(nèi)的參數(shù)
containerCreate(**{'name': 'test1', 'command': '/bin/bash','image':'ubuntu'})
#這里注意要使用**雙星號傳實參,不然會出錯。

4.端口綁定、磁盤掛載和link操作

def containerCreate(port, volume, link, **command): # 創(chuàng)建容器                             
    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 container


containerCreate(**{'name': 'test1','stdin_open':True,'tty':True, 'command': '/bin/bash', 'image': '10.109.252.221:5000/ubuntu',
        'ports':[8008],'port':{8008:9995},'volume':'/home/ubuntu/test:/test','link':{'mysql':'db'}})
#其中,ports必須聲明,port和volume是我自己寫的,用來傳遞參數(shù)

#ports聲明容器開放的端口,port中,第一個是容器端口,后一個是主機端口,正好與dokcer run -p相反

#link操作需要傳遞字典或元組,我自己使用元組沒有成功,用字典即可。

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI