From eb108edb60d56befa39d3887084b73edb64f7a19 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 18 Apr 2021 16:26:32 +1000 Subject: [PATCH] Adds entrypoint for starting a development server --- docker/Dockerfile | 22 ++++---- docker/docker-compose.yml | 52 +++++++++---------- docker/start_dev_server.sh | 46 ++++++++++++++++ .../{start_server.sh => start_prod_server.sh} | 0 4 files changed, 85 insertions(+), 35 deletions(-) create mode 100644 docker/start_dev_server.sh rename docker/{start_server.sh => start_prod_server.sh} (100%) diff --git a/docker/Dockerfile b/docker/Dockerfile index 2911da907a..9682665e04 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -30,12 +30,14 @@ ENV INVENTREE_SECRET_KEY_FILE="${INVENTREE_DATA_DIR}/secret_key.txt" ENV INVENTREE_WEB_PORT="8000" # Pass DB configuration through as environment variables -ENV INVENTREE_DB_ENGINE="${INVENTREE_DB_ENGINE}" -ENV INVENTREE_DB_NAME="${INVENTREE_DB_NAME}" -ENV INVENTREE_DB_HOST="${INVENTREE_DB_HOST}" -ENV INVENTREE_DB_PORT="${INVENTREE_DB_PORT}" -ENV INVENTREE_DB_USER="${INVENTREE_DB_USER}" -ENV INVENTREE_DB_PASSWORD="${INVENTREE_DB_PASSWORD}" +# Default configuration = postgresql +ENV INVENTREE_DB_ENGINE="postgresql" +ENV INVENTREE_DB_NAME="inventree" +ENV INVENTREE_DB_HOST="db" +ENV INVENTREE_DB_PORT="5432" + +# INVENTREE_DB_USER must be specified at run-time +# INVENTREE_DB_PASSWORD must be specified at run-time LABEL org.label-schema.schema-version="1.0" \ org.label-schema.build-date=${DATE} \ @@ -93,14 +95,16 @@ RUN pip install --no-cache-dir -U -r ${INVENTREE_SRC_DIR}/requirements.txt COPY gunicorn.conf.py ${INVENTREE_HOME}/gunicorn.conf.py # Copy startup scripts -COPY start_server.sh ${INVENTREE_SRC_DIR}/start_server.sh +COPY start_prod_server.sh ${INVENTREE_SRC_DIR}/start_prod_server.sh +COPY start_dev_server.sh ${INVENTREE_SRC_DIR}/start_dev_server.sh COPY start_worker.sh ${INVENTREE_SRC_DIR}/start_worker.sh -RUN chmod 755 ${INVENTREE_SRC_DIR}/start_server.sh +RUN chmod 755 ${INVENTREE_SRC_DIR}/start_prod_server.sh +RUN chmod 755 ${INVENTREE_SRC_DIR}/start_dev_server.sh RUN chmod 755 ${INVENTREE_SRC_DIR}/start_worker.sh # exec commands should be executed from the "src" directory WORKDIR ${INVENTREE_SRC_DIR} # Let us begin -CMD ["bash", "./start_server.sh"] +CMD ["bash", "./start_prod_server.sh"] diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 8a18d864b3..31b43bac01 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -12,6 +12,7 @@ version: "3.8" # Before running, ensure that you change the "/path/to/data" directory, # specified in the "volumes" section at the end of this file. # This path determines where the InvenTree data will be stored! +# services: # Database service @@ -25,6 +26,7 @@ services: - 5432/tcp environment: - PGDATA=/var/lib/postgresql/data/pgdb + # The pguser and pgpassword values must be the same in the other containers - POSTGRES_USER=pguser - POSTGRES_PASSWORD=pgpassword volumes: @@ -44,13 +46,28 @@ services: - data:/home/inventree/data - static:/home/inventree/static environment: - - INVENTREE_DB_ENGINE=postgresql - - INVENTREE_DB_NAME=inventree + # Default environment variables are configured to match the 'db' container + # Database permissions + - INVENTREE_DB_USER=pguser + - INVENTREE_DB_PASSWORD=pgpassword + restart: unless-stopped + + # background worker process handles long-running or periodic tasks + worker: + container_name: worker + image: inventree/inventree:latest + entrypoint: ./start_worker.sh + depends_on: + - db + - web + volumes: + - data:/home/inventree/data + - static:/home/inventree/static + environment: + # Default environment variables are configured to match the 'db' container + # Database permissions - INVENTREE_DB_USER=pguser - INVENTREE_DB_PASSWORD=pgpassword - - INVENTREE_DB_PORT=5432 - - INVENTREE_DB_HOST=db - - INVENTREE_WEB_PORT=8000 restart: unless-stopped # nginx acts as a reverse proxy @@ -67,33 +84,14 @@ services: - 1337:80 volumes: # Provide nginx.conf file to the container + # Refer to the provided example file as a starting point - ./nginx.conf:/etc/nginx/templates/default.conf.template:ro # Static data volume is mounted to /var/www/static - static:/var/www/static - - # background worker process handles long-running or periodic tasks - worker: - container_name: worker - image: inventree/inventree:latest - entrypoint: ./start_worker.sh - depends_on: - - db - - web - volumes: - - data:/home/inventree/data - - static:/home/inventree/static - environment: - - INVENTREE_DB_ENGINE=postgresql - - INVENTREE_DB_NAME=inventree - - INVENTREE_DB_USER=pguser - - INVENTREE_DB_PASSWORD=pgpassword - - INVENTREE_DB_PORT=5432 - - INVENTREE_DB_HOST=db restart: unless-stopped volumes: - # Static files, shared between containers - static: + # NOTE: Change /path/to/data to a directory on your local machine # Persistent data, stored externally data: driver: local @@ -103,3 +101,5 @@ volumes: # This directory specified where InvenTree data are stored "outside" the docker containers # Change this path to a local system path where you want InvenTree data stored device: /path/to/data + # Static files, shared between containers + static: \ No newline at end of file diff --git a/docker/start_dev_server.sh b/docker/start_dev_server.sh new file mode 100644 index 0000000000..281493849c --- /dev/null +++ b/docker/start_dev_server.sh @@ -0,0 +1,46 @@ +#!/bin/sh + +# Create required directory structure (if it does not already exist) +if [[ ! -d "$INVENTREE_STATIC_ROOT" ]]; then + echo "Creating directory $INVENTREE_STATIC_ROOT" + mkdir $INVENTREE_STATIC_ROOT +fi + +if [[ ! -d "$INVENTREE_MEDIA_ROOT" ]]; then + echo "Creating directory $INVENTREE_MEDIA_ROOT" + mkdir $INVENTREE_MEDIA_ROOT +fi + +if [[ ! -d "$INVENTREE_BACKUP_DIR" ]]; then + echo "Creating directory $INVENTREE_BACKUP_DIR" + mkdir $INVENTREE_BACKUP_DIR +fi + +# Check if "config.yaml" has been copied into the correct location +if test -f "$INVENTREE_CONFIG_FILE"; then + echo "$INVENTREE_CONFIG_FILE exists - skipping" +else + echo "Copying config file to $INVENTREE_CONFIG_FILE" + cp $INVENTREE_SRC_DIR/InvenTree/config_template.yaml $INVENTREE_CONFIG_FILE +fi + +echo "Starting InvenTree server..." + +# Wait for the database to be ready +cd $INVENTREE_MNG_DIR +python manage.py wait_for_db + +sleep 10 + +echo "Running InvenTree database migrations and collecting static files..." + +# We assume at this stage that the database is up and running +# Ensure that the database schema are up to date +python manage.py check || exit 1 +python manage.py migrate --noinput || exit 1 +python manage.py migrate --run-syncdb || exit 1 +python manage.py collectstatic --noinput || exit 1 +python manage.py clearsessions || exit 1 + +# Launch a development server +python manage.py runserver -a 0.0.0.0:$INVENTREE_WEB_PORT diff --git a/docker/start_server.sh b/docker/start_prod_server.sh similarity index 100% rename from docker/start_server.sh rename to docker/start_prod_server.sh