Skip to content

Commit 5d6b668

Browse files
authored
Merge pull request #4 from jon23d/feature/add-tz
sync timezones and add install script
2 parents 5329fb3 + 9f3fd09 commit 5d6b668

8 files changed

Lines changed: 246 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,28 @@ This will create Docker images tagged as `jdcode-python` and `jdcode-ts`.
4545

4646
## 🌍 How to Use the Docker Containers
4747

48+
### Quick Start with Simplified Interface
49+
50+
We've created a simplified CLI tool to make running containers much easier:
51+
52+
1. Install jdcode CLI (run once):
53+
```bash
54+
./install-jdcode.sh
55+
```
56+
57+
2. Run containers with minimal commands:
58+
```bash
59+
jdcode python # Run Python variant
60+
jdcode ts # Run TypeScript variant
61+
```
62+
63+
### Manual Docker Commands (Legacy)
64+
65+
If you prefer to run docker commands directly:
66+
4867
> Note: Binding ports in docker uses the -p flag in the form HOST_PORT:CONTAINER_PORT
4968
50-
### Basic Usage
69+
#### Basic Usage
5170

5271
1. **Run the containers**:
5372
Start each environment by binding the appropriate port:
@@ -68,7 +87,7 @@ This will create Docker images tagged as `jdcode-python` and `jdcode-ts`.
6887
3. **Access OpenCode**
6988
- In your container, execute `opencode`
7089

71-
### With Telegram Notifications
90+
#### With Telegram Notifications
7291

7392
Both variants include a Telegram notification plugin that sends messages via a Telegram bot when OpenCode session events occur (idle, errors, completion).
7493

