Skip to content

Instantly share code, notes, and snippets.

@lsneucamp
Created May 25, 2017 04:47
Show Gist options
  • Save lsneucamp/75c5ac0939f342746b9753fcda80c1be to your computer and use it in GitHub Desktop.
Save lsneucamp/75c5ac0939f342746b9753fcda80c1be to your computer and use it in GitHub Desktop.
Docker build script
#! /bin/bash
#exit on error
set -e
DOCKER_IMAGE_NAME='maven-indexer-jar'
# define a docker volume for maven packages repositories
PROJECT_NAME='indexer';
# define a docker volume for maven packages repositories
MAVEN_REPO_VOL='maven-repo';
# get build.sh script current folder
pwd="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# clear dist/
rm -rf ${pwd}/dist
# get docker volumes lst
docker_volumes=$(docker volume ls -q);
# create maven-repo if not exists
if [[ ! "${docker_volumes[@]}" =~ "${MAVEN_REPO_VOL}" ]]; then
echo "Creating maven repository volume ${MAVEN_REPO_VOL}";
docker volume create --name ${MAVEN_REPO_VOL};
echo "Creating maven repository volume ${MAVEN_REPO_VOL} completed";
fi
# docker build image
echo "Starting to build docker image ${DOCKER_IMAGE_NAME}";
docker build -f ${pwd}/build.Dockerfile -t ${DOCKER_IMAGE_NAME} ${pwd};
echo "Build docker image ${DOCKER_IMAGE_NAME} completed!";
#$("docker build -t ${DOCKER_IMAGE_NAME} ${pwd}");
#echo $1;
# check if builds
# docker run maven package verify if it runs
echo "Project building in ${DOCKER_IMAGE_NAME} image using maven repository volume ${MAVEN_REPO_VOL}";
mkdir -p dist;
mvn_install="mvn clean package -Dmaven.test.skip=true";
docker run --rm -i -v ${MAVEN_REPO_VOL}:/root/.m2 -v ${pwd}/dist:/usr/src/app/dist ${DOCKER_IMAGE_NAME} /bin/bash -c "${mvn_install}; cp target/${PROJECT_NAME}*.jar dist/${PROJECT_NAME}.jar";
echo "Build ${DOCKER_IMAGE_NAME} was success";
#
#echo "Maven Install!";
# docker run maven install
#echo "Running maven install in ${DOCKER_IMAGE_NAME} image using maven repository volume ${MAVEN_REPO_VOL}";
#docker run -i -v ${MAVEN_REPO_VOL}:/root/.m2 ${DOCKER_IMAGE_NAME} mvn -Pdocker -Denv.DB_USERNAME=root -Denv.DB_PASSWORD=root -Denv.DB_URL=jdbc:mysql://localhost:3306/thevine install
#echo "Maven installed WAR package in volume ${MAVEN_REPO_VOL}";
echo "Building ${PROJECT_NAME} docker!";
docker build -t ${PROJECT_NAME} ${pwd};
#$(`docker run -i -v ${MAVEN_REPO_VOL}:/root/.m2 ${DOCKER_IMAGE_NAME} mvn install`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment