fgonzalez
fgonzalez

Reputation: 3877

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. 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.

something like:

#!
cd /mydirectory
docker-compose start

How can I achieve that?

Thanks

Upvotes: 3

Views: 3906

Answers (3)

mfittko
mfittko

Reputation: 421

Simply run the following command once on the host:

sudo systemctl enable docker

Afterwards the restart: always inside of your service in docker-compose.yml should start working.

Upvotes: 2

Ziolek
Ziolek

Reputation: 182

Consider using Amazon Elastic Container Service (Amazon ECS) which can orchestrate docker containers and take care of your underlying OSes.

Upvotes: 2

Mani
Mani

Reputation: 5648

I would recommend using cron for this as it is easy. Most of the corn supports non-standard instructions like @daily, @weekly, @monthly, @reboot.

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

Other possibilities:

  1. Create a systemd service and enable it. 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. 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. Reference

Upvotes: 3

Related Questions