runitx commited on
Commit
24bf580
·
unverified ·
1 Parent(s): 207bf71

Configure Docker with tmate SSH tunnel

Browse files
Files changed (5) hide show
  1. Dockerfile +14 -35
  2. README.md +1 -1
  3. app.py +0 -69
  4. start.sh +117 -0
  5. supervisord.conf +0 -16
Dockerfile CHANGED
@@ -1,48 +1,27 @@
1
  FROM python:3.10-slim
2
 
3
- # 1. Install SSH and supervisor (to run two processes at once)
4
  RUN apt-get update && apt-get install -y \
5
- openssh-server \
6
- supervisor \
 
 
7
  && rm -rf /var/lib/apt/lists/*
8
 
9
- # 2. SSHD requires this directory to exist to run
10
- RUN mkdir -p /run/sshd
11
-
12
- # 3. Create the standard HF user (UID 1000)
13
  RUN useradd -m -u 1000 user
14
-
15
- # 4. Set up the SSH authorized_keys for the user
16
- RUN mkdir -p /home/user/.ssh && chmod 700 /home/user/.ssh
17
-
18
- # PASTE YOUR ~/.ssh/id_rsa.pub (or id_ed25519.pub) STRING DIRECTLY IN THE QUOTES BELOW:
19
- RUN echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINSySUA8UiToR745UcU9Y10BCb6k0BHVOClO33PPH//v github-push-2026-06-20" > /home/user/.ssh/authorized_keys
20
-
21
- RUN chmod 600 /home/user/.ssh/authorized_keys
22
- RUN chown -R user:user /home/user/.ssh
23
-
24
- # 5. Configure SSHD to listen on HF's only open port (7860)
25
- # and allow key-based login for 'user'
26
- RUN echo "Port 7860\n\
27
- ListenAddress 0.0.0.0\n\
28
- PermitRootLogin no\n\
29
- PasswordAuthentication no\n\
30
- PubkeyAuthentication yes\n\
31
- AllowUsers user" > /etc/ssh/sshd_config.d/custom.conf
32
-
33
- # 6. Copy your supervisor config (created in Step 2)
34
- COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
35
-
36
- # 7. HF strictly requires the container to run as user 1000, NOT root.
37
- # However, sshd strictly requires root to boot.
38
- # Fix: Give 'user' ownership of the sshd run folder so a non-root user can boot it.
39
- RUN chown -R user:user /run/sshd /etc/ssh
40
-
41
  USER user
 
42
  WORKDIR /home/user/app
43
 
 
44
  COPY --chown=user . .
45
 
 
 
 
 
46
  EXPOSE 7860
47
 
48
- CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
 
 
1
  FROM python:3.10-slim
2
 
3
+ # Install tmate and other utilities
4
  RUN apt-get update && apt-get install -y \
5
+ tmate \
6
+ openssh-client \
7
+ curl \
8
+ git \
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
+ # Create user with UID 1000 (Hugging Face default user)
 
 
 
12
  RUN useradd -m -u 1000 user
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  USER user
14
+ ENV HOME=/home/user
15
  WORKDIR /home/user/app
16
 
17
+ # Copy application files
18
  COPY --chown=user . .
19
 
20
+ # Make sure start.sh is executable
21
+ RUN chmod +x start.sh
22
+
23
+ # Expose port 7860 for the web interface
24
  EXPOSE 7860
25
 
26
+ # Run the startup script
27
+ CMD ["./start.sh"]
README.md CHANGED
@@ -3,7 +3,7 @@ title: Test
3
  emoji: 💬
4
  colorFrom: yellow
5
  colorTo: purple
6
- sdk: gradio
7
  sdk_version: 6.5.1
8
  app_file: app.py
9
  pinned: false
 
3
  emoji: 💬
4
  colorFrom: yellow
5
  colorTo: purple
6
+ sdk: docker
7
  sdk_version: 6.5.1
8
  app_file: app.py
9
  pinned: false
app.py DELETED
@@ -1,69 +0,0 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
-
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
- with gr.Blocks() as demo:
63
- with gr.Sidebar():
64
- gr.LoginButton()
65
- chatbot.render()
66
-
67
-
68
- if __name__ == "__main__":
69
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
start.sh ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Start tmate in a socket
4
+ echo "Starting tmate..."
5
+ tmate -S /tmp/tmate.sock new-session -d
6
+
7
+ # Wait for tmate to connect and get the SSH connection string
8
+ echo "Waiting for tmate to generate SSH URL..."
9
+ for i in {1..30}; do
10
+ SSH_CONN=$(tmate -S /tmp/tmate.sock display -p '#{tmate_ssh}' 2>/dev/null)
11
+ if [ -n "$SSH_CONN" ]; then
12
+ echo "============================================="
13
+ echo "SSH connection string: $SSH_CONN"
14
+ echo "============================================="
15
+ break
16
+ fi
17
+ sleep 1
18
+ done
19
+
20
+ # If SSH_CONN is empty, print error
21
+ if [ -z "$SSH_CONN" ]; then
22
+ echo "Failed to get tmate SSH connection string."
23
+ SSH_CONN="Error: Could not retrieve tmate SSH connection string."
24
+ fi
25
+
26
+ # Create a simple web page to display the SSH connection details
27
+ cat <<EOF > index.html
28
+ <!DOCTYPE html>
29
+ <html>
30
+ <head>
31
+ <title>SSH Tunnel Connection Details</title>
32
+ <style>
33
+ body {
34
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
35
+ background-color: #0f172a;
36
+ color: #f8fafc;
37
+ display: flex;
38
+ justify-content: center;
39
+ align-items: center;
40
+ height: 100vh;
41
+ margin: 0;
42
+ }
43
+ .container {
44
+ background-color: #1e293b;
45
+ padding: 2.5rem;
46
+ border-radius: 12px;
47
+ box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
48
+ text-align: center;
49
+ max-width: 600px;
50
+ width: 90%;
51
+ border: 1px solid #334155;
52
+ }
53
+ h1 {
54
+ color: #38bdf8;
55
+ margin-top: 0;
56
+ font-size: 1.8rem;
57
+ }
58
+ p {
59
+ font-size: 1rem;
60
+ color: #94a3b8;
61
+ line-height: 1.5;
62
+ }
63
+ .code-block {
64
+ background-color: #090d16;
65
+ color: #34d399;
66
+ padding: 1rem;
67
+ border-radius: 6px;
68
+ font-family: monospace;
69
+ font-size: 1.1rem;
70
+ word-break: break-all;
71
+ margin: 1.5rem 0;
72
+ border: 1px solid #1e293b;
73
+ user-select: all;
74
+ cursor: pointer;
75
+ }
76
+ .instructions {
77
+ text-align: left;
78
+ margin-top: 2rem;
79
+ border-top: 1px solid #334155;
80
+ padding-top: 1.5rem;
81
+ }
82
+ .instructions h3 {
83
+ color: #cbd5e1;
84
+ margin-bottom: 0.5rem;
85
+ }
86
+ .instructions ol {
87
+ color: #94a3b8;
88
+ padding-left: 1.2rem;
89
+ margin-top: 0.5rem;
90
+ }
91
+ .instructions li {
92
+ margin-bottom: 0.5rem;
93
+ }
94
+ </style>
95
+ </head>
96
+ <body>
97
+ <div class="container">
98
+ <h1>SSH Access Enabled</h1>
99
+ <p>Your Hugging Face Space Docker container is running and has established a secure tunnel via tmate.</p>
100
+ <p>Click below to select and copy the SSH command:</p>
101
+ <div class="code-block">${SSH_CONN}</div>
102
+ <div class="instructions">
103
+ <h3>How to connect:</h3>
104
+ <ol>
105
+ <li>Copy the command above.</li>
106
+ <li>Paste it in your terminal and press Enter.</li>
107
+ <li>You will have direct shell access to the Hugging Face Space container.</li>
108
+ </ol>
109
+ </div>
110
+ </div>
111
+ </body>
112
+ </html>
113
+ EOF
114
+
115
+ # Start a simple Python web server to serve the page on port 7860
116
+ echo "Starting web server on port 7860..."
117
+ python3 -m http.server 7860
supervisord.conf DELETED
@@ -1,16 +0,0 @@
1
- [supervisord]
2
- nodaemon=true
3
- user=user
4
-
5
- [program:sshd]
6
- command=/usr/sbin/sshd -D -e
7
- autostart=true
8
- autorestart=true
9
- priority=10
10
-
11
- [program:my_web_app]
12
- ; CHANGE THIS LINE to whatever your actual app start command is:
13
- command=python main.py
14
- autostart=true
15
- autorestart=true
16
- priority=20