To install Docker on AMI (Amazon Linux 2) running on either an EC2 or Lightsail instance:
1. Update the packages and package cache installed on your instance.
sudo yum update -y
2. Install the latest Docker Community Edition package.
sudo yum install docker -y
3. Start the Docker service.
sudo service docker start
4. Add the ec2 user to the docker group so that you can run docker commands without using sudo.
sudo usermod -a -G docker ec2-user
5. Make sure the ec2 user can run Docker commands without sudo.
docker --version or docker info
Docker is now installed successfully. Let’s play with Docker …
Note:
Finally, we get to install Docker on AMI Instance. Now, You may need to restart your instance to grant ec2-user rights to access the Docker daemon. If you see the following error, try restarting your instance.
Unable to connect to the Docker daemon. Is the Docker daemon running on this host?