Skip to content

Commit 4d1886b

Browse files
authored
Merge pull request #5554 from github/brunoborges-container-image-run-failure
Add opt-in connection token generation to the CLI container
2 parents c41b148 + 70c29ee commit 4d1886b

5 files changed

Lines changed: 242 additions & 68 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,21 @@ By default, `copilot` utilizes Claude Sonnet 4.5. Run the `/model` slash command
125125

126126
The Copilot CLI is also available as a container image that runs the CLI as a headless server for Copilot SDK clients. Images are published to `ghcr.io/github/copilot-cli` for `linux/amd64` and `linux/arm64`. This image is for connecting SDK clients to a server, not for interactive terminal use.
127127

128-
Set a connection token and a GitHub token, then start the server:
128+
Client apps use `COPILOT_CONNECTION_TOKEN` to connect to the Copilot server. Sessions use a separate GitHub token to access GitHub Copilot. To start the server, generate a random connection token:
129129

130130
```bash
131131
export COPILOT_CONNECTION_TOKEN="$(openssl rand -hex 32)"
132132
docker run --rm \
133133
-p 127.0.0.1:4321:4321 \
134134
-e COPILOT_CONNECTION_TOKEN \
135-
-e COPILOT_GITHUB_TOKEN \
136135
ghcr.io/github/copilot-cli:latest
137136
```
138137

139-
Set `COPILOT_GITHUB_TOKEN` in your environment before running the command. For version-pinned deployments, use a release tag instead of `latest`. See the [container image guide](./container/README.md) for SDK connection examples, configuration, security considerations, and build instructions.
138+
On Windows, use the [PowerShell example](./container/README.md#quick-start).
139+
140+
For a local trial, set `COPILOT_CONNECTION_TOKEN_GENERATE=true` to print a generated connection token at startup. On [Kubernetes](./container/README.md#running-on-kubernetes), set `COPILOT_CONNECTION_TOKEN` from a Secret.
141+
142+
For GitHub authentication, your backend can pass each user's GitHub token when it creates or resumes an SDK session. To use one default identity instead, export `COPILOT_GITHUB_TOKEN` and add `-e COPILOT_GITHUB_TOKEN` before the image name. See the [container image guide](./container/README.md) for SDK examples, multi-user deployment, and release tags.
140143

141144
### Experimental Mode
142145

‎container/Dockerfile‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@
99
# COPILOT_VERSION is a release version without the leading "v" (for example
1010
# 1.0.105, or 1.0.106-0 for a prerelease). The image contains the checksum-verified
1111
# `copilot` binary attached to that GitHub release.
12-
FROM debian:trixie-slim@sha256:a72b4433de1cf7d6bae0c4cee74d0547f58601a643b3375f070c51600d63b6b2
12+
FROM debian:trixie-slim@sha256:a72b4433de1cf7d6bae0c4cee74d0547f58601a643b3375f070c51600d63b6b2 AS base
1313

14-
ARG TARGETARCH
15-
ARG COPILOT_VERSION
16-
17-
LABEL org.opencontainers.image.title="GitHub Copilot CLI (server mode)" \
18-
org.opencontainers.image.description="GitHub Copilot CLI running as a headless server that Copilot SDK clients connect to over TCP" \
19-
org.opencontainers.image.source="https://github.com/github/copilot-cli" \
20-
org.opencontainers.image.documentation="https://github.com/github/copilot-cli/blob/main/container/README.md" \
21-
org.opencontainers.image.version="${COPILOT_VERSION}"
14+
# ca-certificates: HTTPS trust roots. openssl: connection token generation.
15+
RUN apt-get update \
16+
&& apt-get install -y --no-install-recommends ca-certificates openssl \
17+
&& rm -rf /var/lib/apt/lists/*
2218

23-
# ca-certificates: TLS to GitHub and the model providers. curl and git: commonly used by agent tools.
19+
FROM base AS download
2420
RUN apt-get update \
25-
&& apt-get install -y --no-install-recommends ca-certificates curl git \
21+
&& apt-get install -y --no-install-recommends curl \
2622
&& rm -rf /var/lib/apt/lists/*
2723

24+
ARG TARGETARCH
25+
ARG COPILOT_VERSION
2826
RUN set -eu; \
2927
: "${COPILOT_VERSION:?Pass --build-arg COPILOT_VERSION=<release version>}"; \
3028
case "${TARGETARCH}" in \
@@ -44,9 +42,17 @@ RUN set -eu; \
4442
chmod 0755 /usr/local/bin/copilot; \
4543
rm -rf "${workdir}"
4644

45+
FROM base AS runtime
46+
ARG COPILOT_VERSION
47+
LABEL org.opencontainers.image.title="GitHub Copilot CLI (server mode)" \
48+
org.opencontainers.image.description="GitHub Copilot CLI running as a headless server that Copilot SDK clients connect to over TCP" \
49+
org.opencontainers.image.source="https://github.com/github/copilot-cli" \
50+
org.opencontainers.image.documentation="https://github.com/github/copilot-cli/blob/main/container/README.md" \
51+
org.opencontainers.image.version="${COPILOT_VERSION}"
52+
4753
RUN useradd --create-home --uid 10001 copilot \
48-
&& install -d -o copilot -g copilot /home/copilot/workspace /home/copilot/.copilot \
49-
&& git config --system --add safe.directory /home/copilot/workspace
54+
&& install -d -o copilot -g copilot /home/copilot/workspace /home/copilot/.copilot
55+
COPY --from=download /usr/local/bin/copilot /usr/local/bin/copilot
5056
COPY --chmod=0755 container/entrypoint.sh /usr/local/bin/copilot-server
5157
COPY LICENSE.md /usr/share/licenses/copilot-cli/LICENSE.md
5258

‎container/README.md‎

Lines changed: 116 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,38 @@ Images are available for `linux/amd64` and `linux/arm64`. Tags are mutable: runn
1818

1919
## Quick start
2020

21-
Start the server with a secret that clients must present, and a token that lets the CLI use Copilot (see [Configuration](#configuration)):
21+
Start the server with a random shared secret. SDK clients present this value as `connectionToken`; it controls who can connect, not which GitHub account a session uses. The entrypoint rejects values that look like GitHub tokens.
2222

2323
```sh
2424
export COPILOT_CONNECTION_TOKEN="$(openssl rand -hex 32)"
2525

2626
docker run --rm \
2727
-p 127.0.0.1:4321:4321 \
2828
-e COPILOT_CONNECTION_TOKEN \
29-
-e COPILOT_GITHUB_TOKEN \
3029
ghcr.io/github/copilot-cli:latest
3130
```
3231

33-
Then connect with a Copilot SDK client. For example, with the Node.js SDK (`npm install @github/copilot-sdk`):
32+
In PowerShell on Windows:
33+
34+
```powershell
35+
$bytes = [byte[]]::new(32)
36+
[Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
37+
$env:COPILOT_CONNECTION_TOKEN = -join ($bytes | ForEach-Object { $_.ToString("x2") })
38+
39+
docker run --rm `
40+
-p 127.0.0.1:4321:4321 `
41+
-e COPILOT_CONNECTION_TOKEN `
42+
ghcr.io/github/copilot-cli:latest
43+
```
44+
45+
The server waits for an SDK client. No GitHub credential is needed to connect or ping. For example, install the Node.js SDK (`npm install @microsoft/copilot-sdk`) and run this client with the same `COPILOT_CONNECTION_TOKEN` in its environment.
3446

3547
```js
36-
import { CopilotClient, RuntimeConnection } from "@github/copilot-sdk";
48+
import { CopilotClient, RuntimeConnection } from "@microsoft/copilot-sdk";
3749

3850
const client = new CopilotClient({
39-
connection: RuntimeConnection.forUri("localhost:4321", {
51+
mode: "empty",
52+
runtime: RuntimeConnection.forTcp("127.0.0.1", 4321, {
4053
connectionToken: process.env.COPILOT_CONNECTION_TOKEN,
4154
}),
4255
});
@@ -46,17 +59,44 @@ console.log(await client.ping("hello"));
4659
await client.stop();
4760
```
4861

49-
The other Copilot SDKs can connect to a running server in the same way. See the [SDK documentation](https://github.com/github/copilot-sdk).
62+
To use one GitHub account as the default for sessions, export `COPILOT_GITHUB_TOKEN` and add `-e COPILOT_GITHUB_TOKEN` before the image name. For a multi-user application, supply credentials per session instead.
63+
64+
### Generate a token for local use
65+
66+
With an image [built from this source](#building-locally), set `COPILOT_CONNECTION_TOKEN_GENERATE=true` to print a fresh connection token at startup:
67+
68+
```sh
69+
docker run --rm --name copilot-local -p 127.0.0.1:4321:4321 \
70+
-e COPILOT_CONNECTION_TOKEN_GENERATE=true copilot-cli:local
71+
```
72+
73+
Use the value after `Generated COPILOT_CONNECTION_TOKEN=` as the SDK's `connectionToken`. If running detached, read it with `docker logs copilot-local`. **Anyone who can read those logs can see the token**, and restarting the container generates a different one. Use a managed secret for shared deployments. `COPILOT_CONNECTION_TOKEN_GENERATE` must be `true` or `false`, and cannot be enabled when `COPILOT_CONNECTION_TOKEN` is set.
74+
75+
## Serving multiple users
76+
77+
Keep the port and connection secret private to your backend. The backend authenticates each user, records which sessions they own, and checks ownership before accessing, resuming, or deleting a session. A connection token admits a client to the server; it does not authorize access to an individual session.
78+
79+
Leave `COPILOT_GITHUB_TOKEN`, `GH_TOKEN`, and `GITHUB_TOKEN` unset on the server. In your backend, use `mode: "empty"` as above and pass the authenticated user's GitHub token when creating or resuming their session. For example, on a connected client:
80+
81+
```js
82+
const session = await client.createSession({
83+
github: { token: user.githubToken }, // From your backend's authentication flow
84+
tools: { availableTools: [] },
85+
});
86+
```
87+
88+
Only enable tools that enforce the user's permissions. Sessions have separate histories and can have separate GitHub identities, but their tools run as the same OS user. If mutually untrusted tenants need shell or filesystem access, give each tenant a separate container with its own workspace, `COPILOT_HOME`, credentials, and connection secret. See the [SDK multi-tenancy guide](https://docs.github.com/en/copilot/how-tos/copilot-sdk/setup/multi-tenancy).
5089

5190
## Configuration
5291

5392
Configure the server with environment variables. Arguments after the image name are appended to the `copilot` command line, for example `--log-level debug`.
5493

5594
| Variable | Default | Description |
5695
| --- | --- | --- |
57-
| `COPILOT_CONNECTION_TOKEN` | required | Secret that clients must present to connect. The container refuses to start without it. |
96+
| `COPILOT_CONNECTION_TOKEN` | unset | Required. Shared secret that SDK clients must present to connect. |
97+
| `COPILOT_CONNECTION_TOKEN_GENERATE` | `false` | Set to `true` to generate and print a fresh token at startup for [local use](#generate-a-token-for-local-use). Cannot be combined with `COPILOT_CONNECTION_TOKEN`. |
5898
| `COPILOT_PORT` | `4321` | Port the server listens on inside the container, from 1024 to 65535. |
59-
| `COPILOT_GITHUB_TOKEN`, `GH_TOKEN`, `GITHUB_TOKEN` | | Token the CLI uses to authenticate with GitHub. The first one that is set wins. A [fine-grained personal access token with the "Copilot Requests" permission](../README.md#launching-the-cli) works. |
99+
| `COPILOT_GITHUB_TOKEN`, `GH_TOKEN`, `GITHUB_TOKEN` | unset | Optional server-wide GitHub identity for sessions without their own credential. The first variable set wins. A [fine-grained personal access token with the "Copilot Requests" permission](../README.md#launching-the-cli) works. |
60100
| `COPILOT_HOME` | `/home/copilot/.copilot` | Where the CLI stores its configuration and state. |
61101

62102
The image is pinned to one CLI version, so auto-update is off (`COPILOT_AUTO_UPDATE=false`). Any other variable listed by `copilot help environment` also works, such as the `COPILOT_PROVIDER_*` variables for using your own model provider.
@@ -66,24 +106,87 @@ Paths in the image:
66106
- `/home/copilot/workspace` is the working directory. Mount a project here to let the agent work on it.
67107
- `/home/copilot/.copilot` holds configuration and state. Mount a volume here to keep them across containers.
68108

69-
The container runs as the non-root user `copilot` (uid 10001), so bind mounts must be writable by that user. Git is configured to trust only `/home/copilot/workspace`, allowing Git operations on a mounted repository even when its host UID differs. This is an exact-path exception, not a wildcard; mount repositories at the workspace path.
109+
The container runs as the non-root user `copilot` (uid 10001), so any mounted workspace or state directory it writes to must be writable by that user.
70110

71111
To run another `copilot` command instead of the server, override the entrypoint:
72112

73113
```sh
74114
docker run --rm --entrypoint copilot ghcr.io/github/copilot-cli:latest --version
75115
```
76116

117+
## Running on Kubernetes
118+
119+
Create the connection secret as a Kubernetes Secret. `--from-literal` stores it without a trailing newline:
120+
121+
```sh
122+
kubectl create secret generic copilot-connection \
123+
--from-literal=token="$(openssl rand -hex 32)"
124+
```
125+
126+
Set `COPILOT_CONNECTION_TOKEN` from the Secret and expose the server inside the cluster:
127+
128+
```yaml
129+
apiVersion: apps/v1
130+
kind: Deployment
131+
metadata:
132+
name: copilot
133+
spec:
134+
replicas: 1
135+
selector:
136+
matchLabels:
137+
app: copilot
138+
template:
139+
metadata:
140+
labels:
141+
app: copilot
142+
spec:
143+
containers:
144+
- name: copilot-server
145+
image: ghcr.io/github/copilot-cli:latest
146+
env:
147+
- name: COPILOT_CONNECTION_TOKEN
148+
valueFrom:
149+
secretKeyRef:
150+
name: copilot-connection
151+
key: token
152+
ports:
153+
- containerPort: 4321
154+
readinessProbe:
155+
tcpSocket:
156+
port: 4321
157+
---
158+
apiVersion: v1
159+
kind: Service
160+
metadata:
161+
name: copilot-server
162+
spec:
163+
selector:
164+
app: copilot
165+
ports:
166+
- port: 4321
167+
```
168+
169+
SDK clients in the same namespace connect to `copilot-server:4321` with the same token.
170+
171+
- **Service links.** The `copilot-server` Service generates `COPILOT_SERVER_PORT` rather than overriding the container's `COPILOT_PORT` default.
172+
- **Rotation.** After updating the Secret, run `kubectl rollout restart deployment/copilot` so new pods pick up the token, and update clients to use the same value.
173+
- **Replicas.** Each pod keeps its own sessions under `/home/copilot/.copilot`. Run one replica per server, or route each user's sessions to the same pod. Mount a persistent volume there to keep sessions across restarts.
174+
- **Network access.** Keep the Service `ClusterIP` and limit which pods can reach it, for example with a NetworkPolicy.
175+
77176
## Security
78177

79178
- The server listens on all interfaces inside the container, and anyone who can reach the port and knows the token can run agent tools in the container. Publish the port on loopback (`-p 127.0.0.1:4321:4321`) or on an isolated container network, and use a long random token.
80-
- The connection is plain TCP without TLS, so the token is sent unencrypted. Keep the traffic on a trusted network, or terminate TLS in front of the container.
179+
- The connection is plain TCP without TLS. Both the connection secret and any GitHub credentials sent by the SDK travel unencrypted. Keep the traffic on a trusted network, or protect it with TLS.
81180
- Agent tools run inside the container with the permissions of the `copilot` user. Treat the container as the sandbox and give it only the files and credentials the agent needs.
82181

83182
## Waiting for the server
84183

85184
A published port can accept TCP connections before the server is ready to answer, for example behind Docker Desktop's port proxy. When you wait for the container to start, for instance with Testcontainers, check with a real client call such as `ping` instead of only checking that the port is open.
86185

186+
## Troubleshooting on Apple Silicon
187+
188+
If the server logs `qemu: uncaught target signal 11`, check whether `DOCKER_DEFAULT_PLATFORM` or a `--platform` flag forces `linux/amd64`. The image includes a native ARM64 build. Remove the override or pass `--platform linux/arm64` to `docker run` before the image name.
189+
87190
## Verifying an image
88191

89192
Images are published with a build provenance attestation:
@@ -94,11 +197,11 @@ gh attestation verify oci://ghcr.io/github/copilot-cli:1.0.105 --repo github/cop
94197

95198
The attestation is stored in the registry next to the image, so the package's tag list also shows a `sha256-<digest>` tag. That tag holds the attestation, not an image to run.
96199

97-
The image contains the `copilot` binary from the matching GitHub release, which the build verifies against the release's `SHA256SUMS.txt`. The license is at `/usr/share/licenses/copilot-cli/LICENSE.md`.
200+
The build uses curl to download the CLI release archive and verifies it against the release's `SHA256SUMS.txt`. Only the extracted `copilot` binary is copied from the download stage into the runtime image; curl is not included. The license is at `/usr/share/licenses/copilot-cli/LICENSE.md`.
98201

99202
## Building locally
100203

101-
The smoke test requires a Unix environment (Linux or macOS), Python 3, and Docker configured to run Linux containers. Native Windows Python is not supported because the bind-mounted Git check uses Unix UID/GID APIs (`os.getuid()` and `os.getgid()`). On Windows, run the commands below from a Linux environment such as WSL 2.
204+
The smoke test requires Python 3 and Docker configured to run Linux containers.
102205

103206
Build from the repository root with the release version, without the leading `v`:
104207

@@ -107,7 +210,7 @@ docker build -f container/Dockerfile --build-arg COPILOT_VERSION=1.0.105 -t copi
107210
python3 container/smoke_test.py copilot-cli:local --version 1.0.105
108211
```
109212

110-
The smoke test checks the container's configuration, Git access to a bind-mounted repository owned by the host user, and a real client handshake with the running server. Add `--platform linux/arm64` or `--platform linux/amd64` to test a build for another architecture.
213+
The smoke test checks the container's configuration and a real client handshake with the running server. Add `--platform linux/arm64` or `--platform linux/amd64` to test a build for another architecture.
111214

112215
## Publishing
113216

‎container/entrypoint.sh‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,39 @@
33
# Any arguments are appended to the `copilot` command line.
44
set -eu
55

6+
case "${COPILOT_CONNECTION_TOKEN_GENERATE:-false}" in
7+
true)
8+
if [ -n "${COPILOT_CONNECTION_TOKEN:-}" ]; then
9+
echo "COPILOT_CONNECTION_TOKEN_GENERATE=true cannot be used with COPILOT_CONNECTION_TOKEN" >&2
10+
exit 1
11+
fi
12+
COPILOT_CONNECTION_TOKEN=$(openssl rand -hex 32) || exit 1
13+
export COPILOT_CONNECTION_TOKEN
14+
printf 'Generated COPILOT_CONNECTION_TOKEN=%s\n' "$COPILOT_CONNECTION_TOKEN" >&2 ;;
15+
false|'') ;;
16+
*)
17+
echo "COPILOT_CONNECTION_TOKEN_GENERATE must be true or false" >&2
18+
exit 1 ;;
19+
esac
20+
621
# The server listens on all interfaces, so never start it without a secret.
722
if [ -z "${COPILOT_CONNECTION_TOKEN:-}" ]; then
8-
echo "COPILOT_CONNECTION_TOKEN must be set to a non-empty secret that clients present to connect" >&2
23+
echo "Set COPILOT_CONNECTION_TOKEN to a shared secret, or set COPILOT_CONNECTION_TOKEN_GENERATE=true for local use." >&2
24+
echo 'To generate a secret on the host: export COPILOT_CONNECTION_TOKEN="$(openssl rand -hex 32)"' >&2
925
exit 1
1026
fi
1127

28+
case "$COPILOT_CONNECTION_TOKEN" in
29+
gho_*|ghp_*|ghu_*|ghs_*|ghr_*|github_pat_*)
30+
echo "COPILOT_CONNECTION_TOKEN looks like a GitHub token. Use a random shared secret for SDK connections instead." >&2
31+
exit 1 ;;
32+
esac
33+
1234
case "${COPILOT_PORT:-}" in
35+
tcp://*)
36+
# Kubernetes injects <SERVICE>_PORT=tcp://... for a Service named "copilot".
37+
echo "COPILOT_PORT was replaced by a Kubernetes Service link. Set enableServiceLinks: false in the pod spec, or set COPILOT_PORT explicitly." >&2
38+
exit 1 ;;
1339
''|*[!0-9]*|??????*) echo "COPILOT_PORT must be an integer between 1024 and 65535" >&2; exit 1 ;;
1440
esac
1541
if [ "$COPILOT_PORT" -lt 1024 ] || [ "$COPILOT_PORT" -gt 65535 ]; then

0 commit comments

Comments
 (0)