From 845c9f46f8e0f9be733590aee13fddf1f667ad15 Mon Sep 17 00:00:00 2001 From: Vir-Cotto Date: Fri, 27 Oct 2017 04:50:32 -0400 Subject: [PATCH] First attempt at a docker development environment (#334) * First attempt at a docker development environment * Adding a section in the development docs about docker development defaults --- .dockerignore | 21 +++++++++++ .gitignore | 4 ++ docker/dev/Dockerfile.django | 28 ++++++++++++++ docker/dev/Dockerfile.npm | 15 ++++++++ docker/dev/docker-compose.yml.example | 45 ++++++++++++++++++++++ docker/dev/docker-entrypoint.sh.django | 52 ++++++++++++++++++++++++++ docker/dev/docker-entrypoint.sh.npm | 37 ++++++++++++++++++ docs/development.rst | 45 +++++++++++++++++++++- 8 files changed, 245 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 docker/dev/Dockerfile.django create mode 100644 docker/dev/Dockerfile.npm create mode 100644 docker/dev/docker-compose.yml.example create mode 100755 docker/dev/docker-entrypoint.sh.django create mode 100755 docker/dev/docker-entrypoint.sh.npm diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..086b43d84 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,21 @@ +__pycache__ +*.log +pip-log.txt +.coverage +*.mo +*.pot +.idea +node_modules/ +socialhome/media/ +.cache +env.local +socialhome/static/css/*.css +socialhome/static/js/project.js +socialhome/static/js/webpack.*.js +socialhome/static/js/webpack.*.js.map +socialhome/static/fonts/ +staticfiles/ +bower_components/ +socialhome/static/mocha/ +/docs/_build/ +/yarn.lock diff --git a/.gitignore b/.gitignore index 20de02dcd..924ec4f46 100644 --- a/.gitignore +++ b/.gitignore @@ -89,3 +89,7 @@ socialhome/static/mocha/ /docs/_build/ /yarn.lock /staticfiles/ + +# Docker +docker-compose.yml + diff --git a/docker/dev/Dockerfile.django b/docker/dev/Dockerfile.django new file mode 100644 index 000000000..1bf2a569a --- /dev/null +++ b/docker/dev/Dockerfile.django @@ -0,0 +1,28 @@ +FROM python:3 + +RUN mkdir /code +WORKDIR /code +COPY . . + +# Ensure pip and setuptools are up to date as well +# We need a slightly older setuptools due to a bug in pip-tools +RUN pip install -U pip setuptools==30.4 pip-tools + +# Development environment +RUN pip-sync dev-requirements.txt + +## # We need Node for Javascript. Install it and then grab the latest version +## RUN apt-get install -y npm +## RUN npm cache clean -f +## RUN npm install -g n +## RUN n stable + +RUN cp .env.example .env + +# This file needs to be outside the /code dir as it's mounted durin development +RUN cp docker/dev/docker-entrypoint.sh.django ./docker-entrypoint.sh + +EXPOSE 8000 + +ENTRYPOINT ["./docker-entrypoint.sh"] +CMD ["runserver"] diff --git a/docker/dev/Dockerfile.npm b/docker/dev/Dockerfile.npm new file mode 100644 index 000000000..1b968238e --- /dev/null +++ b/docker/dev/Dockerfile.npm @@ -0,0 +1,15 @@ +FROM node + +RUN mkdir /code + +WORKDIR /code +COPY . . + +RUN npm install +RUN node_modules/.bin/bower --allow-root install +RUN node_modules/.bin/grunt dev + +RUN cp docker/dev/docker-entrypoint.sh.npm ./docker-entrypoint.sh + +ENTRYPOINT ["./docker-entrypoint.sh"] +CMD ["npm", "watch"] diff --git a/docker/dev/docker-compose.yml.example b/docker/dev/docker-compose.yml.example new file mode 100644 index 000000000..57f48c60d --- /dev/null +++ b/docker/dev/docker-compose.yml.example @@ -0,0 +1,45 @@ +version: '3' + +services: + postgres: + image: postgres:9.6 + volumes: + - pg_data:/var/lib/postgresql/data + - pg_backups:/pg_backups + environment: + - POSTGRES_USER=socialhome + - POSTGRES_PASSWORD=socialhome + - POSTGRES_DB=socialhome + + redis: + image: redis + volumes: + - redis_data:/data + npm: + build: + context: . + dockerfile: docker/dev/Dockerfile.npm + volumes: + - ./socialhome:/code/socialhome + django: + depends_on: + - postgres + - redis + build: + context: . + dockerfile: docker/dev/Dockerfile.django + environment: + - REDIS_HOST=redis + - REDIS_PORT=6379 + - DATABASE_URL=postgres://socialhome:socialhome@postgres:5432/socialhome + - DEBUG=True + - DJANGO_SECRET_KEY=ForDevelopmentEyesOnly + volumes: + - ./socialhome:/code/socialhome + ports: + - 8000:8000 + +volumes: + pg_data: {} + pg_backups: {} + redis_data: {} diff --git a/docker/dev/docker-entrypoint.sh.django b/docker/dev/docker-entrypoint.sh.django new file mode 100755 index 000000000..2dbc68107 --- /dev/null +++ b/docker/dev/docker-entrypoint.sh.django @@ -0,0 +1,52 @@ +#!/bin/bash +# +# Socialhome Docker Development entrypoint + +# Exit immediately if a command exits with a non-zero status. +set -e + +PORT=8000 +CODE_DIR=/code + +cd $CODE_DIR + +# Define help message +show_help() { + echo """ +Usage: docker run COMMAND +Commands +runserver : Run Django development server +bash : Start a bash shell +manage : Run a Django management command +python : Run a python command +shell : Start a Django Python shell +help : Show this message +""" +} + +# Run +case "$1" in + runserver) + echo "Running Development Server..." + python manage.py runserver 0.0.0.0:${PORT} + ;; + bash) + /bin/bash "${@:2}" + ;; + manage) + pwd + echo "Running manage:" "${@:2}" + python manage.py "${@:2}" + ;; + python) + echo "Running python command..." "${@:2}" + python "${@:2}" + ;; + shell) + echo "Running shell_plus..." + python manage.py shell_plus + ;; + *) + show_help + ;; +esac diff --git a/docker/dev/docker-entrypoint.sh.npm b/docker/dev/docker-entrypoint.sh.npm new file mode 100755 index 000000000..6abdc58f2 --- /dev/null +++ b/docker/dev/docker-entrypoint.sh.npm @@ -0,0 +1,37 @@ +#!/bin/bash +# +# Socialhome Docker Development entrpypoint +# +# Exit immediately if a command exits with a non-zero status. +set -e + +# Define help message +show_help() { + echo """ +Usage: docker run COMMAND +Commands +npm : Run an NPM command +watch : Run npm watch +bash : Start a bash shell +help : Show this message +""" +} + +# Run +case "$1" in + npm) + echo "Running npm:" "${@:2}" + npm "${@:2}" + ;; + watch) + echo "Running npm run watch" + npm run watch + ;; + bash) + echo "Running bash" "${@:2}" + /bin/bash "${@:2}" + ;; + *) + show_help + ;; +esac diff --git a/docs/development.rst b/docs/development.rst index bbec3e146..4d58b4816 100644 --- a/docs/development.rst +++ b/docs/development.rst @@ -148,8 +148,8 @@ Execute the following to run the new frontend JavaScript tests. npm run test -API routes ----------- +API Routes +----------- There is a dependency in the API route URL configurations with the new Vue based frontend tests. If you change or add new API routes during development, you must also do the following: @@ -203,6 +203,47 @@ Then execute the following and copy the markdown version for pasting to GitHub r After the release commit has been pushed and a release has been tagged, set a development version in the same above files. This is basically the next minor release postfixed by ``-dev``. +Developing with Docker +------------------------ + +If you choose, you may develop Socialhome using Docker, rather than installing Postgres and Redis manually on your computer. + + +Supported versions +.................. + +This guide assumes you are running Docker on a GNU/Linux based system such as Ubuntu, Debian or Fedora Linux. It may be possible to run this on other platforms where Docker is supported, but those are untested. + +The docker development installation was tested on Docker version 17.09 and docker-compose 1.16.1. + +Steps +...... + +The first step is to copy the example docker-compose file ``docker/dev/docker-compose.yml.example`` file to the root of the project. eg + +``cp docker/dev/docker-compose.yml.example ./docker-compose.yml`` + +You also need to set an .env file as per the above instructions. Use the ``.env.example`` as a starting point. + +From there, you can build the images: + +``docker-compose build`` + +And then the steps you would normally do, but throught he django image, ala: + +``docker-compose run django manage migrate`` +and +``docker-compose run django manage createsuperuser`` + +And then just + +``docker-compose up`` + +Defaults +.......... + +The defaults are that that the Docker image will be running on port 8000 and then exposed to the host OS on the same port (ie you can browse to http;//localhost:8000 to see the Django instance running). Redis and Postgres will be running but not exposed to the host OS by default. These can be changed on the ``docker-compose.yml`` file. + Contact for help ----------------