Spaces:
Runtime error
Runtime error
Update Dockerfile
Browse files- Dockerfile +80 -12
Dockerfile
CHANGED
|
@@ -1,24 +1,92 @@
|
|
|
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
|
|
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
# Install
|
| 6 |
-
RUN apt-get update && apt-get install -y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Copy requirements and install Python
|
| 9 |
COPY requirements.txt .
|
| 10 |
-
RUN
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Create start script
|
| 19 |
-
RUN echo '#!/bin/bash\
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
| 1 |
+
# Dockerfile
|
| 2 |
FROM python:3.9-slim
|
| 3 |
|
| 4 |
+
EXPOSE 8501
|
| 5 |
+
|
| 6 |
WORKDIR /app
|
| 7 |
|
| 8 |
+
# Install system dependencies
|
| 9 |
+
RUN apt-get update && apt-get install -y \
|
| 10 |
+
build-essential \
|
| 11 |
+
software-properties-common \
|
| 12 |
+
git \
|
| 13 |
+
nginx \
|
| 14 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
|
| 16 |
+
# Copy requirements and install Python packages
|
| 17 |
COPY requirements.txt .
|
| 18 |
+
RUN pip3 install -r requirements.txt
|
| 19 |
+
|
| 20 |
+
# Copy the Streamlit app and configuration
|
| 21 |
+
COPY . .
|
| 22 |
|
| 23 |
+
# Create Streamlit config directory and add config.toml
|
| 24 |
+
RUN mkdir -p /root/.streamlit
|
| 25 |
+
RUN echo "\
|
| 26 |
+
[server]\n\
|
| 27 |
+
enableCORS = true\n\
|
| 28 |
+
enableXsrfProtection = false\n\
|
| 29 |
+
" > /root/.streamlit/config.toml
|
| 30 |
|
| 31 |
+
# Create Nginx configuration
|
| 32 |
+
RUN echo "\
|
| 33 |
+
events {\n\
|
| 34 |
+
worker_connections 1024;\n\
|
| 35 |
+
}\n\
|
| 36 |
+
http {\n\
|
| 37 |
+
upstream streamlit {\n\
|
| 38 |
+
server localhost:8501;\n\
|
| 39 |
+
}\n\
|
| 40 |
+
server {\n\
|
| 41 |
+
listen 80;\n\
|
| 42 |
+
server_name localhost;\n\
|
| 43 |
+
\n\
|
| 44 |
+
# CORS configuration\n\
|
| 45 |
+
add_header 'Access-Control-Allow-Origin' '*' always;\n\
|
| 46 |
+
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;\n\
|
| 47 |
+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;\n\
|
| 48 |
+
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;\n\
|
| 49 |
+
\n\
|
| 50 |
+
location / {\n\
|
| 51 |
+
proxy_pass http://streamlit;\n\
|
| 52 |
+
proxy_http_version 1.1;\n\
|
| 53 |
+
proxy_set_header Upgrade \$http_upgrade;\n\
|
| 54 |
+
proxy_set_header Connection 'upgrade';\n\
|
| 55 |
+
proxy_set_header Host \$host;\n\
|
| 56 |
+
proxy_cache_bypass \$http_upgrade;\n\
|
| 57 |
+
proxy_set_header X-Real-IP \$remote_addr;\n\
|
| 58 |
+
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n\
|
| 59 |
+
\n\
|
| 60 |
+
# CORS preflight\n\
|
| 61 |
+
if (\$request_method = 'OPTIONS') {\n\
|
| 62 |
+
add_header 'Access-Control-Allow-Origin' '*';\n\
|
| 63 |
+
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\n\
|
| 64 |
+
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';\n\
|
| 65 |
+
add_header 'Access-Control-Max-Age' 1728000;\n\
|
| 66 |
+
add_header 'Content-Type' 'text/plain; charset=utf-8';\n\
|
| 67 |
+
add_header 'Content-Length' 0;\n\
|
| 68 |
+
return 204;\n\
|
| 69 |
+
}\n\
|
| 70 |
+
}\n\
|
| 71 |
+
}\n\
|
| 72 |
+
}" > /etc/nginx/nginx.conf
|
| 73 |
|
| 74 |
# Create start script
|
| 75 |
+
RUN echo '#!/bin/bash\n\
|
| 76 |
+
service nginx start\n\
|
| 77 |
+
streamlit run streamlit.py --server.port=8501 --server.address=0.0.0.0\n\
|
| 78 |
+
' > /app/start.sh
|
| 79 |
+
RUN chmod +x /app/start.sh
|
| 80 |
+
|
| 81 |
+
# Create a sample Streamlit app
|
| 82 |
+
RUN echo "\
|
| 83 |
+
import streamlit as st\n\
|
| 84 |
+
\n\
|
| 85 |
+
st.title('Sample Streamlit App with CORS')\n\
|
| 86 |
+
st.write('Hello, this is a sample Streamlit app with CORS enabled!')\n\
|
| 87 |
+
" > /app/streamlit.py
|
| 88 |
|
| 89 |
+
# Create requirements.txt if not exists
|
| 90 |
+
RUN if [ ! -f requirements.txt ]; then echo "streamlit>=1.0.0" > requirements.txt; fi
|
| 91 |
|
| 92 |
+
ENTRYPOINT ["/app/start.sh"]
|