Install Docker on Your EC2 in Sixty Seconds or Less

If you're a fan of docker (like me), you package all your code into docker images, then run them in the cloud in ECS or in an EC2 instance.

AWS ECS is designed to run docker images, but a EC2's (specifically the Amazon Linux2 images) doesn't have docker installed out of the box. 

Here's a script that will install docker in less than 60 seconds on an Amazon Linux2 AMI.  It also installs docker-compose, so you can run compose files.

#####################################################################################################################################################
# install docker & configure
sudo amazon-linux-extras install docker -y
#start docker
sudo service docker start
# make sure it statys running
sudo chkconfig docker on
#Add the ec2-user to the docker group so you can execute Docker commands without using sudo.
sudo usermod -a -G docker ec2-user
#get the latest docker-compose program
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
#fix permissions
sudo chmod +x /usr/local/bin/docker-compose

# create a link to add docker-compose to the path
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

# / docker install
#####################################################################################################################################################

You can run this script directly in the EC2 or package it up as part of your user-data and it will install automatically when the new instance is launched.

- Enjoy 😀

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.