diff --git a/Dockerfile b/docker/Dockerfile similarity index 77% rename from Dockerfile rename to docker/Dockerfile index 4eeafc2..2702e03 100644 --- a/Dockerfile +++ b/docker/Dockerfile @@ -5,7 +5,9 @@ ENV LANG=C.UTF-8 ENV PYTHONUNBUFFERED=1 # Set non-root user name: -ENV USER_NAME=scanner +ARG USER_NAME=scanner +ARG BRANCH=master +ARG API_URL='https://db.romi-project.eu' USER root # Change shell to 'bash', default is 'sh' @@ -21,22 +23,21 @@ RUN apt-get update && \ rm -rf /var/lib/apt/lists/* && \ useradd -m ${USER_NAME} && \ cd /home/${USER_NAME} && \ - mkdir project && \ chown -R ${USER_NAME}: /home/${USER_NAME} # Change user USER ${USER_NAME} # Change working directory: -WORKDIR /home/${USER_NAME}/project +WORKDIR /home/${USER_NAME} -RUN git clone https://github.com/romi/3d-plantviewer.git && \ +RUN git clone --branch $BRANCH https://github.com/romi/3d-plantviewer.git && \ cd 3d-plantviewer && \ # Install modules listed as dependencies in `package.json`: npm install -WORKDIR /home/${USER_NAME}/project/3d-plantviewer +WORKDIR /home/${USER_NAME}/3d-plantviewer # Link the docker image to romi database: -ENV REACT_APP_API_URL='https://db.romi-project.eu' +ENV REACT_APP_API_URL=$API_URL CMD ["/bin/bash", "-c", "npm start"] \ No newline at end of file diff --git a/docker/build.sh b/docker/build.sh new file mode 100755 index 0000000..b4df638 --- /dev/null +++ b/docker/build.sh @@ -0,0 +1,103 @@ +#!/bin/bash + +############################################################################### +# Example usages: +############################################################################### +# 1. Default build options will create `roboticsmicrofarms/plantviewer:latest` pointing at ROMI database: +# $ ./build.sh +# +# 2. Build image with 'debug' image tag & another 'plantviewer' branch options: +# $ ./build.sh -t debug -b 'feature/faster_docker' + +user=$USER +vtag="latest" +branch='master' +api_url='https://db.romi-project.eu' +docker_opts="" + +usage() { + echo "USAGE:" + echo " ./build.sh [OPTIONS] + " + + echo "DESCRIPTION:" + echo " Build a docker image named 'roboticsmicrofarms/plantviewer' using Dockerfile in same location. + " + + echo "OPTIONS:" + echo " -t, --tag + Docker image tag to use, default to '$vtag'. + " + echo " -u, --user + User name to create inside docker image, default to '$user'. + " + echo " -b, --branch + Git branch to use for cloning '3d-plantviewer' inside docker image, default to '$branch'. + " + echo " --api_url + REACT API URL to use to retrieve dataset, default is '$api_url'. + " + # Docker options: + echo " --no-cache + Do not use cache when building the image, (re)start from scratch. + " + echo " --pull + Always attempt to pull a newer version of the parent image. + " + # General options: + echo " -h, --help + Output a usage message and exit. + " +} + +while [ "$1" != "" ]; do + case $1 in + -t | --tag) + shift + vtag=$1 + ;; + -u | --user) + shift + user=$1 + ;; + -b | --branch) + shift + branch=$1 + ;; + --api_url) + shift + api_url=$1 + ;; + --no-cache) + shift + docker_opts="$docker_opts --no-cache" + ;; + --pull) + shift + docker_opts="$docker_opts --pull" + ;; + -h | --help) + usage + exit + ;; + *) + usage + exit 1 + ;; + esac + shift +done + +# Get the date to estimate docker image build time: +start_time=`date +%s` + +# Start the docker image build: +docker build -t roboticsmicrofarms/plantviewer:$vtag $docker_opts \ + --build-arg USER_NAME=$user \ + --build-arg BRANCH=$branch \ + --build-arg API_URL=$api_url \ + . + +# Print docker image build time: +echo +echo "Build time: $(expr `date +%s` - $start_time)s" diff --git a/docker-compose.yml b/docker/docker-compose.yml similarity index 86% rename from docker-compose.yml rename to docker/docker-compose.yml index 339dd5e..a8845ed 100644 --- a/docker-compose.yml +++ b/docker/docker-compose.yml @@ -13,6 +13,6 @@ services: depends_on: - db environment: - REACT_APP_API_URL: http://172.21.0.2:5000 + REACT_APP_API_URL: http://172.21.0.1:5000 ports: - "3000:3000" \ No newline at end of file diff --git a/docker/run.sh b/docker/run.sh new file mode 100755 index 0000000..7fac71c --- /dev/null +++ b/docker/run.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +############################################################################### +# Example usages: +############################################################################### +# 1. Default run starts an interactive shell: +# $ ./run.sh + +vtag="latest" +api_url='https://db.romi-project.eu' + +usage() { + echo "USAGE:" + echo " ./run.sh [OPTIONS] + " + + echo "DESCRIPTION:" + echo " Run 'roboticsmicrofarms/plantviewer:' container with a mounted local (host) database. + " + + echo "OPTIONS:" + echo " -t, --tag + Docker image tag to use, default to '$vtag'. + " + echo " --api_url + REACT API URL to use to retrieve dataset, default is '$api_url'. + " + + echo " -h, --help + Output a usage message and exit. + " +} + +while [ "$1" != "" ]; do + case $1 in + -t | --tag) + shift + vtag=$1 + ;; + --api_url) + shift + api_url=$1 + ;; + -h | --help) + usage + exit + ;; + *) + usage + exit 1 + ;; + esac + shift +done + +# Use 'host database path' & 'docker user' to create a bind mount: +if [ "$api_url" != "" ] +then + docker run \ + --env REACT_APP_API_URL="$api_url" \ + -it roboticsmicrofarms/plantviewer:$vtag +else + docker run \ + -it roboticsmicrofarms/plantviewer:$vtag +fi + +