Created
April 18, 2017 09:45
-
-
Save teamon/a030a4f6d2abfb0bd18a011f519e7205 to your computer and use it in GitHub Desktop.
Single and two phased docker build scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
VERSION="1" | |
IMAGE="hello/simple" | |
_run(){ | |
echo "----> $@" | |
$@ | |
} | |
case $1 in | |
build) | |
_run docker build -t "${IMAGE}:build" . | |
;; | |
publish) | |
sha="$(git rev-parse HEAD | cut -c1-10)" | |
num="$(git rev-list --count HEAD)" | |
tag="${IMAGE}:${VERSION}.${num}-${sha}" | |
_run docker tag "${IMAGE}:build" "${tag}" | |
_run docker push "${tag}" | |
echo "Image ${tag} pushed to docker registry" | |
;; | |
shipit) | |
$0 build | |
$0 publish | |
;; | |
start) | |
docker-compose up | |
;; | |
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# configuration | |
VERSION="1" | |
IMAGE="hello/world" | |
DOCKERFILE_BIILD="Dockerfile.build" | |
DOCKERFILE_RELEASE="Dockerfile.release" | |
set -e | |
_run(){ | |
echo "----> $@" | |
$@ | |
} | |
command=$1 | |
shift | |
case $command in | |
build) | |
_run docker build -f "${DOCKERFILE_BIILD}" -t "${IMAGE}:build" $@ . | |
;; | |
release) | |
cid="build-$(tr -cd 0-9 < /dev/urandom | head -c 8)" | |
docker rm -f "${cid}" 2> /dev/null || true | |
_run docker create --name "${cid}" "${IMAGE}:build" | |
_run docker cp "${cid}":/usr/src/app.tgz app.tgz | |
_run docker rm -f "${cid}" || true | |
_run docker build -f "${DOCKERFILE_RELEASE}" -t "${IMAGE}:release" . | |
rm -f app.tgz | |
echo "Docker image ${IMAGE}:release has been successfully created" | |
;; | |
publish) | |
sha="$(git rev-parse HEAD | cut -c1-10)" | |
num="$(git rev-list --count HEAD)" | |
tag="${IMAGE}:${VERSION}.${num}-${sha}" | |
_run docker tag "${IMAGE}:release" "${tag}" | |
_run docker push "${tag}" | |
echo "Image ${tag} pushed to docker registry" | |
;; | |
shipit) | |
$0 build | |
$0 release | |
$0 publish | |
;; | |
*) | |
echo "Usage: $0 build|release|publish|shipit" | |
exit 2 | |
;; | |
esac; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment