Python Music Recommender: A Practical Guide Part-5

Python Music Recommender: A Practical Guide Part-5

Containerizing the Streamlit app using Docker

This article aims to outline the basics of building an image that runs a Python application in a container. In our case, it's a Streamlit app that we created in the previous blog.

Before starting open the Command Line app on your computer and log in to docker using docker login command


Creating Dockerfile

In the working root directory, let’s create a file named ‘Dockerfile’ without any extensions.

# Base image
FROM python:3.11

# Set working directory as app
WORKDIR /app

# Copy file from local (source) to file structure of container (destination)
COPY requirements.txt ./requirements.txt

# Install the requirements specified in file using RUN
RUN pip install -r requirements.txt

# Exposing the port that will be used to run the app
EXPOSE 8501

# Copy all items in current local directory (source) to current container directory (destination)
COPY . .

# Entrypoint to make the image executable
ENTRYPOINT [ "streamlit", "run" ]

# Command to run when image is executed inside a container
CMD ["app.py"]

Here is a breakdown of each command in the Dockerfile above:

  • The FROM command tells Docker to create a base layer. In this case, we have used the Python v3.11 image available in Docker Hub as the base image on which our application will be built.

  • WORKDIR sets the Working Directory within the Docker image. In this case, it is creating an ‘app’ folder where all files will be stored.

  • COPY command duplicates the local requirements.txt and other files (which contain package names and versions required by the model app) into the Docker image’s ‘app’ folder.

  • The RUN command specifies what Command Line Interface (CLI) commands to run within the Docker container. This Dockerfile installs all packages and dependencies in the requirements.txt file and runs some additional commands to download the spaCy ‘en_core_web_sm’ model correctly from the web.

  • EXPOSE specifies the port number on the network where our application will be deployed. The Streamlit library is particularly compatible with Port 8501 as set above.

  • Lastly, CMD is used to specify the commands to be run when the Docker container is started.


Building Dockerfile

Open the terminal and navigate to the project.

  • Build an image out of the Dockerfile by running the following command below. Pass in the -t parameter to name the images.

    docker build -f Dockerfile -t music_recommendation_app:latest .

Note: The dot at the end of the command must be present. It indicates that Docker should use files from the current directory to build the image.

  • Use the following command to view all your images.

    docker image ls


Creating Container

To ensure the image has been built correctly, we can run the application from the Docker container by entering the following command into the CLI:

docker run -p 8501:8501 music_recommendation_app:latest

The -p flag is used to map the container’s port to the host port i.e. port 8501. Streamlit works best on port 8501 specifically.

For a complete list of flags available for the docker run command, you may refer to the Docker documentation.

With that, the Streamlit app is now deployed with docker.

You can also push this dockerfile to the docker hub repository.


In this tutorial, we have learned how to containerize a Streamlit application using Docker. Docker containers are a common way of deploying Streamlit applications built for various business purposes.

Until then take care, Happy Learning!

Thanks for Reading!