Last active
December 15, 2022 08:15
-
-
Save ekky1328/11c655de511dd799a1d9c3cf156e7a94 to your computer and use it in GitHub Desktop.
Docker Deployment via Remote SSH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Basic Script to Automate Docker Deployment | |
# - Details for how this script works, see here: | |
# - https://gist.github.com/christopher-talke/11c655de511dd799a1d9c3cf156e7a94#gistcomment-2935112 | |
# Created By: christopher.talke <[email protected]> | |
##### VARIABLES SECTION ##### | |
# Details for Host & Docker Network, example below: | |
# user=root | |
# host=112.114.7.123 OR subdomain.domain.com | |
# networkName=local-connect-app1 | |
user=<REMOTEUSER> | |
host=<IP OR DNS NAME> | |
networkName=<DOCKER NETWORK NAME> | |
# Details for App, example below: | |
# org=acme | |
# imageName=app1 | |
# APPShortHand=acme-app1 | |
org=<ORGNAME> | |
imageName=<APP IMAGENAME> | |
APPshortHand=<SHORTHAND FOR CONTAINER> | |
# Details for Reverse Proxy, example below: | |
# reverseProxyName=acme-cont-rp01 | |
# RPshortHand=acme-rp01 | |
reverseProxyName=<REVERSE PROXY NAME> | |
RPshortHand=<SHORTHAND FOR RP> | |
######################## | |
##### SCRIPT START ##### | |
######################## | |
# Compress Folder Contents (uses .gitignore values) | |
git archive -o app.tar.gz master | |
# SSH and create deployment folder in the users root '~/' | |
ssh $user@$host "mkdir -p auto-deploy" | |
# Transfer Files to said folder '~/auto-deploy' | |
scp app.tar.gz .env $user@$host:~/auto-deploy | |
# Connect to Server | |
ssh $user@$host << EOF | |
cd ~/auto-deploy | |
tar xvzf app.tar.gz | |
# Creates internal network that will be used to connect the RP and APP | |
docker network create --internal $networkName | |
# Build, Remove and Deploy Container | |
docker build -t $org/$imageName:latest . | |
docker container rm -f $imageName\_prod | |
docker run \ | |
--name $imageName\_prod \ | |
-dit \ | |
--restart unless-stopped \ | |
$org/$imageName:latest | |
# Connects App to Reverse Proxy via Network 'local-connect' | |
docker network connect \ | |
--link $imageName\_prod:$APPshortHand \ | |
$networkName \ | |
$reverseProxyName | |
# Connects Reverse Proxy to App via Network 'local-connect' | |
docker network connect \ | |
--link $reverseProxyName:$RPshortHand \ | |
local-connect \ | |
$imageName\_prod | |
# Cleanup Files | |
rm -r ~/auto-deploy/* | |
# Check Docker | |
docker ps | |
# Disconnect | |
exit | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
High-Level Diagram Overview of Script
Example of Variables in a Production App
Here is a example of how I use the variables section in a production deployment...
Missing Puzzle Pieces
This deployment script is reliant on 'puzzle pieces' not seen within the script, this includes:
.env
files with your source code, if you have multiple environments or dynamic secrets you need to use environment variables will probably come in handy. Here is some useful resources to wrap your head around thisTo Do
--env=staging || --env=production
option to deploy multiple environments--silent
option to silence output--help
flag, might not be necessary, but would be nice...