Skip to content

Commit 315f41d

Browse files
mars-fpurelogiq
authored andcommittedJun 28, 2017
Use uWSGI to serve the app in production
Build and configure the uWSGI application server for use in production environments. The default configuration is to serve connections over the uwsgi protocol to an nginx web head.
1 parent c6a333d commit 315f41d

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed
 

‎docker/Dockerfile-dev

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,26 @@ RUN apk --update --no-cache add \
88
sqlite
99
RUN mkdir /db
1010

11-
ADD requirements.txt /requirements.txt
12-
RUN pip install --no-cache -r /requirements.txt
13-
ADD dev-requirements.txt /dev-requirements.txt
14-
RUN pip install --no-cache -r /dev-requirements.txt
11+
COPY requirements.txt dev-requirements.txt /
12+
13+
RUN set -ex \
14+
&& apk add --no-cache --virtual .build-deps \
15+
gcc \
16+
libc-dev \
17+
musl-dev \
18+
linux-headers \
19+
pcre-dev \
20+
&& pip install --no-cache -r /requirements.txt \
21+
&& pip install --no-cache -r /dev-requirements.txt \
22+
&& runDeps="$( \
23+
scanelf --needed --nobanner --recursive /usr/local \
24+
| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
25+
| sort -u \
26+
| xargs -r apk info --installed \
27+
| sort -u \
28+
)" \
29+
&& apk add --virtual .python-rundeps $runDeps \
30+
&& apk del .build-deps
1531

1632
# Create a Dockerflow version file at the root so
1733
# we don't map over it below.

‎docker/Dockerfile-prod

+46-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,30 @@
44

55
FROM python:3.5-alpine
66

7-
RUN addgroup -g 1001 app && \
8-
adduser -D -u 1001 -G app -s /usr/sbin/nologin app
7+
MAINTAINER mars@mozilla.com
8+
# These are unlikely to change from version to version of the container
9+
EXPOSE 9000
10+
CMD ["/usr/local/bin/uwsgi"]
11+
12+
RUN addgroup -g 10001 app && adduser -D -u 10001 -G app -h /app app
13+
14+
# uWSGI configuration
15+
ENV UWSGI_MODULE=landoapi.wsgi:app \
16+
UWSGI_SOCKET=:9000 \
17+
UWSGI_MASTER=1 \
18+
UWSGI_WORKERS=2 \
19+
UWSGI_THREADS=8 \
20+
# Disable worker memory sharing optimizations. They can cause memory leaks
21+
# and issues with packages like Sentry.
22+
# See https://discuss.newrelic.com/t/newrelic-agent-produces-system-error/43446
23+
UWSGI_LAZY_APPS=1 \
24+
UWSGI_WSGI_ENV_BEHAVIOR=holy \
25+
# Make uWSGI die instead of reload when it gets SIGTERM (fixed in uWSGI 2.1)
26+
UWSGI_DIE_ON_TERM=1 \
27+
# Check that the options we gave uWSGI are sane
28+
UWSGI_STRICT=1 \
29+
# Die if the application threw an exception on startup
30+
UWSGI_NEED_APP=1
931

1032
RUN apk --update --no-cache add \
1133
sqlite
@@ -15,13 +37,32 @@ RUN chown app:app /db
1537
COPY migrations /migrations
1638

1739
COPY requirements.txt /requirements.txt
18-
RUN pip install --no-cache -r /requirements.txt
40+
41+
# Install pure-Python, compiled, and OS package dependencies. Use scanelf to
42+
# uninstall any compile-time OS package dependencies and keep only the run-time
43+
# OS package dependencies.
44+
RUN set -ex \
45+
&& apk add --no-cache --virtual .build-deps \
46+
gcc \
47+
libc-dev \
48+
musl-dev \
49+
linux-headers \
50+
pcre-dev \
51+
&& pip install --no-cache -r /requirements.txt \
52+
&& runDeps="$( \
53+
scanelf --needed --nobanner --recursive /usr/local \
54+
| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
55+
| sort -u \
56+
| xargs -r apk info --installed \
57+
| sort -u \
58+
)" \
59+
&& apk add --virtual .python-rundeps $runDeps \
60+
&& apk del .build-deps
1961

2062
COPY . /app
2163
RUN pip install --no-cache /app
2264

23-
# run as non priviledged user
65+
# Run as a non-privileged user
2466
USER app
2567

26-
# TODO allow ops to use this as a wsgi app
2768
WORKDIR /app

‎landoapi/wsgi.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
"""
5+
Construct an application instance that can be referenced by a WSGI server.
6+
"""
7+
import os
8+
9+
from .app import create_app
10+
11+
app = create_app(os.environ.get('VERSION_PATH', '/app/version.json'))

‎requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,5 @@ python-dateutil==2.6.0 \
7070
Flask-Alembic==2.0.1 \
7171
--hash=sha256:05a1e6f4148dbfcc9280a393373bfbd250af6f9f4f0ca9f744ef8f7376a3deec \
7272
--hash=sha256:7e67740b0b08d58dcae0c701d56b56e60f5fa4af907bb82b4cb0469229ba94ff
73+
uWSGI==2.0.15 \
74+
--hash=sha256:572ef9696b97595b4f44f6198fe8c06e6f4e6351d930d22e5330b071391272ff

‎setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
],
3232
keywords='mozilla commits development conduit',
3333
packages=find_packages(exclude=['docs', 'tests']),
34+
package_data={'landoapi': ['spec/swagger.yml']},
3435
install_requires=[],
3536
extras_require={},
3637
entry_points={

0 commit comments

Comments
 (0)
Failed to load comments.