forked from sonic-net/sonic-buildimage
-
Notifications
You must be signed in to change notification settings - Fork 49
/
build_docker.sh
executable file
·61 lines (49 loc) · 1.94 KB
/
build_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
#!/bin/bash
## This script is to automate the preparation for docker images for ACS.
## If registry server and port provided, the images will be pushed there.
## Usage:
## sudo ./build_docker.sh DOCKER_BUILD_DIR [REGISTRY_SERVER REGISTRY_PORT]
set -x -e
## Dockerfile directory
DOCKER_BUILD_DIR=$1
REGISTRY_SERVER=$2
REGISTRY_PORT=$3
REGISTRY_USERNAME=$4
REGISTRY_PASSWD=$5
[ -d "$DOCKER_BUILD_DIR" ] || {
echo "Invalid DOCKER_BUILD_DIR directory" >&2
exit 1
}
## Docker image label, so no need to remember its hash
docker_image_name=$DOCKER_BUILD_DIR
## File name for docker image
docker_image_gz=$docker_image_name.gz
[ -n "$docker_image_gz" ] || {
echo "Error: Output docker image filename is empty"
exit 1
}
function cleanup {
rm -rf $DOCKER_BUILD_DIR/files
rm -rf $DOCKER_BUILD_DIR/deps
docker rmi -f $docker_image_name || true
}
trap cleanup exit
## Copy dependencies
## Note: Dockerfile ADD doesn't support reference files outside the folder, so copy it locally
mkdir -p $DOCKER_BUILD_DIR/deps
cp deps/*.deb $DOCKER_BUILD_DIR/deps
## Copy the suggested Debian sources
## ref: https://wiki.debian.org/SourcesList
mkdir -p $DOCKER_BUILD_DIR/files
cp files/sources.list $DOCKER_BUILD_DIR/files
docker build -t $docker_image_name $DOCKER_BUILD_DIR
if [ -n "$REGISTRY_SERVER" ] && [ -n "$REGISTRY_PORT" ]; then
## Add registry information as tag, so will push as latest
## Temporarily add -f option to prevent error message of Docker engine version < 1.10.0
docker tag -f $docker_image_name $REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name
## Login the docker image registry server
## Note: user name and password are passed from command line, use fake email address to bypass login check
docker login -u $REGISTRY_USERNAME -p "$REGISTRY_PASSWD" -e "@" $REGISTRY_SERVER:$REGISTRY_PORT
docker push $REGISTRY_SERVER:$REGISTRY_PORT/$docker_image_name
fi
docker save $docker_image_name | gzip -c > $docker_image_gz