From e0d321f0808de92d8291810b7b508c90820d674c Mon Sep 17 00:00:00 2001 From: Hasnat Ullah Date: Sat, 25 May 2019 13:50:29 +0100 Subject: [PATCH] Update docker-compose run --- .dockerignore | 4 ++ Dockerfile | 17 +++-- README.md | 9 +-- bin/docker-run.sh | 122 ---------------------------------- bin/wait-for-it.sh | 161 --------------------------------------------- db/Dockerfile | 5 ++ db/init.sql | 2 - docker-compose.yml | 18 ++--- 8 files changed, 36 insertions(+), 302 deletions(-) create mode 100644 .dockerignore delete mode 100755 bin/docker-run.sh delete mode 100755 bin/wait-for-it.sh create mode 100644 db/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3e90d81 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +Dockerfile +docker-compose.yml +**/target \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 5a701ed..894d3c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,9 @@ +FROM maven:3.6-jdk-8-alpine AS builder +WORKDIR /usr/shepher + +COPY . . +RUN mvn install + FROM java:8-jdk @@ -9,7 +15,10 @@ ENV BASE_DIR /usr/shepher WORKDIR $SHEPHER_HOME -COPY ./lib/*.jar $SHEPHER_HOME/lib/ -COPY ./bin/wait-for-it.sh $SHEPHER_HOME -COPY ./conf/* $SHEPHER_HOME/conf/ -CMD ./wait-for-it.sh -t 300 db:3306 && java -jar lib/shepher-web-1.0.jar --spring.config.location=conf/application-base.properties,conf/application-docker.properties -Djava.ext.dirs=lib +COPY --from=builder /usr/shepher/shepher-web/target/*.jar $SHEPHER_HOME/lib/ +COPY --from=builder /usr/shepher/shepher-common/target/*.jar $SHEPHER_HOME/lib/ +COPY --from=builder /usr/shepher/shepher-model/target/*.jar $SHEPHER_HOME/lib/ +COPY --from=builder /usr/shepher/shepher-service/target/*.jar $SHEPHER_HOME/lib/ + +COPY --from=builder /usr/shepher/conf/* $SHEPHER_HOME/conf/ +CMD java -jar lib/shepher-1.0.jar --spring.config.location=conf/application-base.properties,conf/application-docker.properties -Djava.ext.dirs=lib diff --git a/README.md b/README.md index 0b8c2db..b476f13 100644 --- a/README.md +++ b/README.md @@ -84,15 +84,16 @@ Development environment deployment includes local compilation deployment and Doc MySQL and ZooKeeper will be automatic integrated when Docker deployment is used, and automatically import `db/init.sql` into MySQL, no self installation is needed. 1. Install Docker, take Ubuntu system for example, install [docker engine](https://docs.docker.com/engine/installation/#installation) and [docker-compose](https://docs.docker.com/compose/install/) -2. Enter install directories +2. Clone repo ```sh - $ cd shepher-packaging/target/shepher-packaging-{version}-bin + $ git clone git@github.com:XiaoMi/shepher.git + $ cd shepher ``` -3. Run the script, start Shepher service and waiting start of each container in Docker accomplish +3. Docker compose up to start services ```sh - $ sh bin/docker-run.sh start + $ docker-compose up --build ``` 4. Visit `http://localhost:8089` or self defined `server.url` (Reference to [Parameter instruction](Docs/Parameter.md)) diff --git a/bin/docker-run.sh b/bin/docker-run.sh deleted file mode 100755 index 45efdfa..0000000 --- a/bin/docker-run.sh +++ /dev/null @@ -1,122 +0,0 @@ -# Copyright 2017 Xiaomi, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -#!/bin/bash - -this="${BASH_SOURCE-$0}" -bin=`dirname "$this"` -bin=`cd "$bin">/dev/null; pwd` -BASE_DIR=`dirname "$bin"` -SHEPHERPID="$BASE_DIR/shepher.pid" -KILL=/bin/kill - -export BASE_DIR=$BASE_DIR - -##set default shepher server ip:port -SERVERIP="127.0.0.1" -SERVERPORT="8089" - -##parse arguments from commandline -while getopts 'i:p:' OPT;do - case $OPT in - i) - SERVERIP="$OPTARG";; - p) - SERVERPORT="$OPTARG";; - esac -done - -shift $((OPTIND-1)) - -##set action: start/stop/restart/status -ACTION=$@ - -##verify status -case $ACTION in -start) - echo -n "Starting shepher...\n" - echo "Server ip: $SERVERIP" >&2 - echo "Server port: $SERVERPORT" >&2 - - if [ -f $SHEPHERPID ]; then - if kill -0 `cat $SHEPHERPID` 1> /dev/null 2>&1; then - echo $command already running as process `cat $SHEPHERPID`. - exit 0 - fi - fi - cd $BASE_DIR && docker-compose build - cd $BASE_DIR && docker-compose up -d - if [ $? -eq 0 ] - then - if /bin/echo -n $! > "$SHEPHERPID" - then - curl $SERVERIP:$SERVERPORT 1>/dev/null 2>&1 - ret=$? - while [ $ret -ne '0' ] - do - sleep 5 - curl $SERVERIP:$SERVERPORT 1>/dev/null 2>&1 - ret=$? - done - echo STARTED - else - echo FAILED TO WRITE PID - exit 1 - fi - else - echo SERVER DID NOT START - exit 1 - fi - ;; -start-foreground) - cd $BASE_DIR && docker-compose build - cd $BASE_DIR && docker-compose up - ;; -stop) - echo -n "Stopping shepher...\n" - cd $BASE_DIR && docker-compose stop - cd $BASE_DIR && docker-compose rm -f - if [ ! -f "$SHEPHERPID" ] - then - echo "no shepher to stop (could not find file $SHEPHERPID)" - else - $KILL -9 $(cat "$SHEPHERPID") - rm "$SHEPHERPID" - echo STOPPED - fi - ;; -restart) - shift - sh "$0" stop ${@} - sleep 3 - sh "$0" start ${@} - ;; -status) - if [ -f $SHEPHERPID ]; then - if kill -0 `cat $SHEPHERPID` 1> /dev/null 2>&1; then - echo "running" - exit 0 - fi - fi - - echo "Error contacting service. It is probably not running." - exit 1 - ;; -*) - - echo "Usage: $0 [OPTIONS] {start|start-foreground|stop|restart|status} \n\ - OPTIONS:\n\ - -i Shepher server ip\n\ - -p Shepher server port" >&2 -esac diff --git a/bin/wait-for-it.sh b/bin/wait-for-it.sh deleted file mode 100755 index eca6c3b..0000000 --- a/bin/wait-for-it.sh +++ /dev/null @@ -1,161 +0,0 @@ -#!/usr/bin/env bash -# Use this script to test if a given TCP host/port are available - -cmdname=$(basename $0) - -echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } - -usage() -{ - cat << USAGE >&2 -Usage: - $cmdname host:port [-s] [-t timeout] [-- command args] - -h HOST | --host=HOST Host or IP under test - -p PORT | --port=PORT TCP port under test - Alternatively, you specify the host and port as host:port - -s | --strict Only execute subcommand if the test succeeds - -q | --quiet Don't output any status messages - -t TIMEOUT | --timeout=TIMEOUT - Timeout in seconds, zero for no timeout - -- COMMAND ARGS Execute command with args after the test finishes -USAGE - exit 1 -} - -wait_for() -{ - if [[ $TIMEOUT -gt 0 ]]; then - echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT" - else - echoerr "$cmdname: waiting for $HOST:$PORT without a timeout" - fi - start_ts=$(date +%s) - while : - do - (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1 - result=$? - if [[ $result -eq 0 ]]; then - end_ts=$(date +%s) - echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds" - break - fi - sleep 1 - done - return $result -} - -wait_for_wrapper() -{ - # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 - if [[ $QUIET -eq 1 ]]; then - timeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & - else - timeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & - fi - PID=$! - trap "kill -INT -$PID" INT - wait $PID - RESULT=$? - if [[ $RESULT -ne 0 ]]; then - echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT" - fi - return $RESULT -} - -# process arguments -while [[ $# -gt 0 ]] -do - case "$1" in - *:* ) - hostport=(${1//:/ }) - HOST=${hostport[0]} - PORT=${hostport[1]} - shift 1 - ;; - --child) - CHILD=1 - shift 1 - ;; - -q | --quiet) - QUIET=1 - shift 1 - ;; - -s | --strict) - STRICT=1 - shift 1 - ;; - -h) - HOST="$2" - if [[ $HOST == "" ]]; then break; fi - shift 2 - ;; - --host=*) - HOST="${1#*=}" - shift 1 - ;; - -p) - PORT="$2" - if [[ $PORT == "" ]]; then break; fi - shift 2 - ;; - --port=*) - PORT="${1#*=}" - shift 1 - ;; - -t) - TIMEOUT="$2" - if [[ $TIMEOUT == "" ]]; then break; fi - shift 2 - ;; - --timeout=*) - TIMEOUT="${1#*=}" - shift 1 - ;; - --) - shift - CLI="$@" - break - ;; - --help) - usage - ;; - *) - echoerr "Unknown argument: $1" - usage - ;; - esac -done - -if [[ "$HOST" == "" || "$PORT" == "" ]]; then - echoerr "Error: you need to provide a host and port to test." - usage -fi - -TIMEOUT=${TIMEOUT:-15} -STRICT=${STRICT:-0} -CHILD=${CHILD:-0} -QUIET=${QUIET:-0} - -if [[ $CHILD -gt 0 ]]; then - wait_for - RESULT=$? - exit $RESULT -else - if [[ $TIMEOUT -gt 0 ]]; then - wait_for_wrapper - RESULT=$? - else - wait_for - RESULT=$? - fi -fi - -if [[ $CLI != "" ]]; then - if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then - echoerr "$cmdname: strict mode, refusing to execute subprocess" - exit $RESULT - fi - exec $CLI -else - exit $RESULT -fi diff --git a/db/Dockerfile b/db/Dockerfile new file mode 100644 index 0000000..1d58a8c --- /dev/null +++ b/db/Dockerfile @@ -0,0 +1,5 @@ +FROM mysql:5.7.26 +COPY init.sql /docker-entrypoint-initdb.d/ +COPY --from=healthcheck/mysql /usr/local/bin/docker-healthcheck /usr/local/bin/ + +HEALTHCHECK CMD ["docker-healthcheck"] \ No newline at end of file diff --git a/db/init.sql b/db/init.sql index b4e8370..d6a42ae 100644 --- a/db/init.sql +++ b/db/init.sql @@ -31,8 +31,6 @@ /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -CREATE DATABASE IF NOT EXISTS `shepher`; -USE `shepher`; -- -- Table structure for table `cluster` diff --git a/docker-compose.yml b/docker-compose.yml index 98f67cd..d70408e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,21 +1,21 @@ -version: '2' +version: '2.4' services: - db: - image: mysql + db: + build: ./db + image: shepher-db environment: MYSQL_ROOT_PASSWORD: root - volumes: - - ./db:/docker-entrypoint-initdb.d + MYSQL_DATABASE: shepher web: build: ./ image: shepher ports: - "8089:8089" depends_on: - - db - - zookeeper - links: - - db + db: + condition: service_healthy + zookeeper: + condition: service_started zookeeper: image: zookeeper expose: