Template for creating a Dockerized application using Python, Flask, Docker, and Docker Compose.
Ensure you have Docker, Docker Compose, and Python installed.
This template allows you to create and deploy a Python application using Flask and Docker. Docker Compose is used to build the Docker image, inject environment variables, and act as a starting point to add additional services to your application.
The template contains the following files and directories:
src/
: Directory for source code - will be copied into Docker imagesrc/app.py
: Sample Flask applicationsrc/hello_world.py
: Sample Python script used in Flask app
default.env
: Default environment file for secret management - should be renamed to.env
and never checked into source controldocker-compose.yml
: Default Docker Compose file that injects secrets from.env
file and builds Docker image specified byDockerfile
Dockerfile
: Default Dockerfile that copies source code, installs Python dependencies, and runs Flask serverMakefile
: Helper to easily start, stop, and restart apprequirements.txt
: Defines list of Python requirements that are used by the application and will be installed into the Docker image
The sample application is purely a skeleton to build off of. There are two routes defined in the Flask app:
Endpoint | Description |
---|---|
/ |
Test connection - will return Connected if successful. |
/hello/<name> |
Invokes a function in the Python script using the provided parameter - will return Hello <name>! if successful. |
- Clone the repository.
- Copy the
default.env
file to a new file called.env
and populate with your desired port and any secrets- This should never be commited into source control - this template is setup to ensure the
.env
file is excluded from Git
- This should never be commited into source control - this template is setup to ensure the
- Build your Python application in the
src
directory. - Deploy the app by running
make up
. - Navigate to
http://<IP_ADDRESS>/<FLASK_PORT>
(e.g. http://localhost:5050) in your browser - you should seeConnected
if successful.IP_ADDRESS
is the IP address of the machine running Docker.FLASK_PORT
is the port defined in your.env
file (default is 5050)
- Update app with any changes by re-running
make up
- Restart app by running
make restart
- Stop app by running
make down
This template is made available under a modified MIT license. See the LICENSE file.