A production-grade Bash script that automates the setup, deployment, and configuration of Dockerized applications on remote Linux servers, with a focus on AWS EC2 instances.
- Complete Deployment Pipeline: Handles every aspect of deployment from git clone to Nginx configuration
- Robust Error Handling: Comprehensive validation and error recovery mechanisms
- Detailed Logging: Full audit trail of all operations with timestamps
- AWS EC2 Optimized: Special handling for common EC2 connection issues
- Idempotent Execution: Safe to run multiple times without breaking existing setups
- Cleanup Mode: Optional flag to remove all deployed resources
- Custom SSH Port: Support for non-standard SSH ports
- Bash shell environment
- Git
- SSH client
- rsync
- Docker and Docker Compose (on the remote server)
# Make the script executable
chmod +x deploy.sh
# Run the deployment
./deploy.sh# Using non-standard SSH port (e.g., 2222)
./deploy.sh --port 2222# Remove all deployed resources
./deploy.sh --cleanup./deploy.sh --helpThe script will interactively prompt for the following parameters:
| Parameter | Description | Example |
|---|---|---|
| Git Repository URL | URL of the git repository to deploy | https://github.com/username/repo.git |
| Personal Access Token (PAT) | Authentication token for Git repository | ghp_xxxxxxxxxxxxxxxxxxxx |
| Branch Name | Git branch to deploy (defaults to main) | main |
| SSH Username | Username for SSH login to remote server | ubuntu |
| Server IP | IP address of the remote server | 12.34.56.78 |
| SSH Key Path | Path to the SSH private key | ~/.ssh/id_rsa |
| Application Port | Internal port the application uses | 3000 |
| SSH Port (optional) | Custom SSH port if not using default 22 | 2222 |
- Parameter Collection: Validates all required inputs
- Git Operations: Clones or updates the repository
- SSH Connection: Tests and diagnoses connectivity to the remote server
- Environment Preparation: Installs Docker, Docker Compose, and Nginx
- Application Deployment: Transfers files and builds/runs containers
- Nginx Configuration: Sets up reverse proxy to the application
- Validation: Confirms all components are working correctly
- Automatically detects common EC2 SSH issues
- Suggests correct usernames based on AMI type
- Provides security group troubleshooting guidance
- Handles key permission requirements
- Supports custom SSH ports for enhanced security
The script creates a detailed log file with timestamp in the format:
deploy_YYYYMMDD_HHMMSS.log
Each log entry includes:
- Timestamp
- Log level (INFO, SUCCESS, WARN, ERROR)
- Detailed message
If you encounter SSH connection issues:
- Check Security Group: Ensure port 22 (or your custom SSH port) is open
- Verify Instance Status: Confirm your EC2 instance is running
- Key Permissions: Ensure your SSH key has correct permissions (chmod 400)
- AMI Username: Use the correct username for your AMI:
- Amazon Linux:
ec2-user - Ubuntu:
ubuntu - Debian:
admin - RHEL:
ec2-userorroot
- Amazon Linux:
- Run Verbose SSH:
ssh -vvv -i your_key.pem user@ip
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Invalid input parameters |
| 2 | Git operation failed |
| 3 | SSH connection failed |
| 4 | Docker operation failed |
| 5 | Nginx configuration failed |
| 6 | Validation error |
| 130 | Script interrupted by user |
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
baydre_africa | HNGi13 DevOps Team