Skip to content

Improve dev setup via virtualenv and docker #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SECRET_KEY=devkey
ENV=dev

DATABASE_URL=postgres://pythonph:password@db/pythonph

SLACK_ORG=pythonph
SLACK_API_TOKEN=xxxx-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxx-xxxxxxxxxx
SLACK_BOARD_CHANNEL=pythonph
SLACK_JOBS_CHANNEL=jobs
9 changes: 9 additions & 0 deletions .env.virtualenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SECRET_KEY=devkey
ENV=dev

DATABASE_URL=sqlite:///db.sqlite3

SLACK_ORG=pythonph
SLACK_API_TOKEN=xxxx-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxx-xxxxxxxxxx
SLACK_BOARD_CHANNEL=pythonph
SLACK_JOBS_CHANNEL=jobs
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ npm-debug.log

# docker-compose
*.env
db.sqlite3
27 changes: 12 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
FROM python:3.8.1
FROM nikolaik/python-nodejs:python3.8-nodejs12

RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get -y install nodejs
RUN apt-get -y install libcairo-dev

RUN mkdir -p /usr/src/app

COPY package.json /usr/src/app/
RUN npm install --prefix /usr/src/app/
WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app
RUN pip install -r /usr/src/app/requirements.txt
COPY . /usr/src/app/
RUN pip install -r requirements/docker.txt \
&& npm install \
&& npm run build-jobs \
&& python manage.py compress --force \
&& python manage.py collectstatic --noinput

COPY . /usr/src/app
WORKDIR /usr/src/app
RUN npm run-script build-jobs
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
EXPOSE 8000
CMD ["runprod"]
90 changes: 48 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,68 @@
# PythonPH

## Development Setup
## Development Setup via virtualenv

1. Setup db
1. Make a copy of `.env.virtualenv` as `.env`.

```bash
createuser -P pythonph
# You will be prompted to enter a password
# Enter what you set in POSTGRES_PASSWORD
createdb -O pythonph pythonph
```
```bash
$ cp .env.virtualenv .env
```

2. Setup virtualenv
2. Create and activate your virtualenv.

```bash
mkvirtualenv venv
venv/bin/pip install -r requirements.txt
```
```bash
$ virtualenv venv
$ source venv/bin/activate
```

3. Setup npm
3. Install python packages.

```bash
npm install
```
```bash
$ pip install -r requirements/virtualenv.txt
```

4. Create `dev.env`
4. Run Django migrations.

```bash
SECRET_KEY=secret
ENV=DEV
POSTGRES_USER=pythonph
POSTGRES_PASSWORD=password
SLACK_ORG=pythonph
SLACK_API_TOKEN=xxxx-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxxx-xxxxxxxxxx
SLACK_BOARD_CHANNEL=pythonph
SLACK_JOBS_CHANNEL=jobs
```
```bash
$ python manage.py migrate
```

5. Setup Django
5. Run Django server.

```bash
bin/localmanage migrate
bin/localmanage createsuperuser
```
```bash
$ python manage.py runserver
```

6. Run server
6. Install npm packages and run build watcher (Optional, only run if you need to work on JS).

```bash
npm start
```
```bash
$ npm install
$ npm run dev
```

## Development Setup via docker-compose
1. `./bin/build-dev`
2. `./bin/deploy-dev`

Note: For this setup, you have to run `./bin/build-dev && ./bin/deploy-dev` for changes to reflect.
1. Make a copy of `.env.docker` as `.env`.

```bash
$ cp .env.docker .env
```

## Todo
1. Improve dockerized development setup
2. Build images and run containers.

```bash
$ docker-compose build
$ docker-compose up -d
```

3. Run migrations

```bash
$ docker-compose run --rm web migrate
```

4. Running Django's manage.py to run other commands

```bash
$ docker-compose run --rm web python manage.py <command>
```
5 changes: 2 additions & 3 deletions bin/build
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/bash
set -e

docker build --rm -t pythonph/pythonph .
COMPOSE="docker-compose -f docker-compose-prod.yml"
$COMPOSE build nginx
$COMPOSE up -d source
$COMPOSE build
$COMPOSE up -d
$COMPOSE logs source
9 changes: 4 additions & 5 deletions bin/build-dev
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/bin/bash
set -e

