[英]How to pass Bash commands from Python to a running Docker Container
[英]Pass commands from one docker container to another
我有一個助手容器和一個應用程序容器。
輔助容器通過git處理將代碼安裝到與應用容器共享的安裝中。
我需要幫助容器在克隆的代碼中檢查package.json
或requirements.txt
,是否存在要運行npm install
或pip 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.