<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[运维进行时]]></title> 
<link>https://blog.liuts.com/index.php</link> 
<description><![CDATA[互联网运维与架构]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[运维进行时]]></copyright>
<item>
<link>https://blog.liuts.com/post/241/</link>
<title><![CDATA[Docker远程python API操作容器一例[原创]]]></title> 
<author>刘天斯 &lt;liutiansi@gmail.com&gt;</author>
<category><![CDATA[Docker]]></category>
<pubDate>Mon, 08 Sep 2014 15:48:23 +0000</pubDate> 
<guid>https://blog.liuts.com/post/241/</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://github.com/docker/docker-py" target="_blank">Docker-py</a>作为官方推出的客户端API，功能可以满足我们大部分操作需求，API涉及镜像(images)及容器(CONTAINER)的功能操作，利用docker-py可以轻松开发出Docker的管理平台，以便维护大规模的Docker集群，本文介绍如何通过DockerFile创建一个WEB服务的镜像，再通过远程API对容器进行管理。<br/><br/><strong>一、环境准备</strong><br/>1、环境说明<br/>192.168.1.20 #Docker python API主机<br/>192.168.1.22 #Docker服务主机<br/>2、Docker环境部署(略)<br/>3、修改自启动服务文件，支持远程TCP接口与本地SOCK连接；<br/># vi /etc/init.d/docker<br/><textarea name="code" class="c" rows="15" cols="100">
$exec -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -d &>> $logfile &
</textarea>#service docker restart<br/><br/><strong>二、创建镜像</strong><br/>1、获取最新的centos镜像<br/># docker pull centos:latest<br/>2、编写Dockerfile（支持apache+ssh服务）<br/># mkdir /home/Dockerfile/webserver<br/># cd /home/Dockerfile/webserver<br/># vi Dockerfile<br/><textarea name="code" class="python" rows="15" cols="100">
# This is a base comment
FROM centos:latest
MAINTAINER yorko Liu <liutiansi@gmail.com>

#yum install Package
RUN yum -y install net-tools
RUN yum -y install iputils&nbsp;&nbsp;iproute&nbsp;&nbsp;man&nbsp;&nbsp;vim-minimal&nbsp;&nbsp;openssh-server&nbsp;&nbsp;openssh-clients
RUN yum -y install httpd
RUN yum -y install python-setuptools
RUN easy_install supervisor

#set sshd
RUN ssh-keygen -q -N "" -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
RUN sed -ri 's/session&nbsp;&nbsp;&nbsp;&nbsp;required&nbsp;&nbsp;&nbsp;&nbsp; pam_loginuid.so/#session&nbsp;&nbsp;&nbsp;&nbsp;required&nbsp;&nbsp;&nbsp;&nbsp; pam_loginuid.so/g' /etc/pam.d/sshd
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
RUN echo 'root:Ksjhg34TDju' &#124; chpasswd

#set supervisor
RUN mkdir -p /var/log/supervisor
ADD supervisord.conf /etc/supervisord.conf

#set port
EXPOSE 22
EXPOSE 80

#set ENV
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8

