参考文档:https://docker-py.readthedocs.io/en/stable/index.html
在esc机器上执行命令,可采用两种方案:
一是使用docker client现有的api,需要达到可以执行主机命令的目的。
二是在agent镜像上加个方法,让docker client远程启动这个镜像执行。
所以先需要调研下docker client有哪些可以执行主机命令。
注:
Docker Client是Docker提供给用户的客户端。Docker Client提供给用户一个终端,用户输入Docker提供的命令来管理本地或者远程的服务器。
通过参阅文档,发现有两个方法可实现cmd命令:exec_create和create_container。
首先exec_create:
exec_create
(container, cmd, stdout=True, stderr=True, stdin=False, tty=False, privileged=False, user='', environment=None, workdir=None, detach_keys=None)
Sets up an exec instance in a running container。exec_create相当与docker exec命令 :在运行的容器中执行命令
Parameters: |
|
Returns: | A dictionary with an exec |
Return type: | (dict) |
Raises: |
|
然后是create_container:
create_container
(image, command=None, hostname=None, user=None, detach=False, stdin_open=False, tty=False, ports=None, environment=None, volumes=None, network_disabled=False, name=None, entrypoint=None, working_dir=None, domainname=None, host_config=None, mac_address=None, labels=None, stop_signal=None, networking_config=None, healthcheck=None, stop_timeout=None, runtime=None, use_config_proxy=False)
Creates a container. Parameters are similar to those for the docker run
command except it doesn't support the attach options (-a
).
The arguments that are passed directly to this function are host-independent configuration options. Host-specific configuration is passed with the host_config argument. You’ll normally want to use this method in combination with the create_host_config()
method to generate host_config
.
create_container类似与docker run命令:创建一个新的容器并运行一个命令。指示它不支持-a参数。
pip install docker
pip install docker-compose