Search code examples
azureazure-web-app-servicestreamlitazureml-python-sdkgradio

build and deploy docker container to build fast api app on Azure web app


I am currently testing a small project and developing within the Azure ML notebook environemnt. Due to the confidential issue, I have to test within the Azure. I have a python script that I would like to push into the docker container and to the Azure container registry and subsequently use streamlit/gradio or fast api to do a simple web app demo. I am wondering, what is the best way to go about this without leaving the azure workspace. I see a lot of tutorial building in your local environment, but that would not be feasible for me.

*Is there a way to build such docker from the azure ml notebook and push into the registry?

*Using gradio/streamlit within azure notebook is also not possible as the the web portal needs to be hosted so quick testing of the layout is also kind of nightmare. Any suggestion on that?

Thank you


Solution

    • Here I created Azure ML workspace and navigate to the "Notebooks" section and create a new Python 3.8-based notebook.

    enter image description here

    Dockerfile file:

    # Use the official Python image from Docker Hub
    FROM python:3.8-slim
    
    # Set the working directory to /app
    WORKDIR /app
    
    # Copy the current directory contents into the container at /app
    COPY . /app
    
    # Install any dependencies
    RUN pip install -r requirements.txt
    
    # Expose port 8501 for Streamlit
    EXPOSE 8501
    
    # Run the Streamlit app
    CMD ["streamlit", "run", "testlikeapro.py"]
    
    • In my script I have streamlit dependencies so, I created requirement.txt file in the same directory as Dockerfile existed.

    Then build the docker image using !docker build -t your-image-name:tag .

    enter image description here

    I am able to see the docker image in below.

    enter image description here

    Then Login into Azure Container Registry using az acr login --name your-Acr-name

    • Again build the image for you ACR using docker build -t your-Acr-name.azurecr.io/belikepro:latest .

    enter image description here

    Successfully the image is pushed into ACR registries.

    • Then create a web application and configured the Docker container settings in the App Service to point to the ACR image.

    enter image description here

    Result:

    enter image description here