🚀 Automated Dockerized Application Deployment Script (deploy.sh)
This project provides a robust, production-grade Bash script that automates the setup, deployment, and configuration of a Dockerized application on a remote Linux server.
The script handles:
- Repository cloning and validation
- Remote server provisioning (Docker, Docker Compose, Nginx installation)
- Container deployment and reverse proxy setup
- Logging, error handling, and idempotency (safe re-runs)
It is designed to simulate real-world DevOps workflows, emphasizing automation, reliability, and reusability.
- Securely authenticates with GitHub using a Personal Access Token (PAT)
- Automatically clones or updates repositories
- Installs and configures Docker, Docker Compose, and Nginx on a remote host
- Deploys containers and sets up Nginx reverse proxy for HTTP forwarding
- Logs all actions with timestamps for easy debugging
- Includes error handling, validation, and idempotent logic (safe re-run)
- Optional cleanup flag (
--cleanup) for tearing down previous deployments
Before running the script, ensure you have:
- A Linux operating system (Ubuntu/Debian recommended)
- Bash version 4 or higher
- Git installed locally
- SSH access to a remote Linux server
- A GitHub Personal Access Token (PAT) with
repoaccess - Internet connectivity between your local machine and the remote server
You will be prompted for the following parameters:
- Git Repository URL – e.g.
https://github.com/username/app.git - Personal Access Token (PAT) – your GitHub token for authentication
- Branch Name – defaults to
mainif not provided - SSH Username – the username for your remote server
- Server IP Address – the public IP of your remote host
- SSH Key Path – path to your private SSH key, e.g.
~/.ssh/id_rsa - Application Port – the internal port your app runs on, e.g.
5000
Prompts for required input and validates that no critical fields are empty. Defaults to main branch if none is specified.
Authenticates with GitHub using the PAT, clones the repository, and switches to the correct branch.
If the project folder already exists, it pulls the latest changes instead.
Verifies the presence of a Dockerfile or docker-compose.yml.
Checks that the remote server is reachable via SSH and validates the SSH key provided. Performs a test connection before proceeding.
On the remote server:
- Updates system packages
- Installs Docker, Docker Compose, and Nginx if missing
- Adds the user to the Docker group
- Enables and starts Docker and Nginx services
- Confirms installation versions
- Transfers project files to the remote server (via
scporrsync) - Builds and runs Docker containers using either
docker buildordocker-compose up -d - Checks container health and logs
- Verifies that the app is accessible on the specified port
- Dynamically creates or updates an Nginx configuration file
- Forwards HTTP traffic from port 80 to the app’s internal container port
- Tests and reloads Nginx configuration
- (Optional) Placeholder for SSL setup with Certbot or a self-signed certificate
- Confirms that Docker is running
- Checks that the container is active and healthy
- Tests Nginx proxying using
curlorwgetlocally and remotely
- All actions are logged in a file named
deploy_YYYYMMDD.log - Errors are trapped with
trapand automatically logged before exiting - Each stage has clear success or failure messages
- The script can safely be re-run without breaking existing deployments
- Existing containers are stopped and replaced cleanly
- Duplicate Docker networks or Nginx configs are prevented
- Includes an optional
--cleanupflag to remove all deployed resources
To deploy an application:
chmod +x deploy.sh
./deploy.shYou’ll be prompted to enter details like the Git repository URL, PAT, SSH details, and application port.
To clean up an existing deployment:
./deploy.sh --cleanupThis stops and removes containers, images, and Nginx configurations created by the script.
[2025-10-18 12:42:19] Starting Deployment Script
[2025-10-18 12:42:25] Validating repository URL...
[2025-10-18 12:42:31] Cloning https://github.com/username/app.git
[2025-10-18 12:43:02] SSH connection established to 54.203.12.45
[2025-10-18 12:44:10] Docker and Nginx installed successfully
[2025-10-18 12:45:01] Application deployed and accessible at http://54.203.12.45
.
├── deploy.sh # Main deployment script
├── README.md # Documentation
└── deploy_2025-10-19.log # Example log file (auto-generated)
- Bash – main scripting language
- Git – version control and repo management
- Docker – containerization platform
- Docker Compose – multi-container orchestration
- Nginx – reverse proxy and load balancer
- SSH – secure remote access
- tee – used for logging
- Exit Code 1 – General error
- Exit Code 2 – Invalid user input
- Exit Code 3 – SSH connection failed
- Exit Code 4 – Docker or Nginx installation error
- Exit Code 5 – Deployment failure
All errors are written to the timestamped log file for troubleshooting.
When the --cleanup flag is used:
- Stops and removes running containers
- Deletes container images and volumes
- Removes Nginx configurations created by this script
This project is released under the MIT License. You are free to modify and reuse this project for learning or deployment purposes.