This repository contains a production-ready Bash script (deploy.sh) that automates the setup, deployment, and configuration of a Dockerized application on a remote Linux server. It is designed to handle every step from collecting credentials to validating the deployment, with full logging and error handling.
The script is intended to simplify the deployment workflow, ensure repeatable results, and maintain idempotency.
-
Interactive first-time setup to collect and validate environment variables.
-
Supports both HTTPS (with Personal Access Token) and SSH Git repositories.
-
Automatic cloning or pulling of the repository.
-
Detection and validation of
Dockerfileordocker-compose.yml. -
SSH connectivity verification and remote command execution.
-
Remote server preparation:
- Update system packages
- Install Docker, Docker Compose, and Nginx if missing
- Add user to Docker group
- Enable and start services
-
Deployment of Dockerized application using
dockerordocker-compose. -
Transfer of project files to remote server using
rsync. -
Dynamic Nginx reverse proxy configuration.
-
Health checks to ensure container and service availability.
-
Logging of all actions with timestamped log files.
-
Support for optional flags:
--debugfor pausing between steps--cleanupto remove deployed resources--reconfigureto recreate environment variables
- A remote Linux server (Ubuntu/Debian recommended).
- SSH access to the server.
- Git repository URL (HTTPS or SSH) for the application.
- Bash shell locally and on the remote server.
- Optional: Personal Access Token (PAT) for private repositories.
Make the script executable:
chmod +x deploy.shExecute the script:
./deploy.shOptional flags:
--debug— pauses after each major step for visualization.--cleanup— removes deployed containers and Nginx configuration.--reconfigure— re-runs environment variable setup.
Example:
./deploy.sh --debugThe script stores environment configuration in .deploy.env. This includes:
GIT_REPO— Git repository URLPAT— Personal Access Token (for HTTPS private repos)BRANCH— Git branch to deploySSH_USER— SSH username for remote serverSERVER_IP— Remote server IP address or hostnameSSH_KEY— Path to private SSH keyAPP_PORT— Internal container port
- Collect Parameters: Prompts user for Git repo, PAT, branch, SSH credentials, and app port.
- Clone Repository: Clones or updates the local repository and checks out the specified branch.
- Validate Project: Checks for
Dockerfileordocker-compose.yml. - Test SSH: Ensures connectivity to the remote server.
- Prepare Remote Environment: Updates packages and installs required services.
- Transfer & Deploy: Uses
rsyncto transfer project files and runs Docker containers. - Configure Nginx: Sets up a reverse proxy to forward HTTP traffic to the container port.
- Health Checks: Verifies Docker service, running containers, Nginx status, and endpoint availability.
- Logging & Error Handling: All steps logged; traps handle errors and exit codes.
- Idempotency & Cleanup: Ensures safe re-runs and optionally cleans up deployed resources.
All output is logged to a timestamped log file, e.g., deploy_YYYYMMDD_HHMMSS.LOG, located in the same directory as the script.
- The script assumes Ubuntu/Debian for remote server package management. Other distributions require adjusting package installation commands.
- It avoids configuration management tools; everything is handled via Bash and SSH.
.deploy.envis automatically gitignored.