Navigation Menu

Skip to content

Commit

Permalink
docker architecture update for version 2.1 (#43)
Browse files Browse the repository at this point in the history
* docker architecture update for version 2.1
* docker-compose update
* moving Dockerfile and requirments.txt to docker folder
* access to Dockerfile via context
* applying @lechup suggestions to PR
* remove container build from travis
* versioning postgis image
* readme update
* travis fix
* requirements.txt updated with pip-complie
  • Loading branch information
grillazz authored and lechup committed Feb 6, 2017
1 parent e30ca0b commit 7e3cca0
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 266 deletions.
2 changes: 0 additions & 2 deletions .gitignore
@@ -1,5 +1,3 @@
docker-compose.yml

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
30 changes: 23 additions & 7 deletions .travis.yml
@@ -1,10 +1,26 @@
sudo: required

language: python

python:
- "3.5"

services:
- docker
- docker

env:
global:
- DOCKER_VERSION=1.13.0-0~ubuntu-trusty
- DOCKER_COMPOSE_VERSION=1.10.0

before_install:
# upgrade docker-engine to specific version
- sudo apt-get -o Dpkg::Options::="--force-confnew" install -y docker-engine=${DOCKER_VERSION}

# reinstall docker-compose at specific version
- sudo curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- sudo chmod +x /usr/local/bin/docker-compose

script:
- ln -s ./docker/docker-compose.yml ./docker-compose.yml
- docker-compose build
# test if we are able to setup database schema properly
# should catch most typical errors in migrations
- docker-compose run --rm web python manage.py migrate
- docker-compose run --rm web py.test --cov=.
- docker-compose run --rm test

2 changes: 1 addition & 1 deletion EnviroMonitorWeb/settings/base.py
Expand Up @@ -82,7 +82,7 @@
'NAME': 'docker',
'USER': 'docker',
'PASSWORD': 'docker',
'HOST': 'postgis'
'HOST': 'db'
}
}

Expand Down
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -10,20 +10,20 @@ You can up and runnig this project in a traditional way with virtual environment

