Dockerized Continuous Deployment on AWS: Automating Delivery with Bash


In today's fast-paced development environments, automation and rapid delivery are crucial for meeting competitive demands. Combining Docker, AWS, and Bash scripting allows you to create a highly efficient continuous deployment (CD) pipeline that simplifies application delivery. This post explores how to automate deployment workflows using Docker containers and Bash scripts in an AWS environment.


Why Continuous Deployment?

Continuous Deployment (CD) bridges the gap between development and operations by automating software release processes. It ensures that every code change, once committed and tested, is automatically deployed to production, resulting in:

  • Faster time-to-market

  • Reduced human errors

  • Greater consistency across environments


Why Docker for Deployment?

Docker packages your application and its dependencies into a container, making it portable and consistent across development, staging, and production environments. This containerization ensures:

  • Environment consistency

  • Faster startup and rollback

  • Simplified scaling on AWS ECS, EKS, or EC2


Prerequisites

Before building your Dockerized CD pipeline, ensure the following:

  • An AWS account with access to services like EC2, ECS, or EKS

  • A Dockerized application with a Dockerfile

  • AWS CLI configured with IAM permissions

  • Bash is installed on your CI/CD runner or deployment machine

  • (Optional) GitHub Actions, GitLab CI, Jenkins, or any other CI tool


Sample Bash Script for Dockerized Deployment on AWS EC2

Here’s a simplified example of a Bash script to build and deploy your Docker container:


#!/bin/bash


# Variables

APP_NAME="my-app"

AWS_REGION="us-east-1"

IMAGE_TAG="latest"

EC2_INSTANCE="ec2-user@ec2-XX-XX-XX-XX.compute-1.amazonaws.com"

DOCKERFILE_PATH="."


# Step 1: Build Docker Image

docker build -t $APP_NAME:$IMAGE_TAG $DOCKERFILE_PATH


# Step 2: Tag and Push to Amazon ECR (if using ECR)

aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.$AWS_REGION.amazonaws.com

docker tag $APP_NAME:$IMAGE_TAG <aws_account_id>.dkr.ecr.$AWS_REGION.amazonaws.com/$APP_NAME:$IMAGE_TAG

docker push <aws_account_id>.dkr.ecr.$AWS_REGION.amazonaws.com/$APP_NAME:$IMAGE_TAG


# Step 3: SSH into EC2 and deploy container

ssh -i my-key.pem $EC2_INSTANCE << EOF

  docker pull <aws_account_id>.dkr.ecr.$AWS_REGION.amazonaws.com/$APP_NAME:$IMAGE_TAG

  docker stop $APP_NAME || true

  docker rm $APP_NAME || true

  docker run -d --name $APP_NAME -p 80:80 <aws_account_id>.dkr.ecr.$AWS_REGION.amazonaws.com/$APP_NAME:$IMAGE_TAG

EOF



Automating with Git Hooks or CI Tools

You can trigger this Bash deployment script automatically using:

  • Git hooks (e.g., post-merge)

  • GitHub Actions for pushing on merge

  • Jenkins or GitLab CI will run it on each successful pipeline.


Best Practices

  • Use environment variables instead of hardcoded values.

  • Secure sensitive information with AWS Secrets Manager.

  • Use Docker Compose for multi-container orchestration.

  • Implement health checks and rollback mechanisms.

  • Logging and monitoring are integrated using CloudWatch or the ELK stack.


Benefits of This Approach

  • Lightweight and easy to understand

  • Doesn’t require large CI/CD tools to get started

  • Portable across cloud providers or hybrid deployments

  • Compatible with infrastructure-as-code tools like Terraform


Conclusion

With Docker, AWS, and a little Bash, you can build a robust yet straightforward continuous deployment pipeline. This setup is ideal for startups, hobby projects, and even production systems where simplicity and control are paramount.


Comments

Popular posts from this blog

HTTP Basic vs API Key Auth: Best Practices for Secure API Development

ECS Deployment Best Practices: Blue/Green with CodePipeline and CodeDeploy

AWS Console Not Loading? Here’s How to Fix It Fast

YouTube Channel