Deployment fails on Cloud Run

Hi.

I’m using Google Cloud Run to deploy a Streamlit application.
Cloud Build fails to deploy even though Docker is running successfully in local env.

I looked at the build log and found the following message.

ERROR: (gcloud.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.

I tried to do my own trial and error, but I couldn’t solve the problem.
If you know of a solution or an example repository please help me.

The current code for my application is shown below.

# .streamlit/config.toml
[browser]
serverAddress = "0.0.0.0"

[server]
enableCORS = false
headless = true
# Dockerfile
FROM python:3.8 as builder

WORKDIR /app

COPY . .

RUN pip install poetry
RUN poetry install --no-dev

CMD ["make", "run"]
# Makefile
PORT ?= 8080

run:
	poetry run streamlit run --server.port ${PORT} main.py
# main.py
import streamlit as st


def main():
    st.balloons()


if __name__ == "__main__":
    main()

Thanks.

I solved thid issue myself.
I always thought it was a Streamlit or Docker issue, but Poetry was the cause of the error.

Added RUN poetry config virtualenvs.create false && poetry install --no-dev

2 Likes

Glad you found out :slight_smile: and thanks for sharing the solution !

Hi, Have you had any luck in deploying streamlit on cloudrun.
I was able to deploy it without any issues but st.write doesnt produce any output.

I built a test app with just the following lines and there was no output.
st.title(‘My title’)
st.header(“my header”)
st.text(“my test text”)

I’m in a similar boat. App is deployed and running, but I don’t have a web app to handle authentication so the only way to get at the page is:

  1. Allow unauthenticated access
  2. Send a request with a custom header somehow

Unauthenticated access is a non-starter because of the data I’m displaying, and I don’t know of a way to send a custom header request somewhere that I can actually display the webpage when it loads. It would also be too cumbersome for non-developers to use.

I managed to resolve the issue myself after exploring few options.
#Dockerfile

Python image to use.

FROM python:3.9

Set the working directory to /app

WORKDIR /app

copy the requirements file used for dependencies

COPY requirements.txt .

Install any needed packages specified in requirements.txt

RUN pip install --trusted-host pypi.python.org -r requirements.txt

Copy the rest of the working directory contents into the container at /app

COPY . .

Run app.py when the container launches

ENTRYPOINT [“python”, “app.py”]

CMD streamlit run --server.port 8080 --server.enableCORS false interface.py

end of docker file.

The last line is very important. You can structure the interface.py similar to any python file.

interface.py

import

def run_script():

if name == ‘main’:
run_script()

hope this helps someone who has issues in deploying cloudrun.

@Arun thank you for adding the code.

wanted to check from you , should i install all the packages used in streamlit in the requirements code ?

in addiiton, i’m use different local data files “.csv” “.png” will they be copied while i add the " COPY… " code ?

and last thing, in the end of docker file the script condition is it fixed " name==‘main’ or i should change it to my own py directory file ?

many thanks for the support.

Sheers.

The last line of the Docker file is
CMD streamlit run --server.port 8080 --server.enableCORS false interface.py

where interface.py is the python script which holds the logic. In your case, you need to change “interface.py” to your script file.py name.

Requirements should include all the libraries that your script will need. In addition, you will need
streamlit
streamlit-aggrid
in case you are using the AgGrid for displaying the output of a dataframe.

Docker file can copy any file that you include in the list . Here is an example, paste the below lines in your docker to copy all of the files.

Copy the rest of the working directory contents into the container at /app

COPY . .

GCP Cloud Run has enables websockets (June 2021) …more details and successful example …Streamlit on GCP Cloud Run