From 9460dcbcabacc7e58c496161f01c7600081ec479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Cabrita?= Date: Tue, 11 May 2021 16:40:40 +0100 Subject: [PATCH] feature: Added ability to run OCIM in a container for development. --- .dockerignore | 1 + .gitignore | 2 + Dockerfile.ocim | 15 ++++++++ Makefile | 2 +- docker-compose.yml | 58 ++++++++++++++++++++++++++++ documentation/development/docker.md | 59 +++++++++++++++++++++++++++++ documentation/installation.md | 5 +-- frontend/.dockerignore | 2 + frontend/Dockerfile | 19 ++++++++++ mkdocs.yml | 2 + requirements/dev.txt | 2 +- 11 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile.ocim create mode 100644 docker-compose.yml create mode 100644 documentation/development/docker.md create mode 100644 frontend/.dockerignore create mode 100644 frontend/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..413c21d0f --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +frontend/node_modules/ \ No newline at end of file diff --git a/.gitignore b/.gitignore index bf3ff5248..8a399bd35 100644 --- a/.gitignore +++ b/.gitignore @@ -111,3 +111,5 @@ media/ # Generated scripts bin/archived_cleanup.sh +# used for customizing docker compose +docker-compose.override.yml \ No newline at end of file diff --git a/Dockerfile.ocim b/Dockerfile.ocim new file mode 100644 index 000000000..df64af322 --- /dev/null +++ b/Dockerfile.ocim @@ -0,0 +1,15 @@ +FROM python:3.6 + +WORKDIR /usr/src/ocim + +COPY requirements.txt ./ +COPY requirements/ requirements/ +RUN pip install -r requirements.txt + +COPY Makefile \ + package.json \ + ./ +COPY frontend/ frontend/ +COPY static/ static/ + +CMD ["make", "run.dev"] diff --git a/Makefile b/Makefile index 4bb361782..ea10ec017 100644 --- a/Makefile +++ b/Makefile @@ -111,7 +111,7 @@ run: clean migrations.check static ## Run Ocim in a production setting with conc honcho start --concurrency "worker=$(WORKERS),worker_low_priority=$(WORKERS_LOW_PRIORITY)" run.dev: clean migrations.check static_external ## Run the developmental server using `runserver_plus`. - honcho start -f Procfile.dev + python3 manage.py runserver 0.0.0.0:5000 shell: ## Start the power shell. HUEY_QUEUE_NAME=opencraft_low_priority $(HONCHO_MANAGE) shell_plus diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..ff700fd5f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,58 @@ +# Docker Compose file for the services needed by Ocim, for easy local development setup + +version: "3.6" + +services: + + postgresql: + image: postgres:12.6-alpine + environment: + POSTGRES_USER: opencraft + POSTGRES_DB: opencraft + POSTGRES_HOST_AUTH_METHOD: trust + POSTGRES_INITDB_ARGS: --encoding utf-8 + volumes: + - /var/lib/postgresql/data + ports: + - "5432:5432" + + redis: + image: postgres:12.6-alpine + image: redis:4 + ports: + - "6379:6379" + + ocim: + build: + context: . + dockerfile: Dockerfile.ocim + depends_on: + - postgresql + - redis + env_file: .env + environment: + ALLOWED_HOSTS: '["*"]' + DATABASE_URL: '${DATABASE_URL:-postgres://opencraft@postgresql/opencraft}' + REDIS_URL: '${REDIS_URL:-redis://redis:6379}' + ports: + - "127.0.0.1:5000:5000" + volumes: + - ./:/usr/src/ocim/ + + ocim-frontend: + build: + context: frontend/ + depends_on: + - ocim + environment: {} + ports: + - "127.0.0.1:3000:3000" + tty: true + volumes: + - ./frontend:/usr/src/ocim-frontend/ + - ./static:/usr/src/static + +volumes: {} + +networks: + default: \ No newline at end of file diff --git a/documentation/development/docker.md b/documentation/development/docker.md new file mode 100644 index 000000000..bb04f0794 --- /dev/null +++ b/documentation/development/docker.md @@ -0,0 +1,59 @@ +# Docker + +This describes how to create an environment for development using Docker. + +**This is still experimental, using [Vagrant](../installation.md) is much better tested and supported!** + +## Running OCIM in a docker container + +First, set your `.env` file as you would normally: +```env +DEBUG=True +OPENSTACK_USER='username' +OPENSTACK_PASSWORD='password' +OPENSTACK_TENANT='tenant-name' +OPENSTACK_AUTH_URL='https://auth.cloud.ovh.net/v2.0' +OPENSTACK_REGION='BHS1' +OPENSTACK_SANDBOX_SSH_KEYNAME='keypair-name' +DEFAULT_INSTANCE_BASE_DOMAIN='example.com' +GANDI_API_KEY='api-key' +GITHUB_ACCESS_TOKEN='github-token' +SECRET_KEY='tests' +DEFAULT_INSTANCE_MYSQL_URL=... +DEFAULT_RABBITMQ_API_URL=... +DEFAULT_INSTANCE_RABBITMQ_URL=... +DEFAULT_MONGO_REPLICA_SET_USER=... +DEFAULT_MONGO_REPLICA_SET_PASSWORD=... +DEFAULT_MONGO_REPLICA_SET_NAME=... +DEFAULT_MONGO_REPLICA_SET_PRIMARY=... +DEFAULT_MONGO_REPLICA_SET_HOSTS=... +REDIS_URL=... +``` + +Start a one-off container for setup of the backend: +```sh +# --rm destroys the container at the end +docker-compose run --rm ocim bash + +# then, inside the container... + +# initialize datastores +make migrate +# create super user +make manage createsuperuser +# exit the container +exit +``` + +Then, start another one-off container for setting up the client-facing frontend: +```sh +docker-compose run --rm ocim-frontend bash -c 'npm run build-api-client && npm install' +``` + +Then, start OCIM: +```sh +docker-compose up +``` + +The OCIM UI should be available at http://localhost:5000 . +The registration UI should be available at http://localhost:3000 . \ No newline at end of file diff --git a/documentation/installation.md b/documentation/installation.md index e4b1c7c3a..80e9173f8 100644 --- a/documentation/installation.md +++ b/documentation/installation.md @@ -1,7 +1,6 @@ -Installation ------------- +# Installation -### Vagrant installation +## Vagrant installation For development, we recommend using [Vagrant](https://www.vagrantup.com/) to automatically provision a development environment in a virtual machine. This diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 000000000..16d8d68f4 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,2 @@ +build/ +node_modules/ \ No newline at end of file diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 000000000..fded60e44 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,19 @@ +FROM node:11.15.0 + +# https://github.com/Automattic/node-canvas#compiling +RUN apt-get update \ + && apt-get install -y \ + build-essential \ + libcairo2-dev \ + libpango1.0-dev \ + libjpeg-dev \ + libgif-dev \ + librsvg2-dev + +WORKDIR /usr/src/ocim-frontend +COPY ./ /usr/src/ocim-frontend + +# Move node_modules out of the source path +RUN npm install -g --unsafe-perm + +CMD [ "npm", "start" ] \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index d2c47495a..edd31ac26 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -19,3 +19,5 @@ nav: - howtos.md - periodic_builds.md - infrastructure.md + - "Development": + - "Docker": development/docker.md diff --git a/requirements/dev.txt b/requirements/dev.txt index 2ee1ef97f..88db05cd0 100644 --- a/requirements/dev.txt +++ b/requirements/dev.txt @@ -4,7 +4,7 @@ # # pip-compile --output-file=requirements/dev.txt requirements/dev.in # --e git+https://github.com/open-craft/django-angular.git@v1-with-django2-support#egg=django_angular +git+https://github.com/open-craft/django-angular.git@v1-with-django2-support#egg=django_angular # via -r requirements/base.in aioredis==1.3.1 # via channels-redis