|
| 1 | +Python 3.5 Docker image |
| 2 | +=================== |
| 3 | + |
| 4 | +This container image includes Python 3.5 as a [S2I](https://github.com/openshift/source-to-image) base image for your Python 3.5 applications. |
| 5 | +Users can choose between RHEL and CentOS based builder images. |
| 6 | +The RHEL image is available in the [Red Hat Container Catalog](https://access.redhat.com/containers/#/registry.access.redhat.com/rhscl/python-35-rhel7) |
| 7 | +as registry.access.redhat.com/rhscl/python-35-rhel7. |
| 8 | +The CentOS image is then available on [Docker Hub](https://hub.docker.com/r/centos/python-35-centos7/) |
| 9 | +as centos/python-35-centos7. |
| 10 | +The resulting image can be run using [Docker](http://docker.io). |
| 11 | + |
| 12 | +Description |
| 13 | +----------- |
| 14 | + |
| 15 | +Python 3.5 available as docker container is a base platform for |
| 16 | +building and running various Python 3.5 applications and frameworks. |
| 17 | +Python is an easy to learn, powerful programming language. It has efficient high-level |
| 18 | +data structures and a simple but effective approach to object-oriented programming. |
| 19 | +Python's elegant syntax and dynamic typing, together with its interpreted nature, |
| 20 | +make it an ideal language for scripting and rapid application development in many areas |
| 21 | +on most platforms. |
| 22 | + |
| 23 | +Usage |
| 24 | +--------------------- |
| 25 | +To build a simple [python-sample-app](https://github.com/sclorg/s2i-python-container/tree/master/3.5/test/setup-test-app) application |
| 26 | +using standalone [S2I](https://github.com/openshift/source-to-image) and then run the |
| 27 | +resulting image with [Docker](http://docker.io) execute: |
| 28 | + |
| 29 | +* **For RHEL based image** |
| 30 | + ``` |
| 31 | + $ s2i build https://github.com/sclorg/s2i-python-container.git --context-dir=3.5/test/setup-test-app/ rhscl/python-35-rhel7 python-sample-app |
| 32 | + $ docker run -p 8080:8080 python-sample-app |
| 33 | + ``` |
| 34 | +
|
| 35 | +* **For CentOS based image** |
| 36 | + ``` |
| 37 | + $ s2i build https://github.com/sclorg/s2i-python-container.git --context-dir=3.5/test/setup-test-app/ centos/python-35-centos7 python-sample-app |
| 38 | + $ docker run -p 8080:8080 python-sample-app |
| 39 | + ``` |
| 40 | +
|
| 41 | +**Accessing the application:** |
| 42 | +``` |
| 43 | +$ curl 127.0.0.1:8080 |
| 44 | +``` |
| 45 | +
|
| 46 | +Environment variables |
| 47 | +--------------------- |
| 48 | +
|
| 49 | +To set these environment variables, you can place them as a key value pair into a `.s2i/environment` |
| 50 | +file inside your source code repository. |
| 51 | +
|
| 52 | +* **APP_SCRIPT** |
| 53 | +
|
| 54 | + Used to run the application from a script file. |
| 55 | + This should be a path to a script file (defaults to `app.sh` unless set to null) that will be |
| 56 | + run to start the application. |
| 57 | +
|
| 58 | +* **APP_FILE** |
| 59 | +
|
| 60 | + Used to run the application from a Python script. |
| 61 | + This should be a path to a Python file (defaults to `app.py` unless set to null) that will be |
| 62 | + passed to the Python interpreter to start the application. |
| 63 | +
|
| 64 | +* **APP_MODULE** |
| 65 | +
|
| 66 | + Used to run the application with Gunicorn, as documented |
| 67 | + [here](http://docs.gunicorn.org/en/latest/run.html#gunicorn). |
| 68 | + This variable specifies a WSGI callable with the pattern |
| 69 | + `MODULE_NAME:VARIABLE_NAME`, where `MODULE_NAME` is the full dotted path |
| 70 | + of a module, and `VARIABLE_NAME` refers to a WSGI callable inside the |
| 71 | + specified module. |
| 72 | + Gunicorn will look for a WSGI callable named `application` if not specified. |
| 73 | +
|
| 74 | + If `APP_MODULE` is not provided, the `run` script will look for a `wsgi.py` |
| 75 | + file in your project and use it if it exists. |
| 76 | +
|
| 77 | + If using `setup.py` for installing the application, the `MODULE_NAME` part |
| 78 | + can be read from there. For an example, see |
| 79 | + [setup-test-app](https://github.com/sclorg/s2i-python-container/tree/master/3.5/test/setup-test-app). |
| 80 | +
|
| 81 | +* **APP_HOME** |
| 82 | +
|
| 83 | + This variable can be used to specify a sub-directory in which the application to be run is contained. |
| 84 | + The directory pointed to by this variable needs to contain `wsgi.py` (for Gunicorn) or `manage.py` (for Django). |
| 85 | +
|
| 86 | + If `APP_HOME` is not provided, the `assemble` and `run` scripts will use the application's root |
| 87 | + directory. |
| 88 | +
|
| 89 | +* **APP_CONFIG** |
| 90 | +
|
| 91 | + Path to a valid Python file with a |
| 92 | + [Gunicorn configuration](http://docs.gunicorn.org/en/latest/configure.html#configuration-file) file. |
| 93 | +
|
| 94 | +* **DISABLE_COLLECTSTATIC** |
| 95 | +
|
| 96 | + Set this variable to a non-empty value to inhibit the execution of |
| 97 | + 'manage.py collectstatic' during the build. This only affects Django projects. |
| 98 | +
|
| 99 | +* **DISABLE_MIGRATE** |
| 100 | +
|
| 101 | + Set this variable to a non-empty value to inhibit the execution of 'manage.py migrate' |
| 102 | + when the produced image is run. This only affects Django projects. |
| 103 | +
|
| 104 | +* **PIP_INDEX_URL** |
| 105 | +
|
| 106 | + Set this variable to use a custom index URL or mirror to download required packages |
| 107 | + during build process. This only affects packages listed in requirements.txt. |
| 108 | +
|
| 109 | +* **UPGRADE_PIP_TO_LATEST** |
| 110 | +
|
| 111 | + Set this variable to a non-empty value to have the 'pip' program and related |
| 112 | + python packages (setuptools and wheel) be upgraded to the most recent version |
| 113 | + before any Python packages are installed. If not set it will use whatever |
| 114 | + the default version is included by the platform for the Python version being used. |
| 115 | +
|
| 116 | +* **WEB_CONCURRENCY** |
| 117 | +
|
| 118 | + Set this to change the default setting for the number of |
| 119 | + [workers](http://docs.gunicorn.org/en/stable/settings.html#workers). By |
| 120 | + default, this is set to the number of available cores times 2. |
| 121 | +
|
| 122 | +Source repository layout |
| 123 | +------------------------ |
| 124 | +
|
| 125 | +You do not need to change anything in your existing Python project's repository. |
| 126 | +However, if these files exist they will affect the behavior of the build process: |
| 127 | +
|
| 128 | +* **requirements.txt** |
| 129 | +
|
| 130 | + List of dependencies to be installed with `pip`. The format is documented |
| 131 | + [here](https://pip.pypa.io/en/latest/user_guide.html#requirements-files). |
| 132 | +
|
| 133 | +
|
| 134 | +* **setup.py** |
| 135 | +
|
| 136 | + Configures various aspects of the project, including installation of |
| 137 | + dependencies, as documented |
| 138 | + [here](https://packaging.python.org/en/latest/distributing.html#setup-py). |
| 139 | + For most projects, it is sufficient to simply use `requirements.txt`, if this |
| 140 | + file is present `setup.py` is not processed by default, please use `-e .` to |
| 141 | + trigger its processing from the requirements.txt file. |
| 142 | +
|
| 143 | +
|
| 144 | +Run strategies |
| 145 | +-------------- |
| 146 | +
|
| 147 | +The Docker image produced by s2i-python executes your project in one of the |
| 148 | +following ways, in precedence order: |
| 149 | +
|
| 150 | +* **Gunicorn** |
| 151 | +
|
| 152 | + The Gunicorn WSGI HTTP server is used to serve your application in the case that it |
| 153 | + is installed. It can be installed by listing it either in the `requirements.txt` |
| 154 | + file or in the `install_requires` section of the `setup.py` file. |
| 155 | +
|
| 156 | + If a file named `wsgi.py` is present in your repository, it will be used as |
| 157 | + the entry point to your application. This can be overridden with the |
| 158 | + environment variable `APP_MODULE`. |
| 159 | + This file is present in Django projects by default. |
| 160 | +
|
| 161 | + If you have both Django and Gunicorn in your requirements, your Django project |
| 162 | + will automatically be served using Gunicorn. |
| 163 | +
|
| 164 | +* **Django development server** |
| 165 | +
|
| 166 | + If you have Django in your requirements but don't have Gunicorn, then your |
| 167 | + application will be served using Django's development web server. However, this is not |
| 168 | + recommended for production environments. |
| 169 | +
|
| 170 | +* **Python script** |
| 171 | +
|
| 172 | + This would be used where you provide a Python code file for running you |
| 173 | + application. It will be used in the case where you specify a path to a |
| 174 | + Python script via the `APP_FILE` environment variable, defaulting to a |
| 175 | + file named `app.py` if it exists. The script is passed to a regular |
| 176 | + Python interpreter to launch your application. |
| 177 | +
|
| 178 | +* **Application script file** |
| 179 | +
|
| 180 | + This is the most general way of executing your application. It will be |
| 181 | + used in the case where you specify a path to an executable script file |
| 182 | + via the `APP_SCRIPT` environment variable, defaulting to a file named |
| 183 | + `app.sh` if it exists. The script is executed directly to launch your |
| 184 | + application. |
| 185 | +
|
| 186 | +Hot deploy |
| 187 | +--------------------- |
| 188 | +
|
| 189 | +If you are using Django, hot deploy will work out of the box. |
| 190 | +
|
| 191 | +To enable hot deploy while using Gunicorn, make sure you have a Gunicorn |
| 192 | +configuration file inside your repository with the |
| 193 | +[`reload`](https://gunicorn-docs.readthedocs.org/en/latest/settings.html#reload) |
| 194 | +option set to `true`. Make sure to specify your config via the `APP_CONFIG` |
| 195 | +environment variable. |
| 196 | +
|
| 197 | +To change your source code in running container, use Docker's |
| 198 | +[exec](https://docs.docker.com/reference/commandline/exec/) command: |
| 199 | +
|
| 200 | +``` |
| 201 | +docker exec -it <CONTAINER_ID> /bin/bash |
| 202 | +``` |
| 203 | +
|
| 204 | +After you enter into the running container, your current directory is set |
| 205 | +to `/opt/app-root/src`, where the source code is located. |
| 206 | +
|
| 207 | +
|
| 208 | +See also |
| 209 | +-------- |
| 210 | +Dockerfile and other sources are available on https://github.com/sclorg/s2i-python-container. |
| 211 | +In that repository you also can find another versions of Python environment Dockerfiles. |
| 212 | +Dockerfile for CentOS is called Dockerfile, Dockerfile for RHEL is called Dockerfile.rhel7. |
0 commit comments