Author avatar

Kimaru Thagana

Dockerfile for Python Web Projects

Kimaru Thagana

  • Sep 18, 2020
  • 4 Min read
  • Sep 18, 2020
  • 4 Min read
IT Infrastructure
Container Management


The power of Python is now available to web based projects via frameworks such as Flask and Streamlit. These projects allows the developer to bring the data wrangling and analytics capabilities of Python to the web. When it comes to the deployment and sharing of these projects, development and testing can be hindered by problems with setting up dependencies, requirements, network ports, and execution environments. To solve this, Docker was introduced. Docker is an open source product that offers hardware virtualization at the Operating System (OS) level. This guide will demonstrate how to use Docker to package and ship a Python-based Streamlit web app.

The guide assumes you have at least intermediate knowledge in Python and have a base level of understanding of Streamlit and Docker.

Sample App

Consider the scenario where you would like to build a growth calculator that determines the worth of an investment given the growth rate, initial investment, and time in years.

This app would then be shared as part of your developer portfolio. It is paramount that interested parties will be able to download and run your project with ease. The web being the most common interface, you decide the project will be web based. Since it involves data analysis, it will be in Python. To give your recipients an easy time in setting up and running your app, you decide to package it in docker.

Copy the code below into a Python file and name it

1import streamlit as st
3st.title("Hello Streamlit")
4st.header("Calculate % Growth")
5initial = st.number_input("Initial investment in USD")
6yr = st.number_input("Growth Period in years")
7growth = st.number_input("Growth Rate in %")
8terminal_value = 0
9current_val = initial
10for year in range(int(yr)):
11   current_val += growth * current_val
12   terminal_value = current_val
14# perform cashflow projections for the next 5 years
15st.write(f'Terminal value of {initial} after {yr} years at a growth rate of {growth} is {terminal_value}')

This project requires Python libraries to be installed for it to run. The libraries will be recorded in a requirements.txt file. Copy the contents below into your requirements.txt file.


For ease of handling during the Docker building, place both the Python and requirements files in a folder and name it src/.

Docker Setup

The dockerfile required for this project mainly has to achieve the following logical steps:

  1. Create base image
  2. Copy source code
  3. Install requirements and dependencies
  4. Expose required port
  5. Run the Streamlit app within the Docker environment

Copy the Docker commands below in a file and name it Dockerfile

1FROM python:3.8
3ENV MICRO_SERVICE=/home/app/webapp
4# set work directory
6# where your code lives
9# set environment variables
13# install dependencies
14RUN pip install --upgrade pip
15# copy project
17RUN pip install -r requirements.txt
18EXPOSE 8501
19CMD streamlit run

Running the Project

With both files set up, you are ready to build and run your image. To build your image, run the command

1docker build -t myimage .

After the docker image is built, it is time to run your Docker image.

1docker run -p 8501:8501 myimage

The app is now running at the localhost address provided by the Docker interface. Stremlit pageStremlit page


Packaging and shipping a Python web app using Docker are vital skills for developers holding Python developer and Devops related positions. To further build on these skills, do further research on how to run multi-container apps on Docker using docker-compose.