Skip to content

Latest commit

 

History

History

python_flask

Python Flask with Docker

This directory provides a Dockerfile to containerize your Python Flask application for both development and production use. This example uses Flask as the web framework, but you can modify it to fit your needs.

Files Included

  • This file uses python:alpine as the base image and installs Flask and dependencies manually.
  • This version uses python:slim as the base image to reduce the size of the image while still supporting Flask.

Here’s a comparison of the image sizes. The .slim images are built using Docker Slim.

alt text

Setup Instructions

1. Add a .dockerignore File

Copy the .dockerignore file to the root of your project.

2. Dockerfile

Create a Dockerfile in the root of your project. This file contains the necessary commands to build the Docker image.

3. Build the Docker Image

Using the Generic Dockerfile

docker build -t python_flask .

Using the Dockerfile_python_slim

docker build -f Dockerfile_python_slim -t python_flask .

4. Using Docker Slim (Optional)

We can use Docker Slim to reduce the size of the image further.

Note: Docker Slim can remove some files that are required for your application to run. It is recommended to test the application thoroughly after using Docker Slim to ensure everything works as expected.

docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock dslim/slim build --target python_flask

5. Run the Docker Container

Once the image is built, run the container with:

docker run -p 5000:5000 python_flask

Contributing

If you have suggestions or enhancements for this Docker setup, feel free to open a pull request.


References