运行 docker-在 EC2 用户数据中编写

run docker-compose in EC2 user-data

我有一个包含 dokcer compose 的 EC2 linux2 实例。我想让实例启动 docker 服务,导航到正确的文件夹,然后 运行 docker-compose up -d,简单的三行,每次实例启动时:

sudo service docker start
cd APP
docker-compose up -d

如图所示 here,在实例停止时将此输入到用户数据应该有效:

#!/bin/bash
sudo service docker start
cd APP
docker-compose up -d

但它什么也没做。 aws doc 还有这个脚本:

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
/bin/echo "Hello World" >> /tmp/testfile.txt  <<<<<<<<------- ACTUAL USER SCRIPT
--//

当我粘贴我的三行而不是 hello world 行时,只有第一行,启动 docker 服务,有效。

我是 Linux 的新手,所以它可能是我遗漏的一些小东西。

谢谢

编辑 谢谢大家的帮助,问题确实是dir APP的路径问题。

entering this to the user data when the instance is stopped should work

遗憾的是第一次尝试不会奏效。这是因为 UserData executes only 当新实例 启动时 .

When I paste my three lines instead of the hello world line, only the first one

第二次尝试失败,因为此脚本在 / 文件夹(文件系统的根目录 )中作为 root 运行。所以除非你的APP在/APP,否则这不行。