-
Notifications
You must be signed in to change notification settings - Fork 18
/
docker-setup
executable file
·135 lines (113 loc) · 3.73 KB
/
docker-setup
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
#!/bin/bash -e
# Copyright (c) 2011-2015 TurnKey GNU/Linux - http://www.turnkeylinux.org
#
# This file is part of buildtasks.
#
# Buildtasks is free software; you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
fatal() { echo "FATAL [$(basename $0)]: $@" 1>&2; exit 1; }
warning() { echo "WARNING [$(basename $0)]: $@"; }
info() { echo "INFO [$(basename $0)]: $@"; }
usage() {
cat<<EOF
Syntax: $(basename $0)
Setup/update system ready for bt-docker
Environment::
BT_DEBUG turn on debugging
BT_DOCKER_USER used in dockercfg
BT_DOCKER_PASS used in dockercfg
BT_DOCKER_MAIL used in dockercfg
EOF
exit 1
}
export BT=$(dirname $(dirname $(readlink -f $0)))
export BT_CONFIG=$BT/config
[[ ! -f "$BT_CONFIG/docker.cfg" ]] || . $BT_CONFIG/docker.cfg
while [ "$1" != "" ]; do
case $1 in
--help|-h ) usage;;
*) usage;;
esac
shift
done
[ -n "$BT_DOCKER_USER" ] || fatal "BT_DOCKER_USER not set"
[ -n "$BT_DOCKER_PASS" ] || fatal "BT_DOCKER_PASS not set"
[ -n "$BT_DOCKER_MAIL" ] || fatal "BT_DOCKER_MAIL not set"
[ -n "$BT_DEBUG" ] && set -x
install() {
apt-get -qq update
DEBIAN_FRONTEND=noninteractive apt-get -y install $1
}
unset pkgs
reqd_binaries="lxc-create xz dirmngr"
for binary in $reqd_binaries; do
if ! which $binary >/dev/null; then
if [[ "$binary" == "lxc-create" ]]; then
pkgs="$pkgs lxc"
elif [[ "$binary" == "xz" ]]; then
pkgs="$pkgs xz-utils"
else
pkgs="$pkgs $binary"
fi
fi
done
info "installing $pkgs"
install "$pkgs"
if ! grep -qs /sys/fs/cgroup /etc/fstab; then
info "setting up cgroups"
echo "none /sys/fs/cgroup cgroup defaults 0 0" >> /etc/fstab
mount /sys/fs/cgroup || warning "mounting cgroups failed, if docker fails to run you may need to reboot"
fi
if [ ! -e /etc/apt/sources.list.d/docker.list ]; then
info "importing docker repo key and creating docker apt repo entry"
GPG_FINGERPRINT=9DC858229FC7DD38854AE2D88D81803C0EBFCD88
gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys $GPG_FINGERPRINT
gpg --output /usr/share/keyrings/docker.gpg --export $GPG_FINGERPRINT
cat > /etc/apt/sources.list.d/docker.list <<EOF
deb [signed-by=/usr/share/keyrings/docker.gpg ,arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable
EOF
REPO_ORIGIN="apt.dockerproject.org"
cat > /etc/apt/preferences.d/docker<<EOF
Package: *
Pin: origin "$REPO_ORIGIN"
Pin-Priority: 100
Package: docker-ce
Pin: origin "$REPO_ORIGIN"
Pin-Priority: 500
EOF
fi
if ! which docker >/dev/null; then
info "installing docker"
install docker-ce \
|| warning "docker-ce install exited non-zero - trying to proceed regardless."
fi
if [ ! -e /var/lib/docker ]; then
info "mount binding /var/lib/docker to ephemeral storage"
mkdir -p /var/lib/docker
mkdir -p /mnt/var/lib/docker
mount --bind /mnt/var/lib/docker /var/lib/docker
fi
if [ ! -e /root/.dockercfg ]; then
info "creating docker auth config"
cat >/root/.dockercfg<<EOF
{
"https://index.docker.io/v1/": {
"auth": "$(echo -n $BT_DOCKER_USER:$BT_DOCKER_PASS | base64)",
"email": "$BT_DOCKER_MAIL"
}
}
EOF
fi
if ! pidof docker >/dev/null; then
info "starting docker daemon"
service docker start
else
info "restarting docker daemon"
service docker restart
fi
exit_code=0
info "testing docker"
docker run hello-world \
|| fatal "Docker hello-world test failed for user: '$BT_DOCKER_USER'; email: '$BT_DOCKER_MAIL'; using password: '$BT_DOCKER_PASS'."