Skip to content

Commit 247593d

Browse files
committed
[Docker] Fix & New: Make SSH Initialization at Runtime instead of Buildtime
1 parent 24359d6 commit 247593d

7 files changed

Lines changed: 109 additions & 15 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ DSH listens on container loopback `127.0.0.1:3080`; `socat` bridges it to contai
1212

1313
### Prerequisites
1414

15-
Use a Linux host with Docker Engine, Compose v2, AppArmor, an SSH key pair, and repository access. Put only the login public key in `build/authorized_keys`; never add a private key or API token.
15+
Use a Linux host with Docker Engine, Compose v2, AppArmor, an SSH key pair, and repository access. Keep the private key on the host. See [First Run](docs/README_FIRST_RUN.md) for runtime public-key injection.
1616

1717
Install the host AppArmor profile before starting the service:
1818

@@ -27,10 +27,10 @@ Build and start the environment:
2727
```sh
2828
docker compose config
2929
docker compose build --build-arg DSH_REF=master
30-
docker compose up -d
30+
SSH_PUB_KEY="$(cat ~/.ssh/id_ed25519.pub)" docker compose up -d
3131
```
3232

33-
`DSH_REF` may be a DSH branch or tag.
33+
`DSH_REF` may be a DSH branch or tag. The validated key is persisted in `dsh-data`; subsequent starts do not require `SSH_PUB_KEY`.
3434

3535
Open DSH at `http://127.0.0.1:3080`, or connect over SSH:
3636

@@ -75,4 +75,4 @@ Use `docker compose logs -f deepseek-harness` for logs and `docker compose down`
7575

7676
## License
7777

78-
MIT © Alan Lyu – see [LICENSE](./LICENSE) file for details.
78+
MIT © Alan Lyu – see [LICENSE](LICENSE) file for details.

‎README_zh.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
### 准备工作
1414

15-
宿主机需要 Linux、Docker Engine、Compose v2、AppArmor、SSH 密钥对,以及访问软件包和源码仓库的网络条件。只将用于登录的公钥放入 `build/authorized_keys`,不要加入私钥或 API token。
15+
宿主机需要 Linux、Docker Engine、Compose v2、AppArmor、SSH 密钥对,以及访问软件包和源码仓库的网络条件。私钥始终保留在宿主机上;运行时公钥注入方法见[首次运行指南](docs/README_FIRST_RUN.md)。
1616

1717
启动服务前,在宿主机安装 AppArmor 配置:
1818

@@ -27,10 +27,10 @@ sudo aa-status | grep deepseek-harness
2727
```sh
2828
docker compose config
2929
docker compose build --build-arg DSH_REF=master
30-
docker compose up -d
30+
SSH_PUB_KEY="$(cat ~/.ssh/id_ed25519.pub)" docker compose up -d
3131
```
3232

33-
`DSH_REF` 可以指定 DSH 的分支或标签;也可以通过构建参数将容器内 developer 的 UID/GID 与宿主机工作区所有者对齐。
33+
`DSH_REF` 可以指定 DSH 的分支或标签;也可以通过构建参数将容器内 developer 的 UID/GID 与宿主机工作区所有者对齐。公钥验证后保存在 `dsh-data` 中,后续启动无需再次提供 `SSH_PUB_KEY`。
3434

3535
浏览器访问 `http://127.0.0.1:3080`,或通过 SSH 连接:
3636

@@ -77,4 +77,4 @@ docker compose up -d --force-recreate
7777

7878
## License
7979

80-
MIT © Alan Lyu – see [LICENSE](./LICENSE) file for details.
80+
MIT © Alan Lyu – see [LICENSE](LICENSE) file for details.

‎build/.dockerignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
*
22
!Dockerfile
3-
!authorized_keys
43
!docker-entrypoint.sh
54
!dsh-launcher-package.json

‎build/Dockerfile‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,14 @@ RUN set -eux; \
114114
chmod 0440 /etc/sudoers.d/developer; \
115115
mkdir -p /home/developer/.ssh /home/developer/.cargo /home/developer/workspace; \
116116
chmod 700 /home/developer/.ssh; \
117+
install -o developer -g developer -m 0600 /dev/null /home/developer/.ssh/authorized_keys; \
117118
chown -R developer:developer /home/developer
118119
119-
# Configure SSH public-key authentication.
120-
COPY --chown=developer:developer authorized_keys \
121-
/home/developer/.ssh/authorized_keys
122-
123120
# Provide an independent launcher manifest while keeping DSH's working directory in the mounted workspace.
124121
COPY --chown=developer:developer dsh-launcher-package.json \
125122
/home/developer/.dsh-launcher/package.json
126123

127-
RUN chmod 600 /home/developer/.ssh/authorized_keys \
128-
&& chown -R developer:developer /home/developer/.dsh-launcher \
124+
RUN chown -R developer:developer /home/developer/.dsh-launcher \
129125
&& install -d -m 0755 /run/sshd /etc/ssh/sshd_config.d \
130126
&& printf '%s\n' \
131127
'Port 2222' \

‎build/docker-entrypoint.sh‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ set -eu
33

44
DSH_DATA_DIR=/data/dsh
55
PNPM_HOME="${DSH_DATA_DIR}/pnpm"
6+
SSH_STATE_DIR="${DSH_DATA_DIR}/ssh"
7+
SSH_STATE_FILE="${SSH_STATE_DIR}/authorized_keys"
8+
SSH_TARGET_DIR=/home/developer/.ssh
9+
SSH_TARGET_FILE="${SSH_TARGET_DIR}/authorized_keys"
610

711
umask 077
812

