Installing a docker image in EC2 and pushing it to ECR
Launching EC2 instance
Using AWS Management Console, launch an EC2 instance. (In this tutorial, Amazon Linux 2 is used)
Installing Docker in the instance
SSH into the instance and run the following commands.(Optionally, these can be included in user data while launching an instance)
yum update -y
amazon-linux-extras install docker
service docker start
usermod -a -G docker ec2-user
chkconfig docker on
Configuring the Container
# docker run
docker run -it — rm amazonlinux:2 bash
yum update -y
(amazonlinux:2 — name and tag for the docker image)
Installing Python 3.8
amazon-linux-extras install python3.8 -y
yum install -y python38-devel
pip3.8 install — upgrade pip
pip install upgrade — setuptools# run
python3.8
# venv
cd /tmp
python3.8 -m venv venv
source venv/bin/activate
Creating Repository in Amazon ECR
- In the AWS Management console search for Amazon ECR.
- Click on the Elastic Container Registry option to go to the Container Services Console.
- Select Create Repositories to create a repo.
Pushing the docker image to the ECR repo
- Click on View Push Commands
- Follow the instructions and execute commands using AWS CLI. For an already built docker, replace the image name as required.
Problem faced:
- An error occurred (AccessDeniedException) when calling the GetAuthorizationToken operation: This problem was due to the Multi-Factor Authentication that was enabled in the AWS account.
Solution:
- It was solved by generating a session token and then configuring the .aws/credentials in the server to include the session token. The command to generate aws_session_token is:
aws sts get-session-token \
— serial-number “arn of MFA device” \
— token-code <MFA code>