@@ -160,6 +179,25 @@ jdcode-python-notify # Python with Telegram notifications
160179
jdcode-ts-notify # TypeScript with Telegram notifications
161180
```
162181

182+
### Advanced Usage with CLI Tool
183+
184+
If you've installed the jdcode CLI tool, you can also run:
185+
186+
```bash
187+
# Basic usage
188+
jdcode python # Run Python variant
189+
jdcode ts # Run TypeScript variant
190+
191+
# With custom port
192+
jdcode python --port 9000
193+
194+
# With Telegram notifications
195+
jdcode python --telegram
196+
197+
# Using environment file
198+
jdcode ts --env-file .env
199+
```
200+
163201
---
164202
165203
## ⚙️ Notes for Testing Play Buttons with Vitest (for the TypeScript Variant)

docker/base/entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44
# Create log directory
55
mkdir -p /var/log
66

7-
code-server --port "${PORT}" --auth none --bind-addr "0.0.0.0:${PORT}" /code > /var/log/code-server.log 2>&1 &
7+
code-server --port 8080 --auth none --bind-addr "0.0.0.0:8080" /code > /var/log/code-server.log 2>&1 &
88

99
# Give code-server a moment to start
1010
sleep 3

docker/base/opencode.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"baseURL": "http://host.docker.internal:11434/v1"
99
},
1010
"models": {
11-
"jcoder2": {
12-
"name": "jcoder2:latest"
11+
"jdcoder2": {
12+
"name": "jdcoder2:latest"
1313
},
1414
"qwen3-coder": {
1515
"name": "qwen3-coder"

docker/variants/python/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ RUN apt-get update && \
2727
COPY base/entrypoint.sh /usr/local/bin/entrypoint.sh
2828
RUN chmod +x /usr/local/bin/entrypoint.sh
2929

30+
# Set timezone to match host system
31+
RUN ln -sf /etc/localtime /usr/local/etc/localtime
32+
3033
# ─────────────────────────────────────────────
3134
# Python-specific dependencies
3235
# ─────────────────────────────────────────────

docker/variants/ts/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ RUN apt-get update && \
2727
COPY base/entrypoint.sh /usr/local/bin/entrypoint.sh
2828
RUN chmod +x /usr/local/bin/entrypoint.sh
2929

30+
# Set timezone to match host system
31+
RUN ln -sf /etc/localtime /usr/local/etc/localtime
32+
3033
# ─────────────────────────────────────────────
3134
# TypeScript-specific dependencies
3235
# ─────────────────────────────────────────────

install-jdcode.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo "Installing jdcode CLI tool..."
6+
7+
# Copy the jdcode script to a directory in PATH if possible
8+
if command -v which >/dev/null 2>&1; then
9+
10+
# Check user's preferred locations first
11+
if [ -d "$HOME/bin" ] && [ -w "$HOME/bin" ]; then
12+
TARGET_DIR="$HOME/bin"
13+
elif [ -d "$HOME/.local/bin" ] && [ -w "$HOME/.local/bin" ]; then
14+
TARGET_DIR="$HOME/.local/bin"
15+
elif [ -w "/usr/local/bin" ]; then
16+
TARGET_DIR="/usr/local/bin"
17+
else
18+
# Try to find a suitable location in PATH
19+
TARGET_DIR=""
20+
for dir in $(echo $PATH | tr ':' ' '); do
21+
if [ -w "$dir" ] && [ -d "$dir" ]; then
22+
TARGET_DIR="$dir"
23+
break
24+
fi
25+
done
26+
fi
27+
28+
if [ -n "$TARGET_DIR" ]; then
29+
cp ./jdcode "$TARGET_DIR/jdcode"
30+
chmod +x "$TARGET_DIR/jdcode"
31+
echo "jdcode installed to $TARGET_DIR/jdcode"
32+
echo "You can now use: jdcode python or jdcode ts"
33+
else
34+
echo "Warning: No writable directory in PATH found"
35+
echo "Please copy the jdcode script to a directory in your PATH"
36+
echo "Or run: cp /code/jdcode ~/bin/jdcode && chmod +x ~/bin/jdcode"
37+
fi
38+
else
39+
echo "Warning: which command not found, installation may be limited"
40+
fi
41+
42+
echo ""
43+
echo "Installation complete!"
44+
echo "Usage examples:"
45+
echo " jdcode python # Run Python variant"
46+
echo " jdcode ts # Run TypeScript variant"
47+
echo " jdcode python --port 9000 # Run with custom port"
48+
echo " jdcode --help # Show help"

jdcode

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Default values
6+
VARIANT=""
7+
PORT="8080"
8+
# TELEGRAM_ENABLED can be set via env var (TELEGRAM_ENABLED=1|true|yes) or via --telegram flag.
9+
# Initialize from environment if present, otherwise default to false.
10+
if [ -n "${TELEGRAM_ENABLED:-}" ]; then
11+
case "$(echo "${TELEGRAM_ENABLED}" | tr '[:upper:]' '[:lower:]')" in
12+
1|true|yes|on) TELEGRAM_ENABLED=true ;;
13+
*) TELEGRAM_ENABLED=false ;;
14+
esac
15+
else
16+
TELEGRAM_ENABLED=false
17+
fi
18+
ENV_FILE=""
19+
COMMAND="run"
20+
21+
# Parse command line arguments
22+
while [[ $# -gt 0 ]]; do
23+
case $1 in
24+
python|ts)
25+
VARIANT="$1"
26+
shift
27+
;;
28+
-p|--port)
29+
PORT="$2"
30+
shift 2
31+
;;
32+
--telegram)
33+
TELEGRAM_ENABLED=true
34+
shift
35+
;;
36+
--env-file)
37+
ENV_FILE="$2"
38+
shift 2
39+
;;
40+
--help|-h)
41+
echo "jdcode - Simplified interface for jdcode containers"
42+
echo ""
43+
echo "Usage:"
44+
echo " jdcode [python|ts] [options]"
45+
echo ""
46+
echo "Options:"
47+
echo " python|ts Run Python or TypeScript variant"
48+
echo " -p, --port PORT Port to expose (default: 8080)"
49+
echo " --telegram Enable Telegram notifications"
50+
echo " --env-file FILE Use environment file"
51+
echo " --help, -h Show this help message"
52+
echo ""
53+
echo "Examples:"
54+
echo " jdcode python # Run Python variant with default settings"
55+
echo " jdcode ts --port 9000 # Run TypeScript variant on port 9000"
56+
echo " jdcode python --telegram # Run Python with Telegram notifications"
57+
exit 0
58+
;;
59+
*)
60+
echo "Unknown option: $1"
61+
exit 1
62+
;;
63+
esac
64+
done
65+
66+
# Auto-detect variant if not specified
67+
if [ -z "$VARIANT" ]; then
68+
if [ -f "package.json" ] || [ -f "tsconfig.json" ]; then
69+
VARIANT="ts"
70+
elif [ -f "requirements.txt" ] || [ -f "pyproject.toml" ]; then
71+
VARIANT="python"
72+
else
73+
echo "Error: Could not detect variant. Please specify 'python' or 'ts'"
74+
exit 1
75+
fi
76+
fi
77+
78+
# Validate variant
79+
if [ "$VARIANT" != "python" ] && [ "$VARIANT" != "ts" ]; then
80+
echo "Error: Invalid variant. Must be 'python' or 'ts'"
81+
exit 1
82+
fi
83+
84+
# Build the docker command
85+
DOCKER_CMD="docker run -it --rm"
86+
87+
# Determine the system time zone (try multiple methods for portability)
88+
# Prefer a named timezone (e.g. America/Los_Angeles). Fall back to abbreviation if necessary.
89+
if command -v timedatectl >/dev/null 2>&1; then
90+
TIMEZONE=$(timedatectl show -p TimeZone --value 2>/dev/null || true)
91+
elif [ -f /etc/timezone ]; then
92+
TIMEZONE=$(cat /etc/timezone 2>/dev/null || true)
93+
elif [ -L /etc/localtime ]; then
94+
# try to extract zoneinfo path from symlink
95+
LINK=$(readlink /etc/localtime 2>/dev/null || true)
96+
if [ -n "$LINK" ]; then
97+
TIMEZONE=$(echo "$LINK" | sed 's|.*/zoneinfo/||')
98+
elif command -v realpath >/dev/null 2>&1; then
99+
TIMEZONE=$(realpath /etc/localtime 2>/dev/null | sed 's|.*/zoneinfo/||' || true)
100+
else
101+
TIMEZONE=""
102+
fi
103+
else
104+
TIMEZONE=""
105+
fi
106+
107+
# Fallback: use timezone abbreviation if we couldn't find a named timezone
108+
if [ -z "$TIMEZONE" ]; then
109+
TIMEZONE=$(date +%Z 2>/dev/null || true)
110+
fi
111+
112+
# Only add TZ env var if we found something
113+
if [ -n "$TIMEZONE" ]; then
114+
DOCKER_CMD="$DOCKER_CMD -e TZ=\"$TIMEZONE\""
115+
fi
116+
117+
# Add volume mounts
118+
DOCKER_CMD="$DOCKER_CMD -v \"\$PWD\":/code"
119+
120+
# Add port mapping
121+
DOCKER_CMD="$DOCKER_CMD -p $PORT:8080"
122+
123+
# Add environment file if specified
124+
if [ -n "$ENV_FILE" ] && [ -f "$ENV_FILE" ]; then
125+
DOCKER_CMD="$DOCKER_CMD --env-file $ENV_FILE"
126+
fi
127+
128+
# Add Telegram environment variables if enabled
129+
if [ "$TELEGRAM_ENABLED" = true ]; then
130+
# Check if required environment variables exist
131+
if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then
132+
echo "Error: TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID must be set in environment"
133+
echo "You can set them with:"
134+
echo " export TELEGRAM_BOT_TOKEN=your_token"
135+
echo " export TELEGRAM_CHAT_ID=your_chat_id"
136+
exit 1
137+
fi
138+
DOCKER_CMD="$DOCKER_CMD -e TELEGRAM_ENABLED=true"
139+
DOCKER_CMD="$DOCKER_CMD -e TELEGRAM_BOT_TOKEN=\"$TELEGRAM_BOT_TOKEN\""
140+
DOCKER_CMD="$DOCKER_CMD -e TELEGRAM_CHAT_ID=\"$TELEGRAM_CHAT_ID\""
141+
fi
142+
143+
# Add image name
144+
DOCKER_CMD="$DOCKER_CMD jdcode-$VARIANT"
145+
146+
# Execute the command
147+
echo "Running: $DOCKER_CMD"
148+
eval $DOCKER_CMD

0 commit comments

Comments
 (0)