From f9fb841d10db01391b8bab1d1d87e2c109c3642c Mon Sep 17 00:00:00 2001 From: Kraig Brockschmidt Date: Fri, 21 Sep 2018 10:27:06 -0700 Subject: [PATCH 1/5] Add Dockerfile and uwsgi.ini --- .dockerignore | 11 +++++++++++ Dockerfile | 12 ++++++++++++ uwsgi.ini | 10 ++++++++++ 3 files changed, 33 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 uwsgi.ini diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..57da962 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +node_modules +npm-debug.log +Dockerfile* +docker-compose* +.dockerignore +.git +.gitignore +README.md +LICENSE +.vscode +env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3bb9523 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +# Pull a pre-built alpine docker image with nginx and python3 installed +FROM tiangolo/uwsgi-nginx:python3.6-alpine3.7 + +ENV LISTEN_PORT=8000 +EXPOSE 8000 + +# Copy the app files to a folder and run it from there +WORKDIR /app +ADD . /app + +RUN python3 -m pip install -r requirements.txt +CMD ["uwsgi", "uwsgi.ini"] diff --git a/uwsgi.ini b/uwsgi.ini new file mode 100644 index 0000000..fb31f32 --- /dev/null +++ b/uwsgi.ini @@ -0,0 +1,10 @@ +[uwsgi] +chdir=. +module=web_project.wsgi:application +protocol=http +env=DJANGO_SETTINGS_MODULE=web_project.settings +socket=127.0.0.1:8000 +uid=1000 +master=true +plugins=python3 + From 81944b19e2f54a630231d812e092d9ab04a61ced Mon Sep 17 00:00:00 2001 From: Kraig Brockschmidt Date: Fri, 21 Sep 2018 11:54:17 -0700 Subject: [PATCH 2/5] Update requirements and uwsgi.ini --- requirements.txt | 2 ++ uwsgi.ini | 18 +++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/requirements.txt b/requirements.txt index ef52540..21e7273 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ Django==2.1.1 +pkg-resources==0.0.0 pytz==2018.5 +uWSGI=2.0.17.1 diff --git a/uwsgi.ini b/uwsgi.ini index fb31f32..1d12e0d 100644 --- a/uwsgi.ini +++ b/uwsgi.ini @@ -1,10 +1,10 @@ [uwsgi] -chdir=. -module=web_project.wsgi:application -protocol=http -env=DJANGO_SETTINGS_MODULE=web_project.settings -socket=127.0.0.1:8000 -uid=1000 -master=true -plugins=python3 - +chdir = . +module = web_project.wsgi:application +env = DJANGO_SETTINGS_MODULE=web_project.settings +protocol = http +socket = 127.0.0.1:8000 +uid = 1000 +master = true +threads = 2 +processes = 4 From b66ac97e633f2ec44b5dd9b3ebd5b31f55adf72f Mon Sep 17 00:00:00 2001 From: Kraig Brockschmidt Date: Fri, 21 Sep 2018 12:17:39 -0700 Subject: [PATCH 3/5] Simplify requirements.txt --- requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 21e7273..ef52540 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,2 @@ Django==2.1.1 -pkg-resources==0.0.0 pytz==2018.5 -uWSGI=2.0.17.1 From 07f1e47b0c0baa63ae53b6d4dcf4e77972173934 Mon Sep 17 00:00:00 2001 From: Kraig Brockschmidt Date: Wed, 26 Sep 2018 17:08:06 -0700 Subject: [PATCH 4/5] Final changes to make it work with Azure App Service --- Dockerfile | 27 ++++++++++++++++++++++++++- uwsgi.ini | 2 -- web_project/settings.py | 4 ++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3bb9523..655c009 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,37 @@ # Pull a pre-built alpine docker image with nginx and python3 installed FROM tiangolo/uwsgi-nginx:python3.6-alpine3.7 +# Set the port on which the app runs; make both values the same. +# +# IMPORTANT: When deploying to Azure App Service, go to the App Service on the Azure +# portal, navigate to the Applications Settings blade, and create a setting named +# WEBSITES_PORT with a value that matches the port here (the Azure default is 80). +# You can also create a setting through the App Service Extension in VS Code. ENV LISTEN_PORT=8000 EXPOSE 8000 +# Indicate where uwsgi.ini lives +ENV UWSGI_INI uwsgi.ini + +# Tell nginx where static files live. Typically, developers place static files for +# multiple apps in a shared folder, but for the purposes here we can use the one +# app's folder. Note that when multiple apps share a folder, you should create subfolders +# with the same name as the app underneath "static" so there aren't any collisions +# when all those static files are collected together, as when using Django's +# collectstatic command. +ENV STATIC_URL /app/static_collected + # Copy the app files to a folder and run it from there WORKDIR /app ADD . /app +# Make app folder writeable for the sake of db.sqlite3, and make that file also writeable. +# Ideally you host the database somewhere else so that the app folders can remain read only. +# Without these permissions you see the errors "unable to open database file" and +# "attempt to write to a readonly database", respectively, whenever the app attempts to +# write to the database. +RUN chmod g+w /app +RUN chmod g+w /app/db.sqlite3 + +# Make sure dependencies are installed RUN python3 -m pip install -r requirements.txt -CMD ["uwsgi", "uwsgi.ini"] diff --git a/uwsgi.ini b/uwsgi.ini index 1d12e0d..fdff29b 100644 --- a/uwsgi.ini +++ b/uwsgi.ini @@ -2,8 +2,6 @@ chdir = . module = web_project.wsgi:application env = DJANGO_SETTINGS_MODULE=web_project.settings -protocol = http -socket = 127.0.0.1:8000 uid = 1000 master = true threads = 2 diff --git a/web_project/settings.py b/web_project/settings.py index e42f6c7..510c991 100644 --- a/web_project/settings.py +++ b/web_project/settings.py @@ -23,8 +23,12 @@ SECRET_KEY = '2gult1d96#@#b2%tz+k9x1q%-4(%f@va-!sbv*q&$t^gpp8-_=' # SECURITY WARNING: don't run with debug turned on in production! +# If you set to False, also add "localhost" to ALLOWED_HOSTS or else +# you'll get "Bad Request" when running locally. DEBUG = True +# When deploying to Azure App Service, add you .azurewebsites.net +# domain to ALLOWED_HOSTS; you get an error message if you forget. ALLOWED_HOSTS = [] From 130c3e4152756c041b22803c84d00638b67124c9 Mon Sep 17 00:00:00 2001 From: Kraig Brockschmidt Date: Wed, 26 Sep 2018 17:09:40 -0700 Subject: [PATCH 5/5] Add note about dockerfile addition --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 48bdf12..1449a18 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ This sample contains the completed program from the tutorial, [Using Django in Visual Studio Code](https://code.visualstudio.com/docs/python/tutorial-django). Intermediate steps are not included. +The sample also includes a Dockerfile to build a production-ready container image that uses uwsgi and nginx; the uwsgi.ini file provides uwsgi configuration. + To run the sample: 1. Create a virtual environment as described in the tutorial.