简体   繁体   English

将命令从一个Docker容器传递到另一个

[英]Pass commands from one docker container to another

I have a helper container and an app container. 我有一个助手容器和一个应用程序容器。

The helper container handles mounting of code via git to a shared mount with the app container. 辅助容器通过git处理将代码安装到与应用容器共享的安装中。

I need for the helper container to check for a package.json or requirements.txt in the cloned code and if one exists to run npm install or pip install -r requirements.txt , storing the dependencies in the shared mount. 我需要帮助容器在克隆的代码中检查package.jsonrequirements.txt ,是否存在要运行npm installpip install -r requirements.txt ,并将依赖项存储在共享安装中。 Thing is the npm command and/or the pip command needs to be run from the app container to keep the helper container as generic and as agnostic as possible. 事情就是npm命令和/或pip命令需要从应用程序容器中运行,以使帮助程序容器尽可能通用且不可知。

One solution would be to mount the docker socket to the helper container and run docker exec <command> <app container> but what if I have thousands of such apps on a single host. 一种解决方案是将docker套接字安装到助手容器并运行docker exec <command> <app container>但是如果我在一台主机上有成千上万个这样的应用程序该怎么办。 Will there be issues having hundreds of containers all accessing the docker socket at the same time? 数百个容器同时访问docker套接字是否会出现问题? And is there a better way to do this? 还有更好的方法吗? Get commands run on another container? 获取在另一个容器上运行的命令?

Well there is no "container to container" internal communication layer like "ssh". 嗯,没有像“ ssh”这样的“容器到容器”内部通信层。 In this regard, the containers are as standalone as 2 different VMs ( beside the network part in general ). 在这方面,容器与2个不同的虚拟机一样独立(通常在网络部分旁边)。

You might go the usual way, install opensshd-server on the "receiving" server, configure it key-based only. 您可能会采用通常的方法,在“接收”服务器上安装opensshd-server,仅基于密钥对其进行配置。 You do not need to export the port to the host, just connect to the port using the docker-internal network. 您无需将端口导出到主机,只需使用docker-internal网络连接到端口即可。 Deploy the ssh private key on the 'caller server' and the public key into .ssh/authorized_keys on the 'receiving server' during container start time ( volume mount ) so you do not keep the secrets in the image (build time). 在容器启动时间(卷装入)期间,将“呼叫者服务器”上的ssh私钥和“接收服务器”上的.ssh / authorized_keys部署到容器中(卷安装),这样就不会在映像中保留秘密(构建时间)。

Probably also create a ssh-alias in .ssh/config and also set HostVerify to no, since the containers could be rebuild. 可能还会在.ssh / config中创建一个ssh-alias,并将HostVerify设置为no,因为可以重建容器。 Then do 然后做

ssh <alias> your-command

Found that better way I was looking for :-) . 找到了我寻找的更好的方法:-)。

Using supervisord and running the xml rpc server enables me to run something like: 使用超级用户并运行xml rpc服务器使我可以运行以下内容:

supervisorctl -s http://127.0.0.1:9002 -utheuser -pthepassword start uwsgi supervisorctl -s http://127.0.0.1:9002 -utheuser -pthepassword start uwsgi supervisorctl -s http://127.0.0.1:9002 -utheuser -pthepassword start uwsgi supervisorctl -s http://127.0.0.1:9002 -utheuser -pthepassword start uwsgi

In the helper container, this will connect to the rpc server running on port 9002 on the app container and execute a program block that may look something like; 在助手容器中,这将连接到在应用容器上的端口9002上运行的rpc服务器,并执行一个可能类似于以下内容的程序块:

[program:uwsgi]
directory=/app
command=/usr/sbin/uwsgi --ini /app/app.ini --uid nginx --gid nginx --plugins http,python --limit-as 512
autostart=false
autorestart=unexpected
stdout_logfile=/var/log/uwsgi/stdout.log
stdout_logfile_maxbytes=0
stderr_logfile=/var/log/uwsgi/stderr.log
stderr_logfile_maxbytes=0
exitcodes=0
environment = HOME="/app", USER="nginx"]

This is exactly what I needed! 这正是我所需要的!

For anyone who finds this you'll probably need your supervisord.conf on your app container to look sth like: 对于任何发现此问题的人,您可能需要在应用程序容器上显示supervisor.conf,如下所示:

[supervisord]
nodaemon=true

[supervisorctl]

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[inet_http_server]
port=127.0.0.1:9002
username=user
password=password

[program:uwsgi]
directory=/app
command=/usr/sbin/uwsgi --ini /app/app.ini --uid nginx --gid nginx --plugins http,python --limit-as 512
autostart=false
autorestart=unexpected
stdout_logfile=/var/log/uwsgi/stdout.log
stdout_logfile_maxbytes=0
stderr_logfile=/var/log/uwsgi/stderr.log
stderr_logfile_maxbytes=0
exitcodes=0
environment = HOME="/app", USER="nginx"]

You can setup the inet_http_server to listen on a socket. 您可以将inet_http_server设置为在套接字上侦听。 You can link the containers to be able to access them at a hostname. 您可以链接容器以能够通过主机名访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM