forked from meta-debian/meta-debian-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-docker-image.sh
executable file
·140 lines (129 loc) · 3.84 KB
/
make-docker-image.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
138
139
140
#!/bin/bash
# Config
FLAG_DOCKER_INSTALL=0
FLAG_UPDATE=0
HTTP_PROXY=`printenv http_proxy`
HTTPS_PROXY=`printenv https_proxy`
usage() {
echo "Usage: $0 [-i] [-u] [-h]"
echo " i: Install Docker"
echo " u: Update Docker image"
echo " h: Ptint help message"
exit 0
}
get_latest_tag() {
local TAG=`sudo docker images | grep deby | sed 's/[\t ]\+/\t/g' | cut -f2 | head -1 | tail -1`
return $TAG
}
abort() {
echo "ERROR: $@" 1>&2
exit 1
}
# Check options
while getopts "iu" OPT
do
case $OPT in
i)
echo "INFO : Installing Docker"
FLAG_DOCKER_INSTALL=1
;;
u)
echo "INFO : Updating Docker image"
FLAG_UPDATE=1
;;
h)
usage
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
# Check distro information
if [ `which apt-get` ]; then
# Debian and Ubuntu
sudo apt-get install lsb-release
else
# Other distro
echo "ERROR: Not tested (e.g. yum)"
exit 1
fi
DISTNAME=`lsb_release --id --short` # Distribution
DISTCODE=`lsb_release --codename --short` # Codename
echo "Distribution: $DISTNAME Codename: $DISTCODE"
# Preparation
if [ $FLAG_DOCKER_INSTALL -ne 0 ]; then
if [ $DISTNAME = 'Ubuntu' ]; then
if [ $DISTCODE = "trusty" ]; then
sudo apt-get install -y apt-transport-https
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
sudo sh -c "echo deb https://get.docker.com/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
sudo apt-get update
sudo apt-get install -y lxc-docker
else
echo "ERROR: Not tested on $DISTCODE"
exit 1
fi
elif [ $DISTNAME = 'Debian' ]; then
if [ $DISTCODE = 'jessie' ]; then
sudo apt-get install -y curl
curl -sSL https://get.docker.com/ | sh
else
echo "ERROR: Not tested on $DISTCODE"
exit 1
fi
else
echo "ERROR: Not tested"
exit 1
fi # DISTNAME
fi # FLAG_DOCKER_INSTALL
# Check docker installation
if [ ! `which docker` ]; then
echo "ERROR: Please install Docker."
echo " To install Docker, you can type as the follows:"
echo " ./make-docker-image.sh -i"
exit 1
fi
# Update currrent deby docker image
if [ $FLAG_UPDATE -ne 0 ]; then
if [ ! -f Dockerfile-update ]; then
echo "ERROR: Dockerfile-update does not exist"
exit 1
fi
get_latest_tag
LATEST_TAG=$?
NEW_TAG=`expr $LATEST_TAG + 1`
if [ $LATEST_TAG -le 0 ]; then
echo "ERROR: Latest tag of docker image cannot found"
exit 1
else
# Update FROM section in Dockerfile-update with LATEST_TAG
sed -i -e "s/FROM deby:.*/FROM deby:$LATEST_TAG/g" ./Dockerfile-update
if [ -z "${HTTP_PROXY}" ] || [ -z "${HTTPS_PROXY}" ]; then
sudo docker build -t deby:$NEW_TAG -f Dockerfile-update . || abort "ERROR: Cannot update docker image"
else
sudo docker build --build-arg HTTP_PROXY=$HTTP_PROXY --build-arg HTTPS_PROXY=$HTTPS_PROXY \
-t deby:$NEW_TAG -f Dockerfile-update . || abort "ERROR: Cannot update docker image"
fi
echo "INFO: New tag is deby:$NEW_TAG"
exit 0
fi
fi
# Build a docker container
if [ -f Dockerfile ]; then
if [ -z "${HTTP_PROXY}" ] || [ -z "${HTTPS_PROXY}" ]; then
sudo docker build -t deby:1 . || abort "ERROR: Cannot create docker image"
else
sudo docker build --build-arg HTTP_PROXY=$HTTP_PROXY --build-arg HTTPS_PROXY=$HTTPS_PROXY -t deby:1 . \
|| abort "ERROR: Cannot create docker image"
fi
exit 0
else
echo "ERROR: Dockerfile does not exist"
exit 1
fi
# Check the CONTAINER ID
# CONTAINER_ID=`sudo docker ps -l -q`
# Commit it
# sudo docker commit $CONTAINER_ID deby:1