## To start development:
1. install [docker](https://docs.docker.com/#/components) and [docker-compose](https://docs.docker.com/compose/install/)
2. clone and change working dir to project root `cd ~/ && git clone git@github.com:EnviroMonitor/EnviroMonitorWeb.git && cd EnviroMonitorWeb`
3. copy/symlink docker-compose.yml `cp ./docker/docker-compose.yml ./docker-compose.yml` (we want to be able to override as dev)
4. run `docker-compose build` to build web container
5. run `docker-compose run --rm web python manage.py migrate`
6. run `docker-compose run --rm web python manage.py createsuperuser`
2. run `sudo docker-compose build` to build web container
3. run `sudo docker-compose up web` to test web and db containers
5. run `sudo docker-compose run web python manage.py migrate` to apply migrations
6. run `sudo docker-compose run web python manage.py createsuperuser` to create admin account

## To run project:
1. docker-compose up
1. run `docker-compose up web`
2. point Your browser to `localhost:8000`
3. press `CTRL+C` to stop

## Notes:
1. To run command inside container you can use run entrypoint command.
I.e. `docker-compose run --rm web py.test -s --cov=. --cov-report=html` to run unit tests and check coverage.
I.e. `sudo docker-compose run web py.test -s --cov=. --cov-report=html` to run unit tests and check coverage.
I.e. `sudo docker exec -it enviromonitorweb_db_1 psql -U docker -d docker` when you want access to database

## API documentaion:
Check http://localhost:8000/api/v1/docs/ to received full REST API documentation.
45 changes: 45 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,45 @@
version: '2.1'

services:

db:
build:
context: ./docker/db/
dockerfile: Dockerfile
environment:
POSTGRES_USER: docker
POSTGRES_PASSWORD: docker
POSTGRES_DB: docker
healthcheck:
test: "exit 0"

web:
build:
context: ./docker/web/
dockerfile: Dockerfile
user: root
depends_on:
db:
condition: service_healthy
volumes:
- .:/code/
ports:
- "8080:8000"
environment:
DJANGO_SETTINGS_MODULE: EnviroMonitorWeb.settings.dev
command: python manage.py runserver 0.0.0.0:8080
restart: unless-stopped

test:
build:
context: ./docker/web/
dockerfile: Dockerfile
user: root
depends_on:
db:
condition: service_healthy
volumes:
- .:/code/
environment:
DJANGO_SETTINGS_MODULE: EnviroMonitorWeb.settings.test
command: py.test -s --cov=.
5 changes: 5 additions & 0 deletions docker/db/Dockerfile
@@ -0,0 +1,5 @@
FROM mdillon/postgis:9.6
COPY db-healthcheck /usr/local/bin/
RUN chmod +x /usr/local/bin/db-healthcheck
HEALTHCHECK CMD ["db-healthcheck"]

13 changes: 13 additions & 0 deletions docker/db/README.md
@@ -0,0 +1,13 @@
## To manually monitor conatiner state use below command
`docker inspect --format='{{json .State.Health}}' enviromonitorweb_db_1`

I.e. message below:
```json
{"Status":"healthy","FailingStreak":0,"Log":[{"Start":"2017-02-02T18:50:42.83427782+01:00","End":"2017-02-02T18:50:42.869961142+01:00","ExitCode":0,"Output":""},{"Start":"2017-02-02T18:51:12.870148749+01:00","End":"2017-02-02T18:51:12.911524939+01:00","ExitCode":0,"Output":""}]}
```

With `docker ps` your container status should be healthy

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5f3cf5212031 enviromonitorweb_test "py.test -s --cov=." About a minute ago Restarting (0) 4 seconds ago enviromonitorweb_test_1
7765dc8099d9 enviromonitorweb_db "/docker-entrypoint.s" 2 minutes ago Up 2 minutes (healthy) 5432/tcp enviromonitorweb_db_1
19 changes: 19 additions & 0 deletions docker/db/db-healthcheck
@@ -0,0 +1,19 @@
#!/bin/bash
set -eo pipefail

host="$(hostname --ip-address || echo '127.0.0.1')"
user="${POSTGRES_USER:-postgres}"
export PGPASSWORD="${POSTGRES_PASSWORD:-}"

args=(
# force postgres to not use the local unix socket (test "external" connectibility)
--host "$host"
--username "$user"
--quiet --no-align --tuples-only
)

if select="$(echo 'SELECT 1' | psql "${args[@]}")" && [ "$select" = '1' ]; then
exit 0
fi

exit 1
18 changes: 0 additions & 18 deletions docker/docker-compose-base.yml

This file was deleted.

15 changes: 0 additions & 15 deletions docker/docker-compose.yml

This file was deleted.

37 changes: 5 additions & 32 deletions docker/web/Dockerfile
@@ -1,41 +1,14 @@
FROM python:3.5

ENV PYTHONUNBUFFERED 1
# change owner of all files created during any cmd,
# override them in Yours docker-compose.yml file
ENV DEFAULT_UID=1000
ENV DEFAULT_GID=1000

WORKDIR /code/

# update packages and install apt-reqs
RUN apt-get update && apt-get install -y --no-install-recommends \
# apt requirements
## common
g++ curl ca-certificates \
# pip apt requirements
## common
python3-dev \
## psycopg2
postgresql-server-dev-all \
## manage.py dbshell
postgresql-client \
## django.contrib.gis
binutils libproj-dev gdal-bin \
&& rm -rf /var/lib/apt/lists/*
binutils libproj-dev gdal-bin

RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
# get newest pip wheel and setuptools
RUN curl https://bootstrap.pypa.io/get-pip.py | python

# add pip dependencies
ADD requirements.txt /
RUN pip install -r /requirements.txt

# add cmd
ADD cmd.sh /
CMD ["/cmd.sh"]

# add entrypoint
ADD wait-for-it.sh /
ADD docker_entrypoint.sh /
ENTRYPOINT ["/docker_entrypoint.sh"]
RUN pip install -r requirements.txt
2 changes: 0 additions & 2 deletions docker/web/cmd.sh

This file was deleted.

11 changes: 0 additions & 11 deletions docker/web/docker_entrypoint.sh

This file was deleted.

60 changes: 50 additions & 10 deletions docker/web/requirements.txt
@@ -1,18 +1,58 @@
Django==1.10.4
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file requirements.txt requirements.txt
#
appdirs==1.4.0 # via setuptools
argh==0.26.2 # via watchdog
colorama==0.3.7 # via pytest-watch
coreapi==2.1.1 # via django-rest-swagger, openapi-codec
coverage==4.3.4 # via pytest-cov
decorator==4.0.11 # via ipython, traitlets
defusedxml==0.4.1 # via djangorestframework-xml
django-crispy-forms==1.6.1
django-extensions==1.7.5
django-geoposition==0.3.0
djangorestframework==3.5.3
djangorestframework-xml==1.3.0
django-filter==1.0.1
six==1.10.0
django-geoposition==0.3.0
django-rest-swagger==2.1.0
pytest==3.0.5
Django==1.10.4
djangorestframework-xml==1.3.0
djangorestframework==3.5.3
docopt==0.6.2 # via pytest-watch
factory-boy==2.8.1
Faker==0.7.7 # via factory-boy
ipython-genutils==0.1.0 # via traitlets
ipython==5.1.0
itypes==1.1.0 # via coreapi
olefile==0.44 # via pillow
openapi-codec==1.2.1 # via django-rest-swagger
packaging==16.8 # via setuptools
pathtools==0.1.2 # via watchdog
pexpect==4.2.1 # via ipython
pickleshare==0.7.4 # via ipython
Pillow==4.0.0
prompt-toolkit==1.0.13 # via ipython
psycopg2==2.6.2
ptyprocess==0.5.1 # via pexpect
py==1.4.32 # via pytest
pygments==2.2.0 # via ipython
pyparsing==2.1.10 # via packaging
pytest-cov==2.4.0
pytest-django==3.1.2
pytest-watch==4.1.0
factory-boy==2.8.1
psycopg2==2.6.2
ipython==5.1.0
pytest==3.0.5
python-dateutil==2.6.0 # via faker
PyYAML==3.12 # via watchdog
requests==2.13.0 # via coreapi
simplegeneric==0.8.1 # via ipython
simplejson==3.10.0 # via django-rest-swagger
six==1.10.0
tqdm==4.11.2
Pillow==4.0.0
traitlets==4.3.1 # via ipython
uritemplate==3.0.0 # via coreapi
watchdog==0.8.3 # via pytest-watch
wcwidth==0.1.7 # via prompt-toolkit

# The following packages are considered to be unsafe in a requirements file:
# setuptools # via ipython

0 comments on commit 7e3cca0

Please sign in to comment.