使用docker';以安全的方式访问远程API

使用docker';以安全的方式访问远程API,docker,authentication,encryption,tls1.2,Docker,Authentication,Encryption,Tls1.2,我试图找到一种有效的方法,以安全的方式使用docker远程API。 我有一个docker守护进程在远程主机上运行,另一台机器上有一个docker客户端。我需要我的解决方案不依赖于客户机/服务器操作系统,以便它与任何具有docker客户机/守护进程的机器相关 到目前为止,我发现做这件事的唯一方法是使用openssl在Linux机器上创建证书,并手动将证书复制到客户端/服务器,如本例所示: 然后在两侧配置docker以使用证书进行加密和身份验证 在我看来,这种方法相当笨拙,因为有时复制文件并将它们

我试图找到一种有效的方法,以安全的方式使用docker远程API。 我有一个docker守护进程在远程主机上运行,另一台机器上有一个docker客户端。我需要我的解决方案不依赖于客户机/服务器操作系统,以便它与任何具有docker客户机/守护进程的机器相关

到目前为止,我发现做这件事的唯一方法是使用openssl在Linux机器上创建证书,并手动将证书复制到客户端/服务器,如本例所示:

然后在两侧配置docker以使用证书进行加密和身份验证

在我看来,这种方法相当笨拙,因为有时复制文件并将它们放在我想要使用远程API的每台机器上是一个问题

我在找更优雅的东西

我发现的另一个解决方案是使用代理进行基本的HTTP身份验证,但在这种方法中,流量没有加密,这样做并不真正安全


是否有人对不同的解决方案或改进上述方法提出建议?

您最喜欢的系统自动化工具(,)可能可以直接管理远程主机上正在运行的Docker容器,而无需打开另一个根等效网络路径。有一些面向Docker的集群工具(,)可以在本地或远程运行容器,但您对容器的确切位置控制较少(您通常并不关心),而且它们往往会接管正在运行的机器

如果我真的必须以这种方式管理系统,我可能会使用某种集中式存储来保存TLS客户机密钥,最有可能的是,它具有加密存储密钥的特性,需要某种级别的身份验证来检索密钥,并且能够访问和控制密钥。您可以编写这样的shell函数(未经测试):

dockerHost(){
mkdir-p“$HOME/.docker/$1”
JSON=$(vault kv get-format=JSON“secret/docker/$1”)
对于ca.pem cert.pem key.pem中的f;do
echo“$JSON”| jq.data.data.[\“$f\”]“>“$HOME/.docker/$1/$f”
完成
导出DOCKER_HOST=“https://$1:2376”
导出DOCKER\u CERT\u PATH=“$HOME/.DOCKER/$1”
}

虽然您的问题清楚地表明您理解这一点,但值得重复:不要启用对Docker daemon的未经身份验证的远程访问,因为如果您可以访问套接字,则接管具有不受限制的根访问权限的主机是微不足道的。

根据您的评论,如果您不需要swarm功能并且只需要单主机支持,我建议您使用Ansible。Ansible只需要SSH访问,您可能已经有了这种访问

使用Docker Compose中定义的现有服务非常容易,或者您可以在Ansible中调用shell脚本。无需向外部世界公开Docker守护程序

一个非常简单的示例文件(
playbook.yml

运行剧本

ansible剧本-iusername@mysshhost.com,playbook.yml

Ansible通过其模块系统提供与Docker交互所需的几乎所有功能:

docker_service
    Use your existing Docker compose files to orchestrate containers on a single Docker daemon or on Swarm. Supports compose versions 1 and 2.
docker_container
    Manages the container lifecycle by providing the ability to create, update, stop, start and destroy a container.
docker_image
    Provides full control over images, including: build, pull, push, tag and remove.
docker_image_facts
    Inspects one or more images in the Docker host’s image cache, providing the information as facts for making decision or assertions in a playbook.
docker_login
    Authenticates with Docker Hub or any Docker registry and updates the Docker Engine config file, which in turn provides password-free pushing and pulling of images to and from the registry.
docker (dynamic inventory)
    Dynamically builds an inventory of all the available containers from a set of one or more Docker hosts.

我使用集群工具的问题是,我希望在一台主机上部署多个容器,因此我不需要整个集群,只需要一台主机。我曾想过使用单一主机集群,但我觉得这是不对的。你认为这是一个好的选择吗?老实说,这对很多事情来说都是过分的,我希望Swarm在Docker的官方教程中不是那么突出。但是,对于单个主机上的多个容器,Docker Compose非常轻量级和标准化;但它不适用于多主机安装,这正是您最初的问题的重点。我只希望一台主机成为docker主机,并且我希望能够远程控制此主机,仅在远程主机上使用docker客户端。客户端将不会有docker守护程序,但只有cli docker客户端二进制文件。对于该设置,我的第一个选择是对主机使用
ssh
。目前我正在使用ssh,但我希望在程序/脚本中执行,而不是手动执行。我有很多docker操作和配置,我想从远程主机执行这些操作和配置(创建容器,将它们连接到网络等),我想使用docker py模块来完成这些操作。我还尝试将docker remote API封装在某种与容器上运行的应用程序相关的界面中。感谢您的详细评论。我研究过Ansible,但它似乎不适合,因为“Ansible主机”必须是Linux机器。在我的解决方案中,我希望允许具有任何操作系统的任何类型的机器从远处控制容器。例如,仅使用docker客户端二进制文件,任何支持docker的主机(windows、OSX、Linux等)都可以远程控制容器,除了单个二进制文件外,没有任何依赖关系。
docker_service
    Use your existing Docker compose files to orchestrate containers on a single Docker daemon or on Swarm. Supports compose versions 1 and 2.
docker_container
    Manages the container lifecycle by providing the ability to create, update, stop, start and destroy a container.
docker_image
    Provides full control over images, including: build, pull, push, tag and remove.
docker_image_facts
    Inspects one or more images in the Docker host’s image cache, providing the information as facts for making decision or assertions in a playbook.
docker_login
    Authenticates with Docker Hub or any Docker registry and updates the Docker Engine config file, which in turn provides password-free pushing and pulling of images to and from the registry.
docker (dynamic inventory)
    Dynamically builds an inventory of all the available containers from a set of one or more Docker hosts.