在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
如何实现?
谢谢
推荐答案
我建议为此使用cron简单。多数玉米支持非标准指令,例如 @每日
, @每周,
@每月
, @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目录。这些脚本也会根据优先级启动。(难度:中等)
- Create a systemd service and enable it. All the enabled systems services will be started on powering.(difficulty: medium)
- Put scripts under init.d and link it to rc*.d directory. These scripts are also started based on the priority.(difficulty: medium)
奖金:
如果您在docker-compose文件中为容器指定了重新启动策略,则在您重新启动或打开服务器后它将自动启动。 参考
这篇关于在EC2启动时自动启动docker-compose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!