File size: 2,978 Bytes
d8e619d
d9cfc20
 
d8e619d
 
d9cfc20
 
d8e619d
 
 
 
 
 
 
d9cfc20
d8e619d
d9cfc20
d8e619d
 
 
 
d9cfc20
d8e619d
 
 
 
 
 
 
d9cfc20
d8e619d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d9cfc20
 
d8e619d
 
 
 
 
 
 
 
 
 
 
 
 
d9cfc20
d8e619d
 
d9cfc20
d8e619d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Dockerfile
FROM python:3.9-slim

EXPOSE 8501

WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    software-properties-common \
    git \
    nginx \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements and install Python packages
COPY requirements.txt .
RUN pip3 install -r requirements.txt

# Copy the Streamlit app and configuration
COPY . .

# Create Streamlit config directory and add config.toml
RUN mkdir -p /root/.streamlit
RUN echo "\
[server]\n\
enableCORS = true\n\
enableXsrfProtection = false\n\
" > /root/.streamlit/config.toml

# Create Nginx configuration
RUN echo "\
events {\n\
    worker_connections 1024;\n\
}\n\
http {\n\
    upstream streamlit {\n\
        server localhost:8501;\n\
    }\n\
    server {\n\
        listen 80;\n\
        server_name localhost;\n\
\n\
        # CORS configuration\n\
        add_header 'Access-Control-Allow-Origin' '*' always;\n\
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;\n\
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;\n\
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;\n\
\n\
        location / {\n\
            proxy_pass http://streamlit;\n\
            proxy_http_version 1.1;\n\
            proxy_set_header Upgrade \$http_upgrade;\n\
            proxy_set_header Connection 'upgrade';\n\
            proxy_set_header Host \$host;\n\
            proxy_cache_bypass \$http_upgrade;\n\
            proxy_set_header X-Real-IP \$remote_addr;\n\
            proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n\
\n\
            # CORS preflight\n\
            if (\$request_method = 'OPTIONS') {\n\
                add_header 'Access-Control-Allow-Origin' '*';\n\
                add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';\n\
                add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';\n\
                add_header 'Access-Control-Max-Age' 1728000;\n\
                add_header 'Content-Type' 'text/plain; charset=utf-8';\n\
                add_header 'Content-Length' 0;\n\
                return 204;\n\
            }\n\
        }\n\
    }\n\
}" > /etc/nginx/nginx.conf

# Create start script
RUN echo '#!/bin/bash\n\
service nginx start\n\
streamlit run streamlit.py --server.port=8501 --server.address=0.0.0.0\n\
' > /app/start.sh
RUN chmod +x /app/start.sh

# Create a sample Streamlit app
RUN echo "\
import streamlit as st\n\
\n\
st.title('Sample Streamlit App with CORS')\n\
st.write('Hello, this is a sample Streamlit app with CORS enabled!')\n\
" > /app/streamlit.py

# Create requirements.txt if not exists
RUN if [ ! -f requirements.txt ]; then echo "streamlit>=1.0.0" > requirements.txt; fi

ENTRYPOINT ["/app/start.sh"]