Skip to content

Instantly share code, notes, and snippets.

@ashishjullia
Last active September 25, 2023 20:05
Show Gist options
  • Save ashishjullia/8430290f8b7d31a7cb80946150b38fb4 to your computer and use it in GitHub Desktop.
Save ashishjullia/8430290f8b7d31a7cb80946150b38fb4 to your computer and use it in GitHub Desktop.
docker-build-and-ecs-task-sript
#!/bin/bash
# Set your AWS region and ECR repository name
AWS_REGION="<region>"
ECR_REPOSITORY="<repo-name>"
MYSQL_HOST="dummy-value"
MYSQL_PORT="dummy-value"
MYSQL_USER="dummy-value"
MYSQL_DATABASE="dummy-value"
MYSQL_PASSWORD="dummy-value"
# Set the image name and tag
IMAGE_NAME="<image-name>"
IMAGE_TAG="latest"
# Build the Docker image
docker build \
--build-arg MYSQL_HOST=$MYSQL_HOST \
--build-arg MYSQL_PORT=$MYSQL_PORT \
--build-arg MYSQL_USER=$MYSQL_USER \
--build-arg MYSQL_DATABASE=$MYSQL_DATABASE \
--build-arg MYSQL_PASSWORD=$MYSQL_PASSWORD \
-t $IMAGE_NAME .
# Get the ECR login command
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin <account-name>.dkr.ecr.$AWS_REGION.amazonaws.com
# Log in to ECR
# eval $ECR_LOGIN
# Tag the image with ECR repository information
ECR_IMAGE_URI="<account-name>.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPOSITORY:$IMAGE_TAG"
docker tag $IMAGE_NAME:latest $ECR_IMAGE_URI
# Push the image to ECR
docker push $ECR_IMAGE_URI
# Clean up local Docker images (optional)
# docker rmi $IMAGE_NAME:latest $ECR_IMAGE_URI
aws ecs run-task \
--launch-type FARGATE \
--cluster <cluster-name> \
--network-configuration "awsvpcConfiguration={subnets=[subnet-<>,subnet-<>,subnet-<>],securityGroups=[sg-<>]" \
--platform-version LATEST \
--task-definition arn:aws:ecs:ca-central-1:<account-number>:task-definition/<task-definition>:3 \
--count 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment