13 December 2024

Docker Container Deployment on AWS

Learn to deploy Docker containers on AWS, leveraging services like EC2 or ECS for scalable and efficient application hosting.

thumbnail

Project Details

Docker Container Deployment on AWS

Code Snippets

Dockerfile (Sample Application)

"FROM python:3.9-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 8080
CMD ["python", "app.py"]"

Building and Testing Docker Image

"docker build -t your-app-name .
docker run -p 8080:8080 your-app-name"

Pushing Image to Docker Hub

"docker login
docker tag your-app-name docker-hub-username/your-app-name:latest
docker push docker-hub-username/your-app-name:latest"

Pushing Image to Amazon ECR

"aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <ecr-url>
docker tag your-app-name <ecr-url>/your-app-name:latest
docker push r-url>/your-ap<ecp-name:latest"

Setting Up AWS EC2 Instance

"sudo apt update
sudo apt install docker.io -y
docker pull docker-hub-username/your-app-name:latest
docker run -d -p 80:8080 your-app-name"

AWS ECS Task Definition (JSON Example)

"{
"family": "ecs-task-definition",
"containerDefinitions": [
{
"name": "app-container",
"image": "<ecr-url>/your-app-name:latest",
"memory": 512,
"cpu": 256,
"portMappings": [
{
"containerPort": 8080,
"hostPort": 80,
"protocol": "tcp"
}
]
}
]
}"

ECS Cluster Deployment

"aws ecs create-cluster --cluster-name my-ecs-cluster
aws ecs register-task-definition --cli-input-json file://ecs-task-definition.json
aws ecs run-task --cluster my-ecs-cluster --task-definition ecs-task-definition"