@@ -12,6 +16,26 @@ chown -R developer:developer "${DSH_DATA_DIR}"
1216
chmod 0700 "${DSH_DATA_DIR}"
1317
install -d -o developer -g developer -m 0700 "${PNPM_HOME}"
1418
install -d -o developer -g developer -m 0755 /home/developer/workspace
19+
install -d -o developer -g developer -m 0700 "${SSH_STATE_DIR}" "${SSH_TARGET_DIR}"
20+
21+
if [ -n "${SSH_PUB_KEY:-}" ]; then
22+
SSH_KEY_TEMP="${SSH_STATE_FILE}.tmp"
23+
printf '%s\n' "${SSH_PUB_KEY}" | tr -d '\r' > "${SSH_KEY_TEMP}"
24+
chmod 0600 "${SSH_KEY_TEMP}"
25+
if ! ssh-keygen -l -f "${SSH_KEY_TEMP}" >/dev/null 2>&1; then
26+
rm -f "${SSH_KEY_TEMP}"
27+
echo "Invalid SSH_PUB_KEY: expected an OpenSSH public key." >&2
28+
exit 1
29+
fi
30+
chown developer:developer "${SSH_KEY_TEMP}"
31+
mv "${SSH_KEY_TEMP}" "${SSH_STATE_FILE}"
32+
elif [ ! -s "${SSH_STATE_FILE}" ]; then
33+
echo "SSH_PUB_KEY is required for the first run or after deleting dsh-data." >&2
34+
exit 1
35+
fi
36+
37+
install -o developer -g developer -m 0600 "${SSH_STATE_FILE}" "${SSH_TARGET_FILE}"
38+
unset SSH_PUB_KEY
1539

1640
/usr/sbin/sshd -e
1741

‎docker-compose.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ services:
3232
# The data directory must match DSH_HOME in the Dockerfile.
3333
- DSH_HOME=/data/dsh
3434
- DSH_PORT=3080
35+
# Required on first run; the validated public key is then persisted in dsh-data.
36+
- SSH_PUB_KEY=${SSH_PUB_KEY:-}
3537

3638
volumes:
3739
dsh-data: # Named volume managed by Docker.

‎docs/README_FIRST_RUN.md‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# First Run / 首次运行
2+
3+
SSH access is initialized at runtime. Provide an OpenSSH public key through `SSH_PUB_KEY` on the first start; the entrypoint validates it and stores it in the persistent `dsh-data` volume. Never pass a private key, token, or other secret through this variable.
4+
5+
SSH 访问在运行时初始化。首次启动时通过 `SSH_PUB_KEY` 提供 OpenSSH 公钥;入口脚本验证后将其保存在持久化的 `dsh-data` 卷中。不要通过该变量传入私钥、token 或其他秘密。
6+
7+
## Start with Compose / 使用 Compose 启动
8+
9+
Install the AppArmor profile, create the workspace, then build and start:
10+
11+
安装 AppArmor 配置、创建工作区,然后构建并启动:
12+
13+
```sh
14+
sudo install -m 0644 build/deepseek-harness /etc/apparmor.d/deepseek-harness
15+
sudo apparmor_parser -r /etc/apparmor.d/deepseek-harness
16+
mkdir -p workspace
17+
SSH_PUB_KEY="$(cat ~/.ssh/id_ed25519.pub)" docker compose up -d --build
18+
```
19+
20+
After initialization, ordinary starts need no key variable:
21+
22+
初始化完成后,普通启动无需再次提供公钥变量:
23+
24+
```sh
25+
docker compose up -d
26+
```
27+
28+
## Start a Published Image / 启动已发布镜像
29+
30+
Replace the example `DSH_IMAGE` value with the registry image name. The AppArmor profile must already be installed from this repository.
31+
32+
将示例中的 `DSH_IMAGE` 值替换为镜像仓库中的实际名称;宿主机必须已经从本仓库安装 AppArmor 配置。
33+
34+
```sh
35+
export DSH_IMAGE=ghcr.io/owner/image:tag
36+
docker pull "${DSH_IMAGE}"
37+
mkdir -p workspace
38+
docker volume create dsh-data
39+
docker run -d \
40+
--name deepseek-harness \
41+
--hostname dsh-dev \
42+
--restart unless-stopped \
43+
--pids-limit 1024 \
44+
--userns=host \
45+
--security-opt apparmor=deepseek-harness \
46+
--security-opt seccomp=unconfined \
47+
-p 127.0.0.1:3080:3081 \
48+
-p 127.0.0.1:2222:2222 \
49+
--mount source=dsh-data,target=/data/dsh \
50+
--mount type=bind,source="$(pwd)/workspace",target=/home/developer/workspace \
51+
-e SSH_PUB_KEY="$(cat ~/.ssh/id_ed25519.pub)" \
52+
"${DSH_IMAGE}"
53+
```
54+
55+
Open `http://127.0.0.1:3080` or connect with:
56+
57+
访问 `http://127.0.0.1:3080`,或通过 SSH 连接:
58+
59+
```sh
60+
ssh -i ~/.ssh/id_ed25519 -p 2222 developer@127.0.0.1
61+
```
62+
63+
To rotate the key with Compose, provide a new value while recreating the container:
64+
65+
如需通过 Compose 轮换公钥,在重建容器时提供新值:
66+
67+
```sh
68+
SSH_PUB_KEY="$(cat ~/.ssh/new_key.pub)" docker compose up -d --force-recreate
69+
```
70+
71+
Deleting `dsh-data` also deletes the stored SSH key. The next start must provide `SSH_PUB_KEY` again. A missing or invalid first-run key causes the container to stop with an explanatory log message.
72+
73+
删除 `dsh-data` 也会删除已保存的 SSH 公钥。下次启动必须重新提供 `SSH_PUB_KEY`;首次运行时缺少公钥或格式无效会使容器停止,并在日志中给出原因。

0 commit comments

Comments
 (0)