Installing Docker and Compose in AL2023 AWS EC2

Installing Docker and Compose in AL2023 AWS EC2

Setting up a EC2 Instance - AL2023

Before starting up a instance let's do some background work.

  1. Create a security group with inbound from the internet for http and https i.e port 80 and 443.
  1. Create a S3 Bucket for DB Backup with default config.
  2. Create a IAM Instance Profile Role with these builtin Policies.

This ensure we can ssh into the instance using session manager from the aws console or from local device.

  1. Create a EC2 Instance with of properties as shown below:
    1. Instance Type: t3.small
    2. AMI: AL2023 i.e Amazon Linux 2023
    3. Root Volume 30GB
    4. Public IP
    5. And select the above created SG and IAM Instance Profile from Advanced.
    6. Create a Key Pair and Attach to the instance.

Logging into the Instance

# Session Manager
aws ssm start-session --target <instance_id> --profile <aws_profile> --region <aws_region>
# e.g command aws ssm start-session --target i-0eaf18d070dfa79ab --profile sandbox --region eu-west-2
# aws_profile - profile configured in the your lactop using aws configure or aws sso.
# aws_region - the region where the instance is running
# instance_id - id of the running instance you want to start a session in
chmod 400 <key>.pem # add permission
ssh -i <key>.pem ec2-user@<elastic_ip> # ssh into the instance
sudo su - ec2-user # switch user

Installing Docker and Docker Compose + Git

# Install Docker
sudo dnf install docker -y
docker --version # check docker version
sudo systemctl status docker # check if docker is up and running
sudo systemctl start docker # start docker service
sudo systemctl enable docker # enable docker service to auto start on reboot
sudo usermod -aG docker $USER # Add Current user to Docker Group
newgrp docker # activate changes or reboot instance

# Install Compose

# create dir for docker plugins
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
mkdir -p $DOCKER_CONFIG/cli-plugins
# download and install compose plugin
curl -SL https://github.com/docker/compose/releases/download/v2.29.1/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
# add executable permission
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
docker compose version # check compose version

sudo dnf install git -y # install git

Happy Reading. Please feel free to leave your feedbacks and show some love which will inspire me to write more.