Skip to content

Instantly share code, notes, and snippets.

@jasonmccallister
Last active January 21, 2018 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonmccallister/3299b52b973219b753e8596c91460acf to your computer and use it in GitHub Desktop.
Save jasonmccallister/3299b52b973219b753e8596c91460acf to your computer and use it in GitHub Desktop.
Quick bash script to setup Docker and Docker Swarm on Ubuntu 16.04
#!/usr/bin/env bash
# O1 - BASIC USER AND SSH SETUP
# ask for the new username
read -p 'Enter your new username here: ' NEWUSER
# set the path for the user
USERSSHPATH=/home/$NEWUSER/.ssh
# creates the user, you will be promted for a password
adduser $NEWUSER && usermod -aG sudo $NEWUSER
# make a .ssh directory for the user
mkdir $USERSSHPATH && chmod 700 $USERSSHPATH && chown -R $NEWUSER:$NEWUSER $USERSSHPATH
# ask for the users SSH key
read -sp 'Enter your SSH key here: ' USERKEY
# put the SSH key and set permissions
echo $USERKEY >> $USERSSHPATH/authorized_keys && chmod 600 $USERSSHPATH/authorized_keys
# modify permissions on the ssh authorized keys
chown $NEWUSER:$NEWUSER $USERSSHPATH/authorized_keys
# 02 - SECURITY
# setup the firewall
ufw allow http https OpenSSH
ufw enable
# 03 - DOCKER SETUP
# setup docker with dependencies
apt-get update && apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D && apt-add-repository 'deb https://apt.dockerproject.org/repo ubuntu-xenial main' && apt-get update
# set the policy to install from new repo and install
apt-cache policy docker-engine && apt-get install -y docker-engine
# add user to the docker group to run commands
usermod -aG docker $NEWUSER
# setup the firewall
ufw allow http https OpenSSH
ufw enable
# isntall docker swarm and set the IP
# use the public IP
# IP=$(/sbin/ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}')
# use the internal IP
# IP=$(/sbin/ifconfig eth1 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}')
# private ip for packet.net
IP=$(/sbin/ifconfig bond0:0 | grep 'inet addr' | cut -d: -f2 | awk '{print $1}')
docker swarm init --advertise-addr $IP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment