diff --git a/.gitignore b/.gitignore index 239ecff..80892ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules yarn.lock + +data diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..544f208 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM node:9.11-alpine + +COPY . /opt/application + +WORKDIR /opt/application +RUN ["chmod", "+x", "wait-for.sh"] + +ARG MT_PORT +ARG MONGODB_URI + +ENV MT_PORT "$MT_PORT" +ENV MONGODB_URI "$MONGODB_URI" + +RUN apk update && \ + yarn install diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..5974c95 --- /dev/null +++ b/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh +IMAGE_NAME=doge/react-chat-api + +docker build -t $IMAGE_NAME . +docker tag $IMAGE_NAME:latest diff --git a/config.js b/config.js index cd65ec9..d299dda 100644 --- a/config.js +++ b/config.js @@ -1,5 +1,5 @@ module.exports = { - PORT: process.env.PORT || 8000, + PORT: process.env.MT_PORT || 8000, JWT_SECRET: process.env.JWT_SECRET || 'dogecodes-secret', MONGODB_URI: process.env.MONGODB_URI || 'mongodb://localhost/dogecodes-chat-app', }; diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..796e714 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,26 @@ + +version: "3.3" + +services: + mongo: + image: mongo:3.6.0 + ports: + - "0:27017" + - "0:28017" + volumes: + - ./data/db:/data/db + + server: + build: . + image: doge/react-chat-api:latest + links: + - mongo + ports: + - "8001:8000" + environment: + - MT_PORT=8000 + - MONGODB_URI=mongodb://mongo/dogecodes-chat-app + - NODE_ENV=development + command: sh -c './wait-for.sh mongo:27017 -- npm start' + depends_on: + - mongo \ No newline at end of file diff --git a/wait-for.sh b/wait-for.sh new file mode 100644 index 0000000..bf6a071 --- /dev/null +++ b/wait-for.sh @@ -0,0 +1,79 @@ +#!/bin/sh + +TIMEOUT=15 +QUIET=0 + +echoerr() { + if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi +} + +usage() { + exitcode="$1" + cat << USAGE >&2 +Usage: + $cmdname host:port [-t timeout] [-- command args] + -q | --quiet Do not 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 "$exitcode" +} + +wait_for() { + for i in `seq $TIMEOUT` ; do + nc -z "$HOST" "$PORT" > /dev/null 2>&1 + + result=$? + if [ $result -eq 0 ] ; then + if [ $# -gt 0 ] ; then + exec "$@" + fi + exit 0 + fi + sleep 1 + done + echo "Operation timed out" >&2 + exit 1 +} + +while [ $# -gt 0 ] +do + case "$1" in + *:* ) + HOST=$(printf "%s\n" "$1"| cut -d : -f 1) + PORT=$(printf "%s\n" "$1"| cut -d : -f 2) + shift 1 + ;; + -q | --quiet) + QUIET=1 + shift 1 + ;; + -t) + TIMEOUT="$2" + if [ "$TIMEOUT" = "" ]; then break; fi + shift 2 + ;; + --timeout=*) + TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + break + ;; + --help) + usage 0 + ;; + *) + echoerr "Unknown argument: $1" + usage 1 + ;; + esac +done + +if [ "$HOST" = "" -o "$PORT" = "" ]; then + echoerr "Error: you need to provide a host and port to test." + usage 2 +fi + +wait_for "$@" \ No newline at end of file