A production-grade Bash script for automated deployment of Dockerized applications to remote Linux servers.
- ✅ Automated Git repository cloning with PAT authentication
- ✅ Remote server environment setup (Docker, Docker Compose, Nginx)
- ✅ Docker container deployment and management
- ✅ Nginx reverse proxy configuration
- ✅ Comprehensive logging and error handling
- ✅ Idempotent deployment (safe for re-runs)
- ✅ Resource cleanup functionality
- ✅ Deployment validation and health checks
- ✅ Security headers and best practices
- Bash 4.0+
- SSH access to remote server with key-based authentication
- Git Personal Access Token (PAT) with repo access
- Remote server: Ubuntu 18.04+ or CentOS 7+ (tested on Ubuntu 20.04+)
- Sudo privileges on the remote server
- Download the script:
wget https://raw.githubusercontent.com/your-username/your-repo/main/deploy.sh- Make it executable:
chmod +x deploy.sh./deploy.sh./deploy.sh --cleanup./deploy.sh --help./deploy.sh --versionWhen you run the script, it will prompt for:
| Parameter | Description | Default |
|---|---|---|
| Git Repository URL | HTTPS URL of your Git repository | Required |
| Personal Access Token | GitHub/GitLab token with repo access | Required |
| Branch name | Git branch to deploy | main |
| SSH username | Username for remote server | Required |
| Server IP address | IP of your deployment server | Required |
| SSH key path | Path to SSH private key | ~/.ssh/id_rsa |
| Application port | Internal container port | 3000 |
- Validates all input parameters
- Clones/Pulls the Git repository using PAT authentication
- Verifies Docker configuration files (Dockerfile or docker-compose.yml)
- Sets up comprehensive logging
- Tests SSH connectivity to the server
- Updates system packages and installs dependencies
- Installs Docker, Docker Compose, and Nginx
- Transfers project files to the server via SCP
- Builds and runs Docker containers
- Configures Nginx as a reverse proxy
- Validates the entire deployment
- ✅ Docker service status
- ✅ Container health and logs
- ✅ Nginx configuration syntax
- ✅ Application responsiveness on specified port
- ✅ End-to-end deployment testing
- 🔐 PAT used only for Git authentication (not stored permanently)
- 🔑 SSH keys for secure server access
- 🛡️ Security headers in Nginx configuration
- 🚫 Common exploit protection (blocks .env, .git access)
- 🧹 Temporary credentials cleanup
- 📝 Comprehensive activity logging
The script creates timestamped log files: deploy_YYYYMMDD_HHMMSS.log
Logs include:
- All user interactions and input
- Command execution results and timing
- Error messages and stack traces
- Deployment validation results
- Cleanup operations
The script is designed to be safe for multiple runs:
- 🔄 Existing repositories are updated via
git pull - 🛑 Old containers are stopped before new deployment
- 📝 Nginx configuration is cleanly overwritten
- 🧹 Failed deployments can be safely retried
- 🔧 Partial failures don't break subsequent runs
-
SSH Connection Failed
- Verify SSH key permissions:
chmod 600 your-key.pem - Check server accessibility:
ping your-server-ip - Ensure SSH service is running on the server
- Verify SSH key permissions:
-
Git Clone Failed
- Verify PAT has repository access
- Check repository URL format
- Ensure branch exists
-
Docker Build Failed
- Check Dockerfile syntax
- Verify network connectivity for Docker images
- Review container logs:
docker logs container-name
-
Application Not Accessible
- Verify port mappings
- Check application logs
- Test locally:
curl http://localhost:APP_PORT
Add set -x at the top of the script for detailed debug output.
✓ Deployment completed successfully!
Application Information:
-----------------------
URL: http://192.168.1.100
App Port: 3000
Server: ubuntu@192.168.1.100
Project: my-awesome-app
Log File: deploy_20241021_143022.log
Next steps:
1. Test the application at: http://192.168.1.100
2. Check logs if needed: deploy_20241021_143022.log
3. To cleanup, run: ./deploy.sh --cleanup
.
├── deploy.sh # Main deployment script
├── deploy_20241021_143022.log # Example log file
└── README.md # This file
- The script requires sudo privileges on the remote server for package installation
- Assumes Ubuntu/Debian package management (apt-get)
- Docker containers must expose the specified application port
- Includes 15-second wait for container health checks
- Self-signed SSL certificates are not included (but ready for Certbot)
For issues and questions:
- Check the deployment logs first:
cat deploy_*.log - Verify all input parameters are correct
- Test SSH connectivity manually
- Check server resources (disk space, memory)
This script is provided as part of the HNG Internship DevOps track.
Ready to deploy? Run ./deploy.sh and follow the prompts! 🚀