From 2000e31e123847a7441ee3dfa9c2566689d3aa51 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 15 Apr 2018 16:02:55 +0200 Subject: [PATCH 1/6] [#5] run django using docker-compose --- Dockerfile | 7 +++++++ README.md | 20 ++++++++++++++++++++ docker-compose.yml | 14 ++++++++++++++ manage.py | 21 ++++++++++++++------- requirements.txt | 2 ++ 5 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml mode change 100644 => 100755 manage.py create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b45a393 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3 +ENV PYTHONUNBUFFERED 1 +RUN mkdir /code +WORKDIR /code +ADD requirements.txt /code/ +RUN pip install -r requirements.txt +ADD . /code/ diff --git a/README.md b/README.md index 7cb6c84..43f05b2 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,25 @@ # CryptoCurrencyRestAPI +## docker-compose + +https://docs.docker.com/compose/django/#connect-the-database + +create project + +``` +docker-compose run web django-admin.py startproject composeexample . +``` + +run docker-compose + +``` +docker-compose up +``` + +and navigate to 127.0.0.1:8000 + +## deployment + Running on DigitalOcean droplet, adress: http://206.189.12.80/ ## How was this Django project deployed? diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1bc76f9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3' + +services: + db: + image: postgres + web: + build: . + command: python3 manage.py runserver 0.0.0.0:8000 + volumes: + - .:/code + ports: + - "8000:8000" + depends_on: + - db diff --git a/manage.py b/manage.py old mode 100644 new mode 100755 index 731ccab..0f61484 --- a/manage.py +++ b/manage.py @@ -3,13 +3,20 @@ import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CryptoCurrencyRestAPI.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "composeexample.settings") try: from django.core.management import execute_from_command_line - except ImportError as exc: - raise ImportError( - "Couldn't import Django. Are you sure it's installed and " - "available on your PYTHONPATH environment variable? Did you " - "forget to activate a virtual environment?" - ) from exc + except ImportError: + # The above import may fail for some other reason. Ensure that the + # issue is really that Django is missing to avoid masking other + # exceptions on Python 2. + try: + import django + except ImportError: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) + raise execute_from_command_line(sys.argv) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..71d0fbc --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Django>=1.8,<2.0 +psycopg2 From 082047fbe485cdbed2be73f22c27d71c567a5c89 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 15 Apr 2018 16:44:35 +0200 Subject: [PATCH 2/6] [#5] django 2.0.4 --- .gitignore | 4 +++- CryptoCurrencyRestAPI/settings.py | 2 +- README.md | 2 +- manage.py | 21 +++++++-------------- requirements.txt | 2 +- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index f5e96db..b382089 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -venv \ No newline at end of file +venv +__pycache__ +db.sqlite3 diff --git a/CryptoCurrencyRestAPI/settings.py b/CryptoCurrencyRestAPI/settings.py index ee3dc7d..3234f58 100644 --- a/CryptoCurrencyRestAPI/settings.py +++ b/CryptoCurrencyRestAPI/settings.py @@ -20,7 +20,7 @@ # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'nydy(tk6kg8lq+y9vf!@$qvmwlb*21tlp5)2pwf5kc1dyn1zbd' +SECRET_KEY = '5=0gcw=t^vjrr%i1$!ffv0i=cn)r&!oza%x+@yb39z9gnr&js@' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True diff --git a/README.md b/README.md index 43f05b2..52c226c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ https://docs.docker.com/compose/django/#connect-the-database create project ``` -docker-compose run web django-admin.py startproject composeexample . +docker-compose run web django-admin.py startproject CryptoCurrencyRestAPI . ``` run docker-compose diff --git a/manage.py b/manage.py index 0f61484..731ccab 100755 --- a/manage.py +++ b/manage.py @@ -3,20 +3,13 @@ import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "composeexample.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CryptoCurrencyRestAPI.settings") try: from django.core.management import execute_from_command_line - except ImportError: - # The above import may fail for some other reason. Ensure that the - # issue is really that Django is missing to avoid masking other - # exceptions on Python 2. - try: - import django - except ImportError: - raise ImportError( - "Couldn't import Django. Are you sure it's installed and " - "available on your PYTHONPATH environment variable? Did you " - "forget to activate a virtual environment?" - ) - raise + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc execute_from_command_line(sys.argv) diff --git a/requirements.txt b/requirements.txt index 71d0fbc..4bec01f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -Django>=1.8,<2.0 +Django==2.0.4 psycopg2 From 0961b8164d2e123af60eceedbda3417b152f9e81 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 15 Apr 2018 16:49:35 +0200 Subject: [PATCH 3/6] [#5] ensure that owner and group are correct --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 52c226c..b145d21 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,12 @@ create project docker-compose run web django-admin.py startproject CryptoCurrencyRestAPI . ``` +change owner and group + +``` +sudo chown $USER:$USER -R . +``` + run docker-compose ``` From 662a1696734f7545ed5d94634368b4c67166dff7 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 15 Apr 2018 16:51:18 +0200 Subject: [PATCH 4/6] [#5] connect to postgres docker --- CryptoCurrencyRestAPI/settings.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CryptoCurrencyRestAPI/settings.py b/CryptoCurrencyRestAPI/settings.py index 3234f58..cbd32d0 100644 --- a/CryptoCurrencyRestAPI/settings.py +++ b/CryptoCurrencyRestAPI/settings.py @@ -75,8 +75,11 @@ DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'postgres', + 'USER': 'postgres', + 'HOST': 'db', + 'PORT': 5432, } } From 78ae232b6814885cf7041087c5a3050ccdcdd197 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 15 Apr 2018 17:47:30 +0200 Subject: [PATCH 5/6] [#5] using postgres 10.3 --- docker-compose.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 1bc76f9..deed22f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,13 +2,17 @@ version: '3' services: db: - image: postgres + image: postgres:10.3 + ports: + - 5432:5432 + volumes: + - ./pgdata:/var/lib/postgresql/data/ web: build: . command: python3 manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - - "8000:8000" + - 8000:8000 depends_on: - db From 7e7de71fc1091f957eddc539e4c4771ce66612f3 Mon Sep 17 00:00:00 2001 From: 030 Date: Sun, 15 Apr 2018 18:25:47 +0200 Subject: [PATCH 6/6] [#5] prevent that .idea files will be committed --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b382089..8071609 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea venv __pycache__ db.sqlite3