简体   繁体   English

EC2启动时自动启动docker-compose

[英]Start docker-compose automatically on EC2 startup

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.我有一个 linux AMI 2 AWS 实例,其中一些服务通过 docker-compose 编排,我正在使用 docker-compose up 或 ZBAEDB53E8405AE71AEZ1394 命令来启动它们。 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.现在我每天都在自动启动/停止我的 ec2 实例,但是一旦启动,我想运行一些 ssh 以便能够更改到 docker-compose.yml 文件所在的目录,然后启动它。

something like:就像是:

#!
cd /mydirectory
docker-compose start

How can I achieve that?我怎样才能做到这一点?

Thanks谢谢

I would recommend using cron for this as it is easy.我建议为此使用 cron,因为它很容易。 Most of the corn supports non-standard instructions like @daily , @weekly , @monthly , @reboot .大多数玉米都支持非标准指令,如@daily@weekly@monthly 、@ @reboot

You can put this either in a shell script and schedule that in crontab as @reboot /path/to/shell/script您可以将其放在 shell 脚本中,并在 crontab 中将其安排为@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您可以使用绝对路径指定 docker-compose 文件,并直接在 crontab 中将其调度为@reboot docker-compose -f /path/to/docker-compose.yml start

Other possibilities:其他可能性:

  1. Create a systemd service and enable it.创建一个 systemd 服务并启用它。 All the enabled systems services will be started on powering.所有启用的系统服务将在通电时启动。 (difficulty: medium) (难度:中等)
  2. Put scripts under init.d and link it to rc*.d directory.将脚本放在 init.d 下并将其链接到 rc*.d 目录。 These scripts are also started based on the priority.这些脚本也是根据优先级启动的。 (difficulty: medium) (难度:中等)

Bonus:奖金:

If you specify restart policy in the docker-compose file for a container it will autostart if you reboot or switch on the server.如果您在 docker-compose 文件中为容器指定重新启动策略,则在您重新启动或打开服务器时它将自动启动。 Reference参考

Consider using Amazon Elastic Container Service (Amazon ECS) which can orchestrate docker containers and take care of your underlying OSes.考虑使用 Amazon Elastic Container Service (Amazon ECS),它可以编排 docker 容器并照顾您的底层操作系统。

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

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