This is a hello world app that is written in Python, using Django framework, packaged using Docker.
$ git clone https://github.com/app-generator/sample-dockerize-django.git
$ docker-compose up --build
At this point, the app runs at localhost:8000
Make sure to have Python installed and its package manager, pip
.
python --version
pip --version
If the above command yields an error, install Python.
sudo apt install python3 python3-pip
sudo yum install python3 python3-pip
Refer to your Linux distribution's documentation for how to install Python.
python3 -m venv venv
source venv/bin/activate
pip install django
Then you can import Django in your code.
import django
django-admin startproject project-name
project-name/ # The project's root directory
├───hello_world/ # The project's app directory
│ ├───__init__.py # Module for the app
│ ├───asgi.py # ASGI entrypoint
│ ├───settings.py # Django settings
│ ├───urls.py # URL patterns
│ └───wsgi.py # WSGI entrypoint
├───manage.py # Django's management script
└───requirements.txt # Requirements for the project
This is packaged by using the Python container image as a base, copying the source code and installs necessary dependencies of this app and adds a startup script.
There are 2 modes for running this project, "development" and "production", It's up to the developers to define how their apps would behave under development environment and production environment. For example, development mode may use SQLite as a database whereas in production, the app will use PostgreSQL.
In production, Gunicorn is used as the server, Nginx is used for serving static files and bytecodes will be generated from the source code for faster startup whereas in development, a development server from Flask is used.
docker-compose up --build
Docker images can be customized using environment variables or customized during build time using build arguments.
Key | Description |
---|---|
POSTGRES_HOST |
Hostname of the database server. |
POSTGRES_USERNAME |
Username that will be used to log in into the database server. |
POSTGRES_PASSWORD |
Password that will be used to log in into the database server. |
POSTGRES_PORT |
Port number that is used to connect to database server. |
POSTGRES_DBNAME |
Database that will be used for storing data. |
Key | Description |
---|---|
PYTHON_VERSION |
The version of Python that will be used for building this image, note that Python 2 is not supported. |