docker build --rm -t pythonph/pythonph .
COMPOSE="docker-compose -f docker-compose-dev.yml"
$COMPOSE build nginx
$COMPOSE up -d source
$COMPOSE logs source
COMPOSE="docker-compose -f docker-compose.yml"
$COMPOSE build
$COMPOSE up -d
$COMPOSE logs web
4 changes: 2 additions & 2 deletions bin/deploy-dev
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
set -e

COMPOSE="docker-compose -f docker-compose-dev.yml"
$COMPOSE up -d --force-recreate web nginx db
COMPOSE="docker-compose -f docker-compose.yml"
$COMPOSE up -d --force-recreate web
2 changes: 1 addition & 1 deletion bin/install
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -e

pip install -r requirements.txt
pip install -r requirements/docker.txt

mkdir -p static/jobs/js
npm install
Expand Down
38 changes: 0 additions & 38 deletions docker-compose-dev.yml

This file was deleted.

41 changes: 14 additions & 27 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
source:
image: pythonph/pythonph
volumes:
- /usr/src/app/venv
- /usr/src/app/node_modules
- /usr/src/app/bower_components
- /usr/src/app/static
environment:
- ENV=PROD
env_file: prod.env
command: bin/install
services:
web:
build: .
image: pythonph/pythonph
container_name: pythonph_web
command: runprod

web:
image: pythonph/pythonph
volumes_from:
- source
environment:
- ENV=PROD
env_file: prod.env
command: gunicorn -b 0.0.0.0:8000 --access-logfile - --error-logfile - pythonph.wsgi

nginx:
build: nginx
ports:
- 80:80
- 443:443
links:
- web:web
nginx:
image: nginx:latest
container_name: pythonph_nginx
ports:
- 80:80
- 443:443
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
42 changes: 22 additions & 20 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
db:
image: postgres:latest
environment:
- POSTGRES_USER=pythonph
env_file: dev.env
services:
web:
build: .
image: pythonph/pythonph
container_name: pythonph_web
command: rundev
ports:
- 8000:8000
volumes:
- ./:/usr/src/app/

web:
build: .
volumes:
- .:/usr/src/app
links:
- db:db
environment:
- PYTHONUNBUFFERED=1
- ENV=DEV
- POSTGRES_HOST=db
- POSTGRES_USER=pythonph
env_file: dev.env
ports:
- 8000:8000
command: python manage.py runserver 0.0.0.0:8000
db:
image: postgres:14.2-alpine
container_name: pythonph_db
environment:
- POSTGRES_DB=pythonph
- POSTGRES_USER=pythonph
- POSTGRES_PASSWORD=password
volumes:
- pg-volume:/var/lib/postgresql/data/

volumes:
pg-volume:
29 changes: 29 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env sh
set -e

if [ "$1" = "migrate" ]; then
python manage.py migrate
exit 0
fi

if [ "$1" = "makemigrations" ]; then
python manage.py makemigrations
exit 0
fi

if [ "$1" = "createsuperuser" ]; then
python manage.py createsuperuser
exit 0
fi

if [ "$1" = "rundev" ]; then
npm run dev & python manage.py runserver 0.0.0.0:8000
exit 0
fi

if [ "$1" = "runprod" ]; then
gunicorn -b 0.0.0.0:8000 --access-logfile - --error-logfile - pythonph.wsgi
exit 0
fi

exec "$@"
2 changes: 0 additions & 2 deletions nginx/Dockerfile

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"scripts": {
"watch-jobs": "watchify jobs/static/jobs/js/src/main.js -o jobs/static/jobs/js/dist/main.js -v",
"build-jobs": "browserify jobs/static/jobs/js/src/main.js -o jobs/static/jobs/js/dist/main.js",
"start": "npm run watch-jobs & bin/localmanage runserver $PORT"
"start": "npm run watch-jobs & bin/localmanage runserver $PORT",
"dev": "npm run watch-jobs"
},
"repository": {
"type": "git",
Expand Down
Loading