在 EC2 启动时自动启动 docker-compose [英] Start docker-compose automatically on EC2 startup
问题描述
我有一个 linux AMI 2 AWS 实例,其中一些服务是通过 docker-compose 编排的,我正在使用 docker-compose up 或 docker-compose start 命令来启动它们.现在我每天都在自动启动/停止我的 ec2 实例,但是一旦它启动,我想运行一些 ssh 以便能够切换到 docker-compose.yml 文件所在的目录,然后启动它.
I have a linux AMI 2 AWS instance with some services orchestrated via docker-compose, and I am using docker-compose up or docker-compose start commands to start them all. Now I am in the process to start/stop my ec2 instance automatically every day, but once it is started, I want to run some ssh to be able to change to the directory where docker-compose.yml file is, and then start it.
类似:
#!
cd /mydirectory
docker-compose start
我怎样才能做到这一点?
How can I achieve that?
谢谢
推荐答案
我建议为此使用 cron,因为它很容易.大多数玉米支持非标准指令,如@daily
、@weekly
、@monthly
、@reboot
.
I would recommend using cron for this as it is easy. Most of the corn supports non-standard instructions like @daily
, @weekly
, @monthly
, @reboot
.
你可以把它放在一个 shell 脚本中,并在 crontab 中将它安排为 @reboot/path/to/shell/script
或
您可以使用绝对路径指定 docker-compose 文件,并直接在 crontab 中将其调度为 @reboot docker-compose -f/path/to/docker-compose.yml start
You can put this either in a shell script and schedule that in crontab as @reboot /path/to/shell/script
or
you can specify the docker-compose file using the absolute path and directly schedule it in crontab as @reboot docker-compose -f /path/to/docker-compose.yml start
- 创建一个 systemd 服务并启用它.所有已启用的系统服务将在通电时启动.(难度:中等)
- 将脚本放在 init.d 下并将其链接到 rc*.d 目录.这些脚本也是根据优先级启动的.(难度:中等)
奖励:
如果你在 docker-compose 文件中为容器指定重启策略,它会在你重启或打开服务器时自动启动.参考
这篇关于在 EC2 启动时自动启动 docker-compose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!