Skip to content

Commit

Permalink
✨ Add Python 2.7, image, tests (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiangolo authored May 15, 2019
1 parent 52bfac6 commit 61865df
Show file tree
Hide file tree
Showing 25 changed files with 194 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docker-compose.build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ services:
latest:
build: ./python3.7
image: tiangolo/meinheld-gunicorn:latest
python2.7:
build: ./python2.7
image: tiangolo/meinheld-gunicorn:python2.7
python3.6:
build: ./python3.6
image: tiangolo/meinheld-gunicorn:python3.6
Expand Down
26 changes: 26 additions & 0 deletions python2.7/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM python:2.7

LABEL maintainer="Sebastian Ramirez <tiangolo@gmail.com>"

RUN pip install meinheld gunicorn

COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY ./start.sh /start.sh
RUN chmod +x /start.sh

COPY ./gunicorn_conf.py /gunicorn_conf.py

COPY ./app /app
WORKDIR /app/

ENV PYTHONPATH=/app

EXPOSE 80

ENTRYPOINT ["/entrypoint.sh"]

# Run the start script, it will check for an /app/prestart.sh script (e.g. for migrations)
# And then will start Gunicorn with Meinheld
CMD ["/start.sh"]
10 changes: 10 additions & 0 deletions python2.7/app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import sys


def app(env, start_response):
version = "{}.{}".format(sys.version_info.major, sys.version_info.minor)
start_response("200 OK", [("Content-Type", "text/plain")])
message = "Hello World from a default Python {} app in a Docker container, with Meinheld and Gunicorn (default)".format(
version
)
return [message.encode("utf-8")]
12 changes: 12 additions & 0 deletions python2.7/app/prestart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /usr/bin/env sh

echo "Running inside /app/prestart.sh, you could add migrations to this file, e.g.:"

echo "
#! /usr/bin/env bash
# Let the DB start
sleep 10;
# Run migrations
alembic upgrade head
"
22 changes: 22 additions & 0 deletions python2.7/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env sh
set -e

if [ -f /app/app/main.py ]; then
DEFAULT_MODULE_NAME=app.main
elif [ -f /app/main.py ]; then
DEFAULT_MODULE_NAME=main
fi
MODULE_NAME=${MODULE_NAME:-$DEFAULT_MODULE_NAME}
VARIABLE_NAME=${VARIABLE_NAME:-app}
export APP_MODULE=${APP_MODULE:-"$MODULE_NAME:$VARIABLE_NAME"}

if [ -f /app/gunicorn_conf.py ]; then
DEFAULT_GUNICORN_CONF=/app/gunicorn_conf.py
elif [ -f /app/app/gunicorn_conf.py ]; then
DEFAULT_GUNICORN_CONF=/app/app/gunicorn_conf.py
else
DEFAULT_GUNICORN_CONF=/gunicorn_conf.py
fi
export GUNICORN_CONF=${GUNICORN_CONF:-$DEFAULT_GUNICORN_CONF}

exec "$@"
43 changes: 43 additions & 0 deletions python2.7/gunicorn_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import print_function
import json
import multiprocessing
import os

workers_per_core_str = os.getenv("WORKERS_PER_CORE", "2")
web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)
host = os.getenv("HOST", "0.0.0.0")
port = os.getenv("PORT", "80")
bind_env = os.getenv("BIND", None)
use_loglevel = os.getenv("LOG_LEVEL", "info")
if bind_env:
use_bind = bind_env
else:
use_bind = "{host}:{port}".format(host=host, port=port)

cores = multiprocessing.cpu_count()
workers_per_core = float(workers_per_core_str)
default_web_concurrency = workers_per_core * cores
if web_concurrency_str:
web_concurrency = int(web_concurrency_str)
assert web_concurrency > 0
else:
web_concurrency = int(default_web_concurrency)

# Gunicorn config variables
loglevel = use_loglevel
workers = web_concurrency
bind = use_bind
keepalive = 120
errorlog = "-"

# For debugging and testing
log_data = {
"loglevel": loglevel,
"workers": workers,
"bind": bind,
# Additional, non-gunicorn variables
"workers_per_core": workers_per_core,
"host": host,
"port": port,
}
print(json.dumps(log_data))
15 changes: 15 additions & 0 deletions python2.7/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /usr/bin/env sh
set -e

# If there's a prestart.sh script in the /app directory, run it before starting
PRE_START_PATH=/app/prestart.sh
echo "Checking for script in $PRE_START_PATH"
if [ -f $PRE_START_PATH ] ; then
echo "Running script $PRE_START_PATH"
. "$PRE_START_PATH"
else
echo "There is no script $PRE_START_PATH"
fi

