Deployment of Streamlit App on Docker Container

Deployment of Streamlit App on Docker Container

What is Streamlit?

Streamlit is a free and open-source framework to rapidly build and share beautiful machine learning and data science web apps. It is a Python-based library specifically designed for machine learning engineers. Data scientists or machine learning engineers are not web developers and they're not interested in spending weeks learning to use these frameworks to build web apps. Instead, they want a tool that is easier to learn and to use, as long as it can display data and collect needed parameters for modeling. Streamlit allows you to create a stunning-looking application with only a few lines of code.

No alt text provided for this image

Why should data scientists use Streamlit?

The best thing about Streamlit is that you don't even need to know the basics of web development to get started or to create your first web application. So if you're somebody who's into data science and you want to deploy your models easily, quickly, and with only a few lines of code, Streamlit is a good fit.

One of the important aspects of making an application successful is to deliver it with an effective and intuitive user interface. Many of the modern data-heavy apps face the challenge of building an effective user interface quickly, without taking complicated steps. Streamlit is a promising open-source Python library, which enables developers to build attractive user interfaces in no time.

Streamlit is the easiest way especially for people with no front-end knowledge to put their code into a web application:

  • No front-end (html, js, css) experience or knowledge is required.
  • You don't need to spend days or months to create a web app, you can create a really beautiful machine learning or data science app in only a few hours or even minutes.
  • It is compatible with the majority of Python libraries (e.g. pandas, matplotlib, seaborn, plotly, Keras, PyTorch, SymPy(latex)).
  • Less code is needed to create amazing web apps.
  • Data caching simplifies and speeds up computation pipelines.

What is Docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

What can I use Docker for?

Fast, consistent delivery of your applications

Docker streamlines the development lifecycle by allowing developers to work in standardized environments using local containers which provide your applications and services. Containers are great for continuous integration and continuous delivery (CI/CD) workflows.

Consider the following example scenario:

  • Your developers write code locally and share their work with their colleagues using Docker containers.
  • They use Docker to push their applications into a test environment and execute automated and manual tests.
  • When developers find bugs, they can fix them in the development environment and redeploy them to the test environment for testing and validation.
  • When testing is complete, getting the fix to the customer is as simple as pushing the updated image to the production environment.

Responsive deployment and scaling

Docker’s container-based platform allows for highly portable workloads. Docker containers can run on a developer’s local laptop, on physical or virtual machines in a data center, on cloud providers, or in a mixture of environments.

Docker’s portability and lightweight nature also make it easy to dynamically manage workloads, scaling up or tearing down applications and services as business needs dictate, in near real time.

Running more workloads on the same hardware

Docker is lightweight and fast. It provides a viable, cost-effective alternative to hypervisor-based virtual machines, so you can use more of your compute capacity to achieve your business goals. Docker is perfect for high density environments and for small and medium deployments where you need to do more with fewer resources.

How to install streamlit:

Make sure latest Python version and pip package installer are pre-installed in the environment. Use the below command to install Streamlit:

pip install streamlit        

To test the installation is working:

streamlit hello        

Streamlit's Hello app should appear in a new tab in your web browser!

Dockerfile for Streamlit app

FROM python:3.7

LABEL maintainer="vijaytanu766@gmail.com"

RUN apt update && apt-get update && apt full-upgrade -y && apt-get autoremove -y

RUN pip3 install pillow && pip3 install streamlit && pip3 install requests

WORKDIR /

EXPOSE 8501

COPY frontend.py /

COPY Video /

ENTRYPOINT ["streamlit", "run"]

CMD ["frontend.py"]        

To create an Docker image from the above dockerfile use the below command:

docker build -t <image_name:tag> <Dockerfile location>

docker build -t streamlit_app:latest .        
No alt text provided for this image
No alt text provided for this image

To run the docker image use the below command:

docker run -dit --name <container_name> <image_name:tag>

docker run -dit --name streamlit_container streamlit_app        
No alt text provided for this image

The Final Output:

No alt text provided for this image
No alt text provided for this image


GitHub link for the code reference :

Thank You for Reading my Article.......

To view or add a comment, sign in

More articles by Tanu Vijay

Others also viewed

Explore content categories