Skip to content

Instantly share code, notes, and snippets.

@rilleralle
Created June 29, 2018 05:20
Show Gist options
  • Save rilleralle/67b3042a98484d2a314c67cc5ce5bdf8 to your computer and use it in GitHub Desktop.
Save rilleralle/67b3042a98484d2a314c67cc5ce5bdf8 to your computer and use it in GitHub Desktop.
Shell script to build and push a Docker image to a private or Docker Hub repo
#!/bin/sh
# ============================================================================
# Usage
#
# build-push-docker.sh \
# --image-name=name \
# --dockerfile=Dockerfile \
# --private-repo-url=my-private-docker-registry.foo
# --private-repo-path=/path
# --username=foo \
# --password=bar \
# --version=1.0.0 \
#
# Outcome
# A new image will be created here:
#
# my-private-docker-registry.foo/path/name:1.0.0
#
# ============================================================================
for argumentName in "$@"
do
case $argumentName in
--image-name=*)
IMAGE_NAME="${argumentName#*=}"
;;
--dockerfile=*)
DOCKERFILE="${argumentName#*=}"
;;
--private-repo-url=*)
PRIVATE_REPO_URL="${argumentName#*=}"
;;
--private-repo-path=*)
PRIVATE_REPO_PATH="${argumentName#*=}"
;;
--username=*)
USERNAME="${argumentName#*=}"
;;
--password=*)
PASSWORD="${argumentName#*=}"
;;
--version=*)
VERSION="${argumentName#*=}"
;;
esac
done
if [ -z "${IMAGE_NAME}" ]; then
echo "ERROR: No image name defined! (Missing --image-name argument)"
exit 1
elif [ -z "${USERNAME}" ]; then
echo "ERROR: No username defined! (Missing --username argument)"
exit 1
elif [ -z "${PASSWORD}" ]; then
echo "ERROR: No password defined! (Missing --password argument)"
exit 1
elif [ -z "${VERSION}" ]; then
echo "ERROR: No version defined! (Missing --version argument)"
exit 1
fi
if [ -z "${DOCKERFILE}" ]; then
echo "Info: No explicit Dockerfile has been defined! Using 'Dockerfile' as default"
DOCKERFILE="Dockerfile"
fi
FULL_IMAGE_NAME=${PRIVATE_REPO_URL}${PRIVATE_REPO_PATH}/${IMAGE_NAME}:${VERSION}
echo "Push image: ${FULL_IMAGE_NAME}:${VERSION}"
docker login ${PRIVATE_REPO_URL} -u ${USERNAME} --password-stdin ${PASSWORD}
docker build -f ${DOCKERFILE} -t ${FULL_IMAGE_NAME} .
docker push ${FULL_IMAGE_NAME}
@faheemkhan5
Copy link

Dear , will you please provide me the latest script for building and pushing docker image. i shall be very thankful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment