-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_docker.sh
executable file
·137 lines (121 loc) · 4.28 KB
/
setup_docker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
set -eo pipefail
rederror="\033[1;31mERROR:\033[0m"
if [ $EUID -ne 0 ]; then
printf "$rederror This script must be run as root\n"
exit 1
fi
echo "Uninstalling old versions of Docker..."
apt-get remove docker docker.io containerd runc
echo "Installing any necessary dependencies..."
apt-get update > /dev/null
apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common \
pwgen
if [ -z "$(cat /etc/apt/sources.list | grep "download\.docker\.com" || true)" ]; then
echo "Setting up official Docker repository..."
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
if [ -z "$(apt-key fingerprint 0EBFCD88 | grep "0EBF CD88$" || true)" ]; then
echo "$rederror Fingerprint does not match!"
exit 1
fi
add-apt-repository \
"deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
fi
echo "Installing Docker (if necessary)..."
apt-get update > /dev/null
apt-get install docker-ce docker-ce-cli containerd.io
echo "Installing docker-compose..."
# get latest docker compose released tag
compose_latest_ver=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep 'tag_name' | cut -d\" -f4)
compose_curr_ver=$(docker-compose --version | cut -d" " -f3 | sed s/,//g || true)
if [ compose_latest_ver != compose_curr_ver ]; then
curl -L "https://github.com/docker/compose/releases/download/${compose_latest_ver}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version
fi
while [ -z "$name" ]; do
read -p " Site name: " name
done
prefix="/var/www/$name"
if [ ! -d "$prefix" ]; then
read -p "Directory $prefix does not exist. Do you want me to create it? (y/N) " create
if [ "${create,,}" != "y" ]; then
echo "Please create $prefix and then run this script again."
exit 1
else
mkdir -p "$prefix"
fi
fi
wp_container_exists=$(sudo docker ps -a | grep "\b${name}_wordpress" || true)
db_container_exists=$(sudo docker ps -a | grep "\b${name}_db" || true)
if [[ ( -n $wp_container_exists ) && ( -n $db_container_exists ) ]]; then
printf "$rederror There are already Docker containers running for the $name site.\n"
exit 1
fi
while [ -z "$db" ]; do
read -p " Database name: " db
done
while [ -z "$username" ]; do
read -p "MySQL username: " username
done
while [ -z "$password" ]; do
read -sp "MySQL database password: " password; echo ""
read -sp "Re-enter MySQL database password: " rpassword; echo ""
if [[ $password != $rpassword ]]; then
password=""
echo "Passwords do not match. Please try again."
fi
done
while [ -z "$rootpassword" ]; do
read -sp "MySQL root password: " rootpassword; echo ""
read -sp "Re-enter MySQL root password: " rrootpassword; echo ""
if [[ $rootpassword != $rrootpassword ]]; then
rootpassword=""
echo "Passwords do not match. Please try again."
fi
done
ports=$(netstat -lat --numeric-ports | awk '{print $4}' | awk -F: '{print $NF}' | grep -o "\b8[0-9][0-9][0-9]" || true)
if [ -z $ports ]; then
port_no="8000"
else
let port_no="$(printf "$ports" | sort -nr | head -1) + 10"
fi
echo "Generating docker-compose file..."
cat docker-compose-template.yml |
sed "s/8000/$port_no/g" |
sed "s/db_data/$db/g" |
sed "s/db_name/$db/g" |
sed "s/mysql_username/$username/g" |
sed "s/mysql_password/$password/g" |
sed "s/root_password/$rootpassword/g" > "$prefix/docker-compose.yml"
echo "Starting up Docker containers..."
(
cd "$prefix"
containers="$(docker ps --filter name="${name}_*" -aq)"
if [ -n "$containers" ]
then
echo $containers | xargs docker stop | xargs docker rm
else
echo "No containers need to be removed."
fi
docker-compose up -d
while [ "${continue,,}" != "y" ]; do
read -p "On the next screen, you need to enter a secure passphrase. Continue? (y/N) " continue
done
gpg --no-symkey-cache -c docker-compose.yml
rm docker-compose.yml
)
read -p "Would you like to use the local IP address? (y/N) " local_ip
if [ "${local_ip,,}" == "y" ]; then
ip=$(hostname -I | awk '{print $1}')
else
ip=$(curl ifconfig.me)
fi
echo "Your Docker containers are now up and running! Go to http://$ip:$port_no to start setting up WordPress."