如何从容器执行主机的Docker命令? [英] How to execute host's Docker command from container?

查看:562
本文介绍了如何从容器执行主机的Docker命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python中编写Docker容器管理脚本。然而,由于我使用CoreOS,Python不包括在标准命令中。所以,我正在考虑使用Python Docker容器( https://registry.hub.docker.com / _ / python / )来执行我的脚本。但是,在这种情况下,脚本将在容器的虚拟机中执行,无法访问主机的Docker CLI。



有没有办法使用Python(或其他编程语言未包装在CoreOS中),以管理主机环境而不将其安装在主机上?



PS,脚本将执行如下操作:



docker run / rm / stop<另一个容器> ;;

解决方案

如果您通过HTTPS可以访问Docker守护程序,那么您可以从远程机器或Docker容器中与守护程序进行通信。在守护进程中启用HTTPS的说明如下: https://docs.docker.com/articles/https /



简而言之,它涉及创建客户端和服务器证书(用于安全性),并使用命令(如

$ b $)运行Docker守护程序b

  docker -d --tlsverify --tlscacert = ca.pem \ 
--tlscert = server-cert.pem --tlskey =服务器-key.pem \
-H = 0.0.0.0:2376

在这种模式下,您可以使用适当的客户端库来选择所需的编程语言。 https: //docs.docker.com/engine/reference/api/remote_api_client_libraries/



有一个用于python docker-py 我没有尝试过,但可以从经验中说出这种方法使用 docker-java 客户端库,在容器内部有一个Java程序停止并启动其他容器。


I want to write Docker containers management script in Python. However, since I use CoreOS, Python is not included as standard command. So, I am thinking of using Python Docker container (https://registry.hub.docker.com/_/python/) to execute my script. However, in that case the script will be executed in container's VM which doesn't have access to the host's Docker CLI.

Is there a way to use Python (or other programming languages not packaged in CoreOS), to manage host environment without installing it on the host machine?

PS, the script will do something like:

docker run/rm/stop <another container>;

解决方案

If you make the Docker daemon reachable via HTTPS then you can communicate with the daemon from remote machines, or from within Docker containers. The instructions for enabling HTTPS in the daemon are here https://docs.docker.com/articles/https/

In short it involves creating client and server certificates (for security) and running the Docker daemon with a command such as

docker -d --tlsverify --tlscacert=ca.pem \
--tlscert=server-cert.pem --tlskey=server-key.pem \
-H=0.0.0.0:2376

When running in this mode, you can use an appropriate client library for the programming language of your choice https://docs.docker.com/engine/reference/api/remote_api_client_libraries/

There is one for python docker-py I haven't tried it but can say from experience this approach works using docker-java client library, having a Java program inside a container stopping and starting other containers.

这篇关于如何从容器执行主机的Docker命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