#run supervisor
CMD ["/usr/bin/supervisord -c /etc/supervisord.conf"]
</textarea><br/>通过supervisord来维护Docker容器中服务进程，编写supervisord.conf<br/># vi supervisord.conf<br/><textarea name="code" class="c" rows="15" cols="100">
[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D

[program:httpd]
command=/usr/sbin/httpd -DFOREGROUND
</textarea><br/>创建镜像，运行：<br/># docker build -t yorko/webserver:v1 .<br/><span style="color: #DC143C;">注：最后有一个“.”，别遗漏。</span><br/><br/>镜像生成完毕后运行docker images查看，见下图：<br/><a href="https://blog.liuts.com/attachment.php?fid=348" target="_blank"><img src="https://blog.liuts.com/attachment.php?fid=348" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/><strong>三、编写操作API</strong><br/>登录192.168.1.20服务器<br/># mkdir /home/test/docker-py<br/># cd /home/test/docker-py<br/>1、安装docker-py<br/># wget https://github.com/docker/docker-py/archive/master.zip<br/># unzip master<br/># cd docker-py-master/<br/># python setup.py install<br/>如正常导入模块（import docker）说明安装成功。<br/><br/>2、创建容器docker_create.py<br/><textarea name="code" class="python" rows="15" cols="100">
import docker

c = docker.Client(base_url='tcp://192.168.1.22:2375',version='1.14',timeout=10)
c.create_container(image="yorko/webserver:v1",stdin_open=True,tty=True,command="/usr/bin/supervisord -c /etc/supervisord.conf",volumes=['/data'],ports=[80,22],name="webserver11")
#通过create_container方法创建容器，指定"yorko/webserver:v1"镜像名称，使用supervisord接管进程服务，挂载主宿机/data作为数据卷，容器监听80与22端口，容器的名称为webserver11
print str(r)
</textarea><br/>3、运行容器docker_start.py<br/><textarea name="code" class="python" rows="15" cols="100">
import docker

c = docker.Client(base_url='tcp://192.168.1.22:2375',version='1.14',timeout=10)
r=c.start(container='webserver11', binds=&#123;'/data':&#123;'bind': '/data','ro': False&#125;&#125;, port_bindings=&#123;80:80,22:2022&#125;, lxc_conf=None,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;publish_all_ports=True, links=None, privileged=False,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dns=None, dns_search=None, volumes_from=None, network_mode=None,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;restart_policy=None, cap_add=None, cap_drop=None)
#通过start方法启动容器，指定数据卷的挂载关系及权限，以及端口与主宿机的映射关系等
print str(r)
</textarea><br/>4、运行<br/># python docker_create.py<br/># python docker_start.py<br/>更多API参考<a href="https://github.com/docker/docker-py" target="_blank">https://github.com/docker/docker-py</a><br/><br/>5、在Docker主机观察结果，见下图：<br/><a href="https://blog.liuts.com/attachment.php?fid=349" target="_blank"><img src="https://blog.liuts.com/attachment.php?fid=349" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/><strong>三、校验服务</strong><br/>1、校验SSH服务<br/><a href="https://blog.liuts.com/attachment.php?fid=351" target="_blank"><img src="https://blog.liuts.com/attachment.php?fid=351" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>2、校验WEB服务<br/><a href="https://blog.liuts.com/attachment.php?fid=350" target="_blank"><img src="https://blog.liuts.com/attachment.php?fid=350" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>3、检查数据卷<br/><a href="https://blog.liuts.com/attachment.php?fid=352" target="_blank"><img src="https://blog.liuts.com/attachment.php?fid=352" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>Tags - <a href="https://blog.liuts.com/tags/docker/" rel="tag">docker</a> , <a href="https://blog.liuts.com/tags/docker-py/" rel="tag">docker-py</a> , <a href="https://blog.liuts.com/tags/dockerfile/" rel="tag">dockerfile</a> , <a href="https://blog.liuts.com/tags/supervisord/" rel="tag">supervisord</a>
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/241/#blogcomment2472</link>
<title><![CDATA[[评论] Docker远程python API操作容器一例[原创]]]></title> 
<author>胶粘剂 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 09 Sep 2014 14:42:56 +0000</pubDate> 
<guid>https://blog.liuts.com/post/241/#blogcomment2472</guid> 
<description>
<![CDATA[ 
	不错，带走
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/241/#blogcomment2473</link>
<title><![CDATA[[评论] Docker远程python API操作容器一例[原创]]]></title> 
<author>rakeliujie &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Wed, 10 Sep 2014 01:32:01 +0000</pubDate> 
<guid>https://blog.liuts.com/post/241/#blogcomment2473</guid> 
<description>
<![CDATA[ 
	干货docker这块&nbsp;&nbsp;有出书的打算么？
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/241/#blogcomment2474</link>
<title><![CDATA[[评论] Docker远程python API操作容器一例[原创]]]></title> 
<author>加密 &lt;dd@12.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Wed, 17 Sep 2014 03:12:20 +0000</pubDate> 
<guid>https://blog.liuts.com/post/241/#blogcomment2474</guid> 
<description>
<![CDATA[ 
	膜拜一下大神。
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/241/#blogcomment2477</link>
<title><![CDATA[[评论] Docker远程python API操作容器一例[原创]]]></title> 
<author>cc &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 25 Sep 2014 14:42:14 +0000</pubDate> 
<guid>https://blog.liuts.com/post/241/#blogcomment2477</guid> 
<description>
<![CDATA[ 
	好也好复杂
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/241/#blogcomment2584</link>
<title><![CDATA[[评论] Docker远程python API操作容器一例[原创]]]></title> 
<author>wangjifeng &lt;307372819@qq.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Wed, 01 Apr 2015 08:31:06 +0000</pubDate> 
<guid>https://blog.liuts.com/post/241/#blogcomment2584</guid> 
<description>
<![CDATA[ 
	Dockerfile 中的CMD 有错误改为如下：CMD [&quot;/usr/bin/supervisord&quot;, &quot;-c&quot;, &quot;/etc/supervisord.conf&quot;]
]]>
</description>
</item><item>
<link>https://blog.liuts.com/post/241/#blogcomment2664</link>
<title><![CDATA[[评论] Docker远程python API操作容器一例[原创]]]></title> 
<author>atptour &lt;49615154@qq.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 07 Feb 2017 08:13:00 +0000</pubDate> 
<guid>https://blog.liuts.com/post/241/#blogcomment2664</guid> 
<description>
<![CDATA[ 
	docker-py与RESTful风格的Docker Remote API相比，能实现相同的功能，貌似docker-py用起来更方便，敢问楼主是不是倾向于使用docker-py呢？
]]>
</description>
</item>
</channel>
</rss>