-
Notifications
You must be signed in to change notification settings - Fork 22
Use uWSGI to serve the app in production #14
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,30 @@ | |
|
|
||
| FROM python:3.5-alpine | ||
|
|
||
| RUN addgroup -g 1001 app && \ | ||
| adduser -D -u 1001 -G app -s /usr/sbin/nologin app | ||
| MAINTAINER mars@mozilla.com | ||
| # These are unlikely to change from version to version of the container | ||
| EXPOSE 9000 | ||
| CMD ["/usr/local/bin/uwsgi"] | ||
|
|
||
| RUN addgroup -g 10001 app && adduser -D -u 10001 -G app -h /app app | ||
|
|
||
| # uWSGI configuration | ||
| ENV UWSGI_MODULE=landoapi.wsgi:app \ | ||
| UWSGI_SOCKET=:9000 \ | ||
| UWSGI_MASTER=1 \ | ||
| UWSGI_WORKERS=2 \ | ||
| UWSGI_THREADS=8 \ | ||
| # Disable worker memory sharing optimizations. They can cause memory leaks | ||
| # and issues with packages like Sentry. | ||
| # See https://discuss.newrelic.com/t/newrelic-agent-produces-system-error/43446 | ||
| UWSGI_LAZY_APPS=1 \ | ||
| UWSGI_WSGI_ENV_BEHAVIOR=holy \ | ||
| # Make uWSGI die instead of reload when it gets SIGTERM (fixed in uWSGI 2.1) | ||
| UWSGI_DIE_ON_TERM=1 \ | ||
| # Check that the options we gave uWSGI are sane | ||
| UWSGI_STRICT=1 \ | ||
| # Die if the application threw an exception on startup | ||
| UWSGI_NEED_APP=1 | ||
|
|
||
| RUN apk --update --no-cache add \ | ||
| sqlite | ||
|
|
@@ -15,13 +37,32 @@ RUN chown app:app /db | |
| COPY migrations /migrations | ||
|
|
||
| COPY requirements.txt /requirements.txt | ||
| RUN pip install --no-cache -r /requirements.txt | ||
|
|
||
| # Install pure-Python, compiled, and OS package dependencies. Use scanelf to | ||
| # uninstall any compile-time OS package dependencies and keep only the run-time | ||
| # OS package dependencies. | ||
| RUN set -ex \ | ||
| && apk add --no-cache --virtual .build-deps \ | ||
| gcc \ | ||
| libc-dev \ | ||
| musl-dev \ | ||
| linux-headers \ | ||
| pcre-dev \ | ||
| && pip install --no-cache -r /requirements.txt \ | ||
| && runDeps="$( \ | ||
| scanelf --needed --nobanner --recursive /usr/local \ | ||
| | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \ | ||
| | sort -u \ | ||
| | xargs -r apk info --installed \ | ||
| | sort -u \ | ||
| )" \ | ||
| && apk add --virtual .python-rundeps $runDeps \ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. *nit: Do you anticipate this list changing often? if not, why not just add these packages as part of the container build? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for originality!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The list of packages changes every time we add a new compiled dependency. So far that's been for database adapters, uWSGI, and crypto/SSL handling. This is the second time a reviewer has said they would be less surprised by an explicit list of runtime packages, and I'm sensing a trend, so I'll look into changing it. :) But we might not want to block on the rewrite and make that a follow-up commit. |
||
| && apk del .build-deps | ||
|
|
||
| COPY . /app | ||
| RUN pip install --no-cache /app | ||
|
|
||
| # run as non priviledged user | ||
| # Run as a non-privileged user | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of our invoke commands (specifically the ones that create the database) stopped working with the addition of these changes. I tracked this down to the is what causes these changes. We either need to re-add them, or we need to 1) confirm that the production environment doesn't need them and 2) confirm that the our circle.yml (which runs our invoke commands for production, doesn't use the production image as the context). I tested the app after re-adding those two lines and confirmed that it worked. I recommend that we re-add those two lines.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reproduced. I added the |
||
| USER app | ||
|
|
||
| # TODO allow ops to use this as a wsgi app | ||
| WORKDIR /app | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # This Source Code Form is subject to the terms of the Mozilla Public | ||
| # License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| # file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| """ | ||
| Construct an application instance that can be referenced by a WSGI server. | ||
| """ | ||
| import os | ||
|
|
||
| from .app import create_app | ||
|
|
||
| app = create_app(os.environ.get('VERSION_PATH', '/app/version.json')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to let @ckolos and the ops team configure these values? I'm not sure what the best values are for the hardware they will deploy this on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can set these values by editing the Dockerfile, setting them in the Docker environment, or by passing in a uWSGI config file. I'll let @ckolos decide which is best. We can iterate on the Dockerfile to find the approach that works best for them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Env vars are fine; we can override those