如何在已经通过Terraform和GitHub Action启动的AWS EC2机器中使用Docker-Compose?

发布于 2025-01-22 15:14:54 字数 1380 浏览 3 评论 0 原文

我有一个简单的小网络应用程序,该应用程序由四个带有烧瓶应用程序的Docker容器和一个Docker-Compose文件组成。他们通过消息队列通信。 对于本地启动该应用程序,我只需使用Docker-Compose。 然后,可以通过Localhost上的网关访问所有Web应用程序:5004。

现在,我试图以下面的方式自动化部署:

  • 每次我推到github repo时,通过github操作的工作流程开始启动
  • 此工作流程将连接到Terraform Cloud(在Terraform.yml中指定)
  • Terraform创建一个新的AWS EC2实例端口5004打开(在main.tf中指定)
  • ,这里出现了问题...

在本地,我将

  • 使用“ SCP -R -I -i my_key .pem ”将文件复制到EC2计算机。 pathtoproject ec2-user@ publicip :〜/“
  • ssh进入ec2机器
  • 安装docker and docker-compose
  • do doce do docker-compose
  • 在此之后,我可以使用 publicip:5004

我该如何通过github动作和Terraform做到这一点?

我已经尝试了以下操作:

  • 在main.tf中:使用EC2计算机的用户_DATA属性添加启动计算机后应执行的命令。问题:我不知道如何从存储库中复制文件或克隆EC2计算机内的存储库。要复制,我需要正确的路径(显然,它不仅仅是指向文件的链接)。对于克隆,我需要在EC2计算机内创建一个新的SSH键,然后手动将其添加到我的GitHub帐户中 - 但是由于它应该是自动化的,因此这不是解决方案。
  • 另一个选择是使用我的容器的公共码头图像 - 它们也是由另一个github动作创建的。但是我仍然有同样的问题:我必须以某种方式上传 /复制 /克隆docker-compose文件到EC2计算机。
  • 我还试图通过GitHub操作直接进行操作,并仅将Terraform用于机器的创建,但是由于我每次都在创建新机器,因此EC2机器的IP地址总是会更改。但是我需要SSH-ing在...

这是回购: https:// /GITHUB.com/janakaras/docker-in-the-cloud

我希望我能够准确描述这个问题,如果不是,我很抱歉,我是所有CI/CD材料的新手:-)。

最好的问候,谢谢 Jana

I have a simple little web application that consists of four docker containers with flask apps in them and a docker-compose file. They communicate via a message queue.
For starting the app locally, I simply use docker-compose up.
Then, all the web apps can be accessed via a gateway on localhost:5004.

Now I am trying to automate the deployment in the following way:

  • Every time I push to the GitHub repo, a workflow via Github Actions is started
  • This workflow connects to Terraform Cloud (specified in terraform.yml)
  • Terraform creates a new AWS EC2 instance with port 5004 opened (specified in main.tf)
  • And here comes the problem...

Locally, I would now

  • copy the files to the EC2 machine with "scp -r -i my_key.pem pathtoproject ec2-user@publicIP:~/"
  • ssh into the EC2 machine
  • install docker and docker-compose
  • do docker-compose up
  • After that, I am able to access my app with publicIP:5004

How can I do that via Github Actions and Terraform?

I already tried the following:

  • In main.tf: Use the user_data property of the EC2 machine for adding commands that should be executed after starting the machine. Problem here: I don't know how I can copy the files from the repository or clone the repository inside the EC2 machine. For copying, I would need the correct path (apparently, it is not simply the link to the file). For cloning, I would need to create a new SSH key inside the EC2 machine and add it manually to my github account - but since it should be automated, this is not a solution.
  • Another option would be to use the public docker images of my containers - they are also being created by another Github Action. But still here, I have the same problem: I would have to somehow upload / copy / clone the docker-compose file to the EC2 machine.
  • I also tried to do it directly via Github Actions and using Terraform solely for the creation of the machine, but since I am creating a new machine every time, also the IP address of the EC2 machine always changes. But I need that for ssh-ing in...

This is the repo: https://github.com/janakaras/docker-in-the-cloud

I hope I was able to describe the issue accurately, if not, I am sorry, I am new to all the CI/CD stuff :-).

Best regards and thanks,
Jana

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文