Deploy Docker Container on AWS Quickly Using Cloud Init
One can use "cloud-config" scripts (executed by "cloud-init") to quickly spin up a minimal Ubuntu instance that will then:
- Install the necessary packages for docker.
- Pull the relevant docker image
- Deploy the image however you like (
docker run ....
)
This makes it very quick to deploy your code, and is a good step on the way to using code to manage your infrastructure.
Steps
First select the Ubuntu 20.04 image from the defaults.

Copy the cloud-config script below...
... and paste it into the user_data field as shown below before deploying:

Then just wait a while :)
If it appears to not work, be sure to log into the EC2 instance and check the logs at /var/log/cloud-init-output.log
Using Docker PPA
If you want to install Docker from the PPA instead, then change the cloud-init config to:
Using A Private Registry
If you are using a private registry to host your docker images, then you need to add some commands to perform a docker login like so:
Using AWS ECR
If you are using AWS's Elastic Container Registry, then you will need to login using IAM credentials like so (because the code to get the password, gets a password that expires)
References
First published: 24th September 2020