繁体   English   中英

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

[英]Pass commands from one docker container to another

我有一个助手容器和一个应用程序容器。

辅助容器通过git处理将代码安装到与应用容器共享的安装中。

我需要帮助容器在克隆的代码中检查package.jsonrequirements.txt ,是否存在要运行npm installpip install -r requirements.txt ,并将依赖项存储在共享安装中。 事情就是npm命令和/或pip命令需要从应用程序容器中运行,以使帮助程序容器尽可能通用且不可知。

一种解决方案是将docker套接字安装到助手容器并运行docker exec <command> <app container>但是如果我在一台主机上有成千上万个这样的应用程序该怎么办。 数百个容器同时访问docker套接字是否会出现问题? 还有更好的方法吗? 获取在另一个容器上运行的命令?

嗯,没有像“ ssh”这样的“容器到容器”内部通信层。 在这方面,容器与2个不同的虚拟机一样独立(通常在网络部分旁边)。

您可能会采用通常的方法,在“接收”服务器上安装opensshd-server,仅基于密钥对其进行配置。 您无需将端口导出到主机,只需使用docker-internal网络连接到端口即可。 在容器启动时间(卷装入)期间,将“呼叫者服务器”上的ssh私钥和“接收服务器”上的.ssh / authorized_keys部署到容器中(卷安装),这样就不会在映像中保留秘密(构建时间)。

可能还会在.ssh / config中创建一个ssh-alias,并将HostVerify设置为no,因为可以重建容器。 然后做

ssh <alias> your-command

找到了我寻找的更好的方法:-)。

使用超级用户并运行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

在助手容器中,这将连接到在应用容器上的端口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"]

这正是我所需要的!

对于任何发现此问题的人,您可能需要在应用程序容器上显示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"]

您可以将inet_http_server设置为在套接字上侦听。 您可以链接容器以能够通过主机名访问它们。

暂无
暂无

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

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