We have a .NET application that has been running for years, but once a week, the application fails to recover, and needs the server needs to be rebooted. To preemptively reboot the EC2 instance nightly we decided to use Docker and ECS scheduled tasks.
Here is what the finished Dockerfile looks like:
ECS has a role with the correct access to run reboot.
Here is what the Python script looks like:
We need to import boto
and os
here. boto
is to do the AWS magic of rebooting the servers and os
to use an environment variable. Here we use a pipe delimiter to target multiple EC2 instances. This will help out while testing the container on your local machine because you can pass in environment variables on the command line using the -e flag. There is probably a more efficient and elegant way to go about this, but this works for us.
Keep these files in the same directory and run:
If it builds successfully, you can try to run it:
This will run your container interactively and drop you into a bash shell. Alternatively, try running with environment variables passed in.
If all of this is working as expected, you can go to ECS in AWS and create your task definition. It will provide you with commands to push your image to ECR.
Maybe I'll add some screenshots here.
After that, you might find you have some extra docker images and containers to clean up locally. The following commands should help. Consult the Docker documentation for more information.
for i in ```docker images | grep '<none>'| awk '{ print $3 }'```; do docker rmi -f $i; done
for i in `docker container ls --all | awk '{ print $1 }'`; do docker container rm $i; done