Skip to content

Commit fa735ec

Browse files
authored
Add hot-reload option to docker entrypoint (#1880)
* Add hot-reload to entrypoint * Move gunicorn exec to bash function * Create WSGI_APP for gunicorn entrypoint Create WSGI_APP for gunicorn entrypoint with default value of `pygeoapi.flask_app:APP` * Respond to PR feedback
1 parent 511578a commit fa735ec

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

docker/entrypoint.sh

+31-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
# =================================================================
33
#
44
# Authors: Just van den Broecke <[email protected]>
5+
# Benjamin Webb <[email protected]>
56
#
67
# Copyright (c) 2019 Just van den Broecke
8+
# Copyright (c) 2024 Benjamin Webb
79
#
810
# Permission is hereby granted, free of charge, to any person
911
# obtaining a copy of this software and associated documentation
@@ -43,6 +45,7 @@ SCRIPT_NAME=${SCRIPT_NAME:=/}
4345
CONTAINER_NAME=${CONTAINER_NAME:=pygeoapi}
4446
CONTAINER_HOST=${CONTAINER_HOST:=0.0.0.0}
4547
CONTAINER_PORT=${CONTAINER_PORT:=80}
48+
WSGI_APP=${WSGI_APP:=pygeoapi.flask_app:APP}
4649
WSGI_WORKERS=${WSGI_WORKERS:=4}
4750
WSGI_WORKER_TIMEOUT=${WSGI_WORKER_TIMEOUT:=6000}
4851
WSGI_WORKER_CLASS=${WSGI_WORKER_CLASS:=gevent}
@@ -66,6 +69,20 @@ pygeoapi openapi generate ${PYGEOAPI_CONFIG} --output-file ${PYGEOAPI_OPENAPI}
6669

6770
echo "openapi.yml generated continue to pygeoapi"
6871

72+
start_gunicorn() {
73+
# SCRIPT_NAME should not have value '/'
74+
[[ "${SCRIPT_NAME}" = '/' ]] && export SCRIPT_NAME="" && echo "make SCRIPT_NAME empty from /"
75+
76+
echo "Starting gunicorn name=${CONTAINER_NAME} on ${CONTAINER_HOST}:${CONTAINER_PORT} with ${WSGI_WORKERS} workers and SCRIPT_NAME=${SCRIPT_NAME}"
77+
exec gunicorn --workers ${WSGI_WORKERS} \
78+
--worker-class=${WSGI_WORKER_CLASS} \
79+
--timeout ${WSGI_WORKER_TIMEOUT} \
80+
--name=${CONTAINER_NAME} \
81+
--bind ${CONTAINER_HOST}:${CONTAINER_PORT} \
82+
${@} \
83+
${WSGI_APP}
84+
}
85+
6986
case ${entry_cmd} in
7087
# Run Unit tests
7188
test)
@@ -91,19 +108,21 @@ case ${entry_cmd} in
91108

92109
# Run pygeoapi server
93110
run)
94-
# SCRIPT_NAME should not have value '/'
95-
[[ "${SCRIPT_NAME}" = '/' ]] && export SCRIPT_NAME="" && echo "make SCRIPT_NAME empty from /"
96-
97-
echo "Start gunicorn name=${CONTAINER_NAME} on ${CONTAINER_HOST}:${CONTAINER_PORT} with ${WSGI_WORKERS} workers and SCRIPT_NAME=${SCRIPT_NAME}"
98-
exec gunicorn --workers ${WSGI_WORKERS} \
99-
--worker-class=${WSGI_WORKER_CLASS} \
100-
--timeout ${WSGI_WORKER_TIMEOUT} \
101-
--name=${CONTAINER_NAME} \
102-
--bind ${CONTAINER_HOST}:${CONTAINER_PORT} \
103-
pygeoapi.flask_app:APP
104-
;;
111+
# Start
112+
start_gunicorn
113+
;;
114+
115+
# Run pygeoapi server with hot reload
116+
run-with-hot-reload)
117+
# Lock all Python files (for gunicorn hot reload)
118+
find . -type f -name "*.py" | xargs chmod 0444
119+
120+
# Start with hot reload options
121+
start_gunicorn --reload --reload-extra-file ${PYGEOAPI_CONFIG}
122+
;;
123+
105124
*)
106-
error "unknown command arg: must be run (default) or test"
125+
error "unknown command arg: must be run (default), run-with-hot-reload, or test"
107126
;;
108127
esac
109128

docs/source/running-with-docker.rst

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ To run with the default built-in configuration and data:
2929
3030
...then browse to http://localhost:5000
3131

32+
You can also run pygeoapi with run-with-hot-reload of the configuration enabled
33+
34+
.. code-block:: bash
35+
36+
docker run -p 5000:80 -it geopython/pygeoapi run-with-hot-reload
37+
3238
You can also run all unit tests to verify:
3339

3440
.. code-block:: bash
@@ -91,8 +97,10 @@ The base Docker image supports two additional environment variables for configur
9197
.. code-block:: bash
9298
9399
docker run -p 5000:80 -e PYGEOAPI_SERVER_ADMIN=true -it geopython/pygeoapi
100+
# run with hot reload
101+
docker run -p 5000:80 -e PYGEOAPI_SERVER_ADMIN=true -it geopython/pygeoapi run-with-hot-reload
94102
95-
This does not enable hot reloading of the `pygeoapi` configuration. To learn more about the Admin API see :ref:`admin-api`.
103+
To learn more about the Admin API see :ref:`admin-api`.
96104

97105

98106
Deploying on a sub-path

0 commit comments

Comments
 (0)