Prepare docker Image
Create a python script
Create file demp.py in a new directory LightningTalk and add code in it.
Create a docker file
Create a file docker file in same directory
Add following code in Dockerfile
WORKDIR /home/harsh/workplace/LightningTalk
COPY requirements.txt ./
RUN pip install –no-cache-dir -r requirements.txt
COPY . .
CMD [ “python”, “./demo.py” ]
We are using python base image from ECR Public Gallery https://gallery.ecr.aws/docker/library/python/?page=1
Build Docker Image
Run container for testing
Create ECR Registry and push image to ECR
Run the get-login-password command to authenticate the Docker CLI to your Amazon ECR registry.
After you have authenticated to an Amazon ECR registry with this command, you can use the client to push and pull images from that registry
Create a repository in Amazon ECR using the create-repository command.
Run the docker tag command to tag your local image into your Amazon ECR repository as the latest version. Copy the repositoryUri from the output in the previous step.
Run the docker push command to deploy your local image to the Amazon ECR repository. Make sure to include :latest at the end of the repository URI.
Create ECS Resources and run the task
Create a new cluster
Register a Task Definition
Before you can run a task on your ECS cluster, you must register a task definition. Task definitions are lists of containers grouped together.
Before running above command we need to save the task definition JSON as a file fargate-task.json.
“family”: “python-tasks”,
“containerDefinitions”: [
{
“name”: “lightning-talk”,
“image”: “$AWSACCOUNTID.dkr.ecr.us-east-1.amazonaws.com/python-images:latest”,
“cpu”: 256,
“memory”: 2048,
“portMappings”: [
{
“containerPort”: 80,
“hostPort”: 80,
“protocol”: “tcp”
}
],
“essential”: true,
“environment”: [],
“environmentFiles”: [],
“mountPoints”: [],
“volumesFrom”: [],
“ulimits”: [],
“logConfiguration”: {
“logDriver”: “awslogs”,
“options”: {
“awslogs-group”: “/ecs/python-tasks”,
“awslogs-create-group”: “true”,
“awslogs-region”: “us-east-1”,
“awslogs-stream-prefix”: “ecs”
},
“secretOptions”: []
},
“systemControls”: []
}
],
“executionRoleArn”: “arn:aws:iam::$AWSACCOUNTID:role/ecsTaskExecutionRole”,
“networkMode”: “awsvpc”,
“requiresCompatibilities”: [
“FARGATE”
],
“cpu”: “256”,
“memory”: “2048”
}
Running a task
Run following task
To get the values of subnet and subnet we can use values from default VPC and default security group.