# Start Gunicorn
exec gunicorn -k egg:meinheld#gunicorn_worker -c "$GUNICORN_CONF" "$APP_MODULE"
4 changes: 4 additions & 0 deletions tests/test_01_main/test_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"image,response_text",
[
(
"tiangolo/meinheld-gunicorn:python2.7",
"Hello World from a default Python 2.7 app in a Docker container, with Meinheld and Gunicorn (default)",
),
(
"tiangolo/meinheld-gunicorn:python3.6",
"Hello World from a default Python 3.6 app in a Docker container, with Meinheld and Gunicorn (default)",
Expand Down
4 changes: 4 additions & 0 deletions tests/test_01_main/test_env_vars_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"image,response_text",
[
(
"tiangolo/meinheld-gunicorn:python2.7",
"Hello World from a default Python 2.7 app in a Docker container, with Meinheld and Gunicorn (default)",
),
(
"tiangolo/meinheld-gunicorn:python3.6",
"Hello World from a default Python 3.6 app in a Docker container, with Meinheld and Gunicorn (default)",
Expand Down
1 change: 1 addition & 0 deletions tests/test_01_main/test_env_vars_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def verify_container(container):
@pytest.mark.parametrize(
"image",
[
("tiangolo/meinheld-gunicorn:python2.7"),
("tiangolo/meinheld-gunicorn:python3.6"),
("tiangolo/meinheld-gunicorn:python3.7"),
("tiangolo/meinheld-gunicorn:latest"),
Expand Down
4 changes: 4 additions & 0 deletions tests/test_01_main/test_env_vars_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"image,response_text",
[
(
"tiangolo/meinheld-gunicorn:python2.7",
"Hello World from a default Python 2.7 app in a Docker container, with Meinheld and Gunicorn (default)",
),
(
"tiangolo/meinheld-gunicorn:python3.6",
"Hello World from a default Python 3.6 app in a Docker container, with Meinheld and Gunicorn (default)",
Expand Down
3 changes: 3 additions & 0 deletions tests/test_02_app/custom_app/python2.7.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM tiangolo/meinheld-gunicorn:python2.7

COPY ./app /app
3 changes: 3 additions & 0 deletions tests/test_02_app/package_app/python2.7.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM tiangolo/meinheld-gunicorn:python2.7

COPY ./app /app
3 changes: 3 additions & 0 deletions tests/test_02_app/package_app_config/python2.7.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM tiangolo/meinheld-gunicorn:python2.7

COPY ./app /app
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM tiangolo/meinheld-gunicorn:python2.7

COPY ./app /app
3 changes: 3 additions & 0 deletions tests/test_02_app/package_app_sub_config/python2.7.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM tiangolo/meinheld-gunicorn:python2.7

COPY ./app /app
3 changes: 3 additions & 0 deletions tests/test_02_app/simple_app/python2.7.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM tiangolo/meinheld-gunicorn:python2.7

COPY ./app /app
3 changes: 3 additions & 0 deletions tests/test_02_app/simple_app_prestart/python2.7.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM tiangolo/meinheld-gunicorn:python2.7

COPY ./app /app
5 changes: 5 additions & 0 deletions tests/test_02_app/test_custom_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"dockerfile,environment,response_text",
[
(
"python2.7.dockerfile",
{"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"},
"Test app. From Meinheld with Gunicorn. Using Python 2.7",
),
(
"python3.6.dockerfile",
{"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"},
Expand Down
4 changes: 4 additions & 0 deletions tests/test_02_app/test_package_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"dockerfile,response_text",
[
(
"python2.7.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 2.7",
),
(
"python3.6.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 3.6",
Expand Down
4 changes: 4 additions & 0 deletions tests/test_02_app/test_package_app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"dockerfile,response_text",
[
(
"python2.7.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 2.7",
),
(
"python3.6.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 3.6",
Expand Down
4 changes: 4 additions & 0 deletions tests/test_02_app/test_package_app_custom_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"dockerfile,response_text",
[
(
"python2.7.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 2.7",
),
(
"python3.6.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 3.6",
Expand Down
4 changes: 4 additions & 0 deletions tests/test_02_app/test_package_app_sub_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"dockerfile,response_text",
[
(
"python2.7.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 2.7",
),
(
"python3.6.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 3.6",
Expand Down
4 changes: 4 additions & 0 deletions tests/test_02_app/test_simple_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"dockerfile,response_text",
[
(
"python2.7.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 2.7",
),
(
"python3.6.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 3.6",
Expand Down
4 changes: 4 additions & 0 deletions tests/test_02_app/test_simple_app_prestart.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def verify_container(container, response_text):
@pytest.mark.parametrize(
"dockerfile,response_text",
[
(
"python2.7.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 2.7",
),
(
"python3.6.dockerfile",
"Test app. From Meinheld with Gunicorn. Using Python 3.6",
Expand Down

0 comments on commit 61865df

Please sign in to comment.