简体   繁体   English

从 Docker 容器 A(在 Docker 容器 B 上)运行 Bash 脚本

[英]Running a Bash Script from (on Docker Container B) from Docker Container A

I have two Docker Containers configured through a Docker Compose file.我通过 Docker Compose 文件配置了两个 Docker 容器。

  • Docker Container A - (teamcity-agent) Docker 容器 A - (teamcity-agent)
  • Docker Container B - (build-tool) Docker 容器 B -(构建工具)

Both start up fine.两者都启动良好。 But as part of the build process in TeamCity - I would like the Agent (Container A) to run a bash script which is on Docker Container B (Only B can run this script).但作为 TeamCity 构建过程的一部分 - 我希望代理(容器 A)运行 Docker 容器 B 上的 bash 脚本(只有 B 可以运行此脚本)。

I tried to set this up using the SSH build step in Team City, but I get connection refused.我尝试使用 Team City 中的 SSH 构建步骤进行设置,但连接被拒绝。

Further reading into it shows that SSH isn't enabled in containers and that I shouldn't really be trying to SSH into a container.进一步阅读它表明容器中未启用 SSH,我不应该真正尝试将 SSH 连接到容器中。

So how can I get Container A to run the script on Container B and see the output of the script on A?那么如何让容器 A 在容器 B 上运行脚本并查看脚本在 A 上的输出呢?

What is the best practice for this?这方面的最佳做法是什么?

The only way without modifying the application itself is through SSH.不修改应用程序本身的唯一方法是通过 SSH。 It is completely false you cannot SSH to a container.您无法通过 SSH 连接到容器是完全错误的。 I use SSH to a database container to run database export inside it.我使用 SSH 连接到数据库容器以在其中运行数据库导出。

First be sure openssh-server is installed on B. Then you must setup a passwordless connection between A and B.首先确保 openssh-server 安装在 B 上。 然后你必须在 A 和 B 之间设置无密码连接。

Then be sure you link your containers in the docker-compose file so you won't need to expose the SSH port.然后确保在 docker-compose 文件中链接您的容器,这样您就不需要公开 SSH 端口。

Snippet to add in Dockerfile for container B在容器 B 的 Dockerfile 中添加的代码段

RUN apt-get install -q -y openssh-server
ADD id_rsa.pub /home/ubuntu/.ssh/authorized_keys
RUN chown -R ubuntu:ubuntu /home/ubuntu/.ssh ; \
  chmod 700 /home/ubuntu/.ssh ; \
  chmod 600 /home/ubuntu/.ssh/authorized_keys

Also you can run the script outside the containers using docker exec in a crontab in the host.您也可以在主机的 crontab 中使用 docker exec 在容器外运行脚本。 But I think you are not looking for this extreme solution.但我认为您不是在寻找这种极端的解决方案。

I can help you via comments我可以通过评论帮助你

Regards问候

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

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