首页 » docker » 正文

Python连接Docker编程

系统:
CentOS7

python版本:
Python 3.6.3

docker版本:

Client:
Version: 17.09.0-ce
API version: 1.32
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:41:23 2017
OS/Arch: linux/amd64

Server:
Version: 17.09.0-ce
API version: 1.32 (minimum version 1.12)
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:42:49 2017
OS/Arch: linux/amd64
Experimental: false

一.Docker的可编程性

在 《Dockerfile的使用,创建Apache镜像》 一文中我们使用了 来创建我们自己的docker镜像

然后,我们的控制台上会出现这样一个提示:
Sending build context to Docker daemon 2.048kB

官网的解释

The build is run by the Docker daemon, not by the CLI. The first thing a build process does is send the entire context (recursively) to the daemon. In most cases, it’s best to start with an empty directory as context and keep your Dockerfile in that directory. Add only the files needed for building the Dockerfile.

大概的意思是build 是Docker 守护进程运行的,不是CLI(客户端命令),运行时会把整个context文件发送给Daemon

参考文档:
https://docs.docker.com/engine/reference/builder/#usage

理解

两个重要的概念,docker分为 “客户端” “服务端” (也就是Docker守护进程)

Docker 服务端提供了一系列REST API(Docker Remote API),当我们
敲入docker命令时实际上是通过API和Docker服务端进行交互的

那也就是说,我们的docker客户端不仅可以使用官方提供的,还可以写程序来调用,甚至可以通过curl 来访问docker服务端的接口,达到调用docker服务端的目的。

参考文档:

如何调用Docker 服务端呢,请见参考文档

Docker provides an API for interacting with the Docker daemon (called the Docker Engine API), as well as SDKs for Go and Python. The SDKs allow you to build and scale Docker apps and solutions quickly and easily. If Go or Python don’t work for you, you can use the Docker Engine API directly.

The Docker Engine API is a RESTful API accessed by an HTTP client such as wget or curl, or the HTTP library which is part of most modern programming languages.

https://docs.docker.com/develop/sdk/#install-the-sdks

官方提供的连接方式

  • unix:///var/run/docker/sock(默认连接方式)
  • tcp://host:port
  • fd://socketfd

我们可以看看当前docker的连接方式

参考文档:

The Docker daemon can listen for Docker Engine API requests via three different types of Socket: unix, tcp, and fd.

By default, a unix domain socket (or IPC socket) is created at /var/run/docker.sock, requiring either root permission, or docker group membership.

If you need to access the Docker daemon remotely, you need to enable the tcp Socket. Beware that the default setup provides un-encrypted and un-authenticated direct access to the Docker daemon – and should be secured either using the built in HTTPS encrypted socket, or by putting a secure web proxy in front of it. You can listen on port 2375 on all network interfaces with -H tcp://0.0.0.0:2375, or on a particular network interface using its IP address: -H tcp://192.168.59.103:2375. It is conventional to use port 2375 for un-encrypted, and port 2376 for encrypted communication with the daemon.

https://docs.docker.com/engine/reference/commandline/dockerd/#examples

二.配置远程连接

如果我们是使用包管理器安装的docker,我们可以这样做

CentOS7

Ubuntu18.04 LTS

以下操作 CentOS7 与 Ubuntu18.04 LTS 相同

把原本是

ExecStart=/usr/bin/dockerd #这一行 注释掉 改成
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

配置成既支持 tcp连接 又支持 本地socks连接

然后保存后,重启Docker

再一次执行:

三.python连接Docker编程

首先进入python虚拟环境,安装docker包

通过代码测试一下

运行结果:
sha256:18542ecb6e46988c6389149695c2dfd43dbd85148d1bdf1fd3b98f352fba3745

参考文档:
https://docs.docker.com/develop/sdk/#go-sdk

发表评论

正在加载验证码......

请先完成验证