Building an Automated Docker Deployment Script: A Complete Beginner's Guide
Source: Dev.to
Introduction
Have you ever wondered how professional developers deploy their applications to servers automatically? In this comprehensive guide, you’ll walk through creating a powerful Bash script that automates the entire deployment process for Docker‑based applications. By the end of this article, you’ll have a script that can:
- Clone your Git repository
- Set up a remote server environment
- Build and deploy Docker containers
- Configure Nginx as a reverse proxy
- Validate your deployment
- Handle cleanup and log management
Best of all: you’ll understand every step, even if you’re just starting your DevOps journey!
What is Automated Deployment and Why Do We Need It?
In the early days of web development, deploying an application meant manually copying files to a server, installing dependencies, and configuring everything by hand. This process was:
- Time‑consuming – could take hours for a single deployment
- Error‑prone – easy to forget a step or misconfigure something
- Not repeatable – hard to deploy the same way twice
- Not scalable – imagine doing this for 10 different applications!
Automated deployment solves these problems by using scripts to perform all deployment steps consistently and reliably.
Understanding the Deployment Workflow

Before we dive into the code, here’s the big picture of what happens during deployment:
- Your local machine runs the deployment script.
- The script clones your application code from Git.
- It connects to your remote server via SSH.
- It sets up Docker and Nginx on the server.
- It builds your Docker container and runs it.
- It configures Nginx to route traffic to your app.
- It validates everything is working correctly.
Prerequisites
Knowledge
- Linux command‑line basics
- Git version control
- Basic understanding of Docker (we’ll explain as we go)
Technical Requirements
- A Linux/macOS machine (or WSL on Windows)
- A remote server (AWS EC2, DigitalOcean, etc.)
- SSH key access to your server
- A Git repository with your application code
Tip: If you’re new to SSH keys, think of them as a secure password alternative that uses cryptographic keys instead of text passwords.
The 7 Stages of Our Deployment Script

Our deployment script is organized into seven distinct stages. Below is a detailed look at each stage.
Stage 0.5 – Setup and Housekeeping
What it does: Sets up logging, error handling, and cleanup functionality.
Safety Features
#!/usr/bin/env bash
set -o errexit # Exit on any error
set -o nounset # Exit on undefined variable
set -o pipefail # Exit if any command in a pipe fails
errexit: stops the script immediately if any command fails.nounset: catches typos in variable names.pipefail: ensures errors in pipelines aren’t hidden.
Logging System
TIMESTAMP="$(date +%Y%m%d_%H%M%S)"
LOGDIR="./logs"
LOGFILE="${LOGDIR}/deploy_${TIMESTAMP}.log"
log() {
printf "%s [%s] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1" "$2" | tee -a "${LOGFILE}"
}
Creates a timestamped log file for every deployment, making troubleshooting easier.
Cleanup Mode
The script includes a --cleanup flag that removes all deployed containers, images, and configurations—useful for starting fresh.
./deploy.sh --cleanup
Caution: Cleanup mode will remove all Docker containers and images on your server. Always back up important data first.
Stage 1 – Collect Parameters and Basic Validation
What it does: Gathers all information needed for deployment through interactive prompts.
Information Collected
- Git Repository URL (e.g.,
https://github.com/yourusername/your-app.git) - Personal Access Token (PAT) for private repos (entered securely)
- Branch Name (default:
main) - SSH Details – username, host/IP, and private key path
- Application Port (e.g.,
3000for Node.js,8080for many others)
Example Interaction
Git repository URL: https://github.com/john/awesome-app.git
Branch name: main
Remote SSH username: ubuntu
Remote SSH host/IP: 54.123.45.67
Path to local SSH private key: ~/.ssh/deploy-key.pem
Application internal container port: 3000
Input Validation Example
case "${APP_PORT}" in
''|*[!0-9]* ) die 12 "Invalid port";;
esac
Ensures the port is numeric, preventing later errors.
Stage 2 – Repository Clone and Validation
What it does: Downloads your code and verifies connectivity to the server.
Workspace Creation & Clone
WORKDIR="./workspace_${TIMESTAMP}"
mkdir -p "${WORKDIR}"
git clone -b "${BRANCH}" "${GIT_REPO_URL}" "${WORKDIR}/repo"
Creates a timestamped workspace directory and clones the specified branch.
Authentication for Private Repos (HTTPS)
# Inject PAT into the URL securely
AUTH_URL="$(echo "${GIT_REPO_URL}" | sed -E "s#https://#https://${GIT_PAT}@#")"
git clone -b "${BRANCH}" "${AUTH_URL}" "${WORKDIR}/repo"
Authentication for SSH Repositories
GIT_SSH_COMMAND="ssh -i ${SSH_KEY_PATH} -o StrictHostKeyChecking=no" \
git clone -b "${BRANCH}" "${GIT_REPO_URL}" "${WORKDIR}/repo"
Docker Setup Detection
if [ -f "${WORKDIR}/repo/Dockerfile" ]; then
log "INFO" "Found Dockerfile."
elif [ -f "${WORKDIR}/repo/do