Skip to content

Instantly share code, notes, and snippets.

@christopher-talke
Last active June 17, 2025 00:44
Show Gist options
  • Save christopher-talke/11c655de511dd799a1d9c3cf156e7a94 to your computer and use it in GitHub Desktop.
Save christopher-talke/11c655de511dd799a1d9c3cf156e7a94 to your computer and use it in GitHub Desktop.
Docker Deployment via Remote SSH
#!/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
@christopher-talke
Copy link
Author

christopher-talke commented Jun 5, 2019

High-Level Diagram Overview of Script

alt text

Prerequisites

This deployment script is reliant on 'puzzle pieces' not seen within the script, this includes:

  • Setup and installation of docker on the remote host

Details on how to install docker on a remote host, see here:
https://docs.docker.com/install

  • Setup and configuration of docker image for the repository

Here are some good resources on how to dockerize your image
https://www.youtube.com/watch?v=6aBsjT5HoGY
https://docs.docker.com/engine/examples
https://hackernoon.com/how-to-dockerize-any-application-b60ad00e76da

  • Setup and configuration of NGINX with a Reverse Proxy configuration:

https://hub.docker.com/r/linuxserver/letsencrypt
Please Note You could just use the normal NGINX image, however, you'll need to spend time getting LetsEncrypt setup, which can be a pain if you don't know what you are doing...

  • Setting up an SSH Key with the remote host:
  • Setting up Git / Git Bash on your local machine, see below resources for help

https://gitforwindows.org or https://git-scm.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment