2026-11-01

docker

#Dockerfile
FROM python:3.11-alpine
WORKDIR /www
COPY index.html .
EXPOSE 8080
CMD ["python3", "-m", "http.server", "8080"]
--------------------------------------------

export AWS_PROFILE=iamuser

#!/bin/bash
AWS_ACCOUNT_ID="123456789012"
REGION="us-east-1"
REPO_NAME="lightweight"

aws ecr create-repository --repository-name $REPO_NAME --region $REGION
nano ~/.docker/config.json
aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com

sudo usermod -aG docker $USER
newgrp docker
-----------------------------------------


#!/bin/bash
AWS_ACCOUNT_ID="123456789012"
REGION="us-east-1"
REPO_NAME="lightweight"

# Build container
docker build --no-cache -t $REPO_NAME .
docker rm -f my-web-server 2>/dev/null || true
docker run -d -p 8080:8080 --name my-web-server $REPO_NAME

sleep 2
if docker ps | grep -q my-web-server; then
    echo "Container is running successfully!"
    curl http://localhost:8080
    echo ""
    
    # Push to ECR
    echo "Pushing to AWS ECR..."
    aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com
    docker tag $REPO_NAME:latest $AWS_ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$REPO_NAME:latest
    docker push $AWS_ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$REPO_NAME:latest
    echo "Successfully pushed to ECR!"
else
    echo "Container failed to stay up. Check logs:"
    docker logs my-web-server
fi

------------------
pull and run:

chmod 400 "keyPair.pem"
ssh -i "keyPair.pem" ec2-user@

sudo apt-get update -y

#docker
sudo apt-get install -y docker.io
sudo systemctl enable --now docker
sudo systemctl start docker
sudo usermod -aG docker $USER
newgrp docker

#aws cli
sudo apt update && sudo apt install -y curl unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
#assign role to ec2
aws sts get-caller-identity

aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com
docker pull $AWS_ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/lightweight-web:latest
docker run -d -p 8080:8080 --name my-web-server $AWS_ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/lightweight-web:latest
curl http://localhost:8080

No comments: