Skip to content

Commit

Permalink
First attempt at a docker development environment (#334)
Browse files Browse the repository at this point in the history
* First attempt at a docker development environment
* Adding a section in the development docs about docker development defaults
  • Loading branch information
Vir-Cotto authored and jaywink committed Oct 27, 2017
1 parent 4c1b068 commit 845c9f4
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 2 deletions.
21 changes: 21 additions & 0 deletions .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
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -89,3 +89,7 @@ socialhome/static/mocha/
/docs/_build/
/yarn.lock
/staticfiles/

# Docker
docker-compose.yml

28 changes: 28 additions & 0 deletions 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"]
15 changes: 15 additions & 0 deletions 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"]
45 changes: 45 additions & 0 deletions 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: {}
52 changes: 52 additions & 0 deletions 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 <imagename> 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
37 changes: 37 additions & 0 deletions 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 <imagename> 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
45 changes: 43 additions & 2 deletions docs/development.rst
Expand Up @@ -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:

Expand Down Expand Up @@ -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
----------------

Expand Down

0 comments on commit 845c9f4

Please sign in to comment.