Skip to content

Commit 45265e7

Browse files
committed
Switch buildx remote driver from TCP to docker-container
Replace tcp://localhost:1234 endpoint with docker-container://buildcage. This eliminates the need for TCP port exposure and simplifies the connection between buildx and the builder container.
1 parent 56cf858 commit 45265e7

File tree

11 files changed

+36
-39
lines changed

11 files changed

+36
-39
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ run_audit_mode: ## Start in audit mode
2222
@echo "Creating buildx builder..."
2323
@docker buildx create --bootstrap \
2424
--name buildcage \
25-
--driver remote tcp://localhost:1234
25+
--driver remote docker-container://buildcage
2626

2727
.PHONY: run_restrict_mode
2828
run_restrict_mode: ## Start in restrict mode
@@ -36,7 +36,7 @@ run_restrict_mode: ## Start in restrict mode
3636
@echo "Creating buildx builder..."
3737
@docker buildx create --bootstrap \
3838
--name buildcage \
39-
--driver remote tcp://localhost:1234
39+
--driver remote docker-container://buildcage
4040

4141
.PHONY: test_restrict_mode
4242
test_restrict_mode: ## Run restrict mode tests

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ jobs:
9999
uses: docker/setup-buildx-action@v3
100100
with:
101101
driver: remote
102-
endpoint: tcp://localhost:${{ steps.buildcage.outputs.port }}
102+
endpoint: docker-container://buildcage
103103

104104
- name: Build and discover dependencies
105105
uses: docker/build-push-action@v6
@@ -150,7 +150,7 @@ jobs:
150150
uses: docker/setup-buildx-action@v3
151151
with:
152152
driver: remote
153-
endpoint: tcp://localhost:${{ steps.buildcage.outputs.port }}
153+
endpoint: docker-container://buildcage
154154
155155
- name: Build with protection
156156
uses: docker/build-push-action@v6
@@ -189,11 +189,11 @@ Starts the Buildcage builder container.
189189
|-----------|----------|---------|-------------|
190190
| `buildcage_image` | No | `ghcr.io/<owner>/<repo>` | Docker image name |
191191
| `buildcage_version` | No | `1` | Image tag |
192+
| `builder_name` | No | `buildcage` | Name of the builder container |
192193
| `proxy_mode` | No | `restrict` | Operation mode (`audit` / `restrict`) |
193194
| `allowed_https_rules` | No | empty | HTTPS allow rules (wildcard or regex, port required) |
194195
| `allowed_http_rules` | No | empty | HTTP allow rules (wildcard or regex, port required) |
195196
| `allowed_ip_rules` | No | empty | IP address allow rules (wildcard or regex, port required) |
196-
| `port` | No | `1234` | BuildKit endpoint port on localhost |
197197

198198
**Rule syntax**
199199

@@ -210,20 +210,16 @@ IP address rules (e.g., `192.168.1.1:443`) use the same syntax but go in `allowe
210210

211211
For detailed syntax, see [Rule Syntax](./docs/rules.md).
212212

213-
#### Outputs
213+
#### Connecting Buildx
214214

215-
| Name | Description |
216-
|------|-------------|
217-
| `port` | BuildKit endpoint port |
218-
219-
Pass this port to [`docker/setup-buildx-action`](https://github.com/docker/setup-buildx-action) to use Buildcage as a remote builder:
215+
Pass the container name to [`docker/setup-buildx-action`](https://github.com/docker/setup-buildx-action) to use Buildcage as a remote builder. The `endpoint` must match the `builder_name` parameter (default: `buildcage`):
220216

221217
```yaml
222218
- name: Set up Docker Buildx
223219
uses: docker/setup-buildx-action@v3
224220
with:
225221
driver: remote
226-
endpoint: tcp://localhost:${{ steps.buildcage.outputs.port }}
222+
endpoint: docker-container://buildcage
227223
```
228224

229225
#### Operation Modes
@@ -286,6 +282,7 @@ In restrict mode, the report step fails if blocked connections are detected, cau
286282

287283
| Parameter | Required | Default | Description |
288284
|-----------|----------|---------|-------------|
285+
| `builder_name` | No | `buildcage` | Name of the builder container |
289286
| `fail_on_blocked` | No | `true` | Fail the step if blocked connections are detected (restrict mode only; ignored in audit mode) |
290287

291288
---

compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
services:
22
builder:
3+
container_name: ${BUILDER_NAME:-buildcage}
34
build:
45
context: docker
56
dockerfile: Dockerfile
@@ -25,8 +26,6 @@ services:
2526
cgroup: host
2627
volumes:
2728
- /sys/fs/cgroup:/sys/fs/cgroup:rw
28-
ports:
29-
- "${PORT:-1234}:1234"
3029
environment:
3130
- PROXY_MODE=${PROXY_MODE:-restrict}
3231
- ALLOWED_HTTPS_RULES=${ALLOWED_HTTPS_RULES:-}
@@ -35,7 +34,7 @@ services:
3534
- EXTERNAL_RESOLVER=${EXTERNAL_RESOLVER:-1.1.1.1,8.8.8.8}
3635
restart: unless-stopped
3736
healthcheck:
38-
test: ["CMD", "sh", "-c", "curl -sf --unix-socket /var/run/haproxy-health.sock http://localhost/health && nc -z 127.0.0.1 1234"]
37+
test: ["CMD", "sh", "-c", "curl -sf --unix-socket /var/run/haproxy-health.sock http://localhost/health"]
3938
interval: 30s
4039
timeout: 5s
4140
retries: 10

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ RUN chmod +x /etc/s6-overlay/scripts/* && \
7474
chmod +x /etc/s6-overlay/s6-rc.d/haproxy-log/run
7575

7676
ENTRYPOINT ["/init"]
77-
CMD ["buildkitd", "--oci-worker-net=cni", "--addr", "tcp://0.0.0.0:1234"]
77+
CMD ["buildkitd", "--oci-worker-net=cni"]

docker/files/s6-scripts/init-iptables

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,3 @@ iptables -t nat -A PREROUTING -i buildkit0 -p tcp \
99
iptables -A FORWARD -i buildkit0 -j DROP
1010
ip6tables -A FORWARD -i buildkit0 -j DROP
1111
echo "init-iptables: REDIRECT configured, FORWARD from buildkit0 blocked (IPv4/IPv6)"
12-
13-
iptables -A INPUT -i buildkit0 -p tcp --dport 1234 -j DROP
14-
ip6tables -A INPUT -i buildkit0 -p tcp --dport 1234 -j DROP
15-
echo "init-iptables: INPUT to buildkitd API from buildkit0 blocked (IPv4/IPv6)"

report/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ name: Report
22
description: Show proxy communication logs and fail if blocked connections detected
33

44
inputs:
5+
builder_name:
6+
description: "Name of the builder container"
7+
required: false
8+
default: 'buildcage'
59
fail_on_blocked:
610
description: "Fail the step if blocked connections are detected"
711
required: false

report/main.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
88

99
// 1. Get structured report from container via QuickJS
1010
const composeFile = process.argv[2] || join(__dirname, "..", "setup", "compose.yml");
11+
const composeEnv = {
12+
...process.env,
13+
BUILDER_NAME: process.env.INPUT_BUILDER_NAME || "buildcage",
14+
};
1115

1216
let jsonOutput;
1317
try {
1418
jsonOutput = execFileSync(
1519
"docker",
1620
["compose", "-f", composeFile, "exec", "builder", "qjs", "/opt/buildcage/tools/report.mjs"],
17-
{ encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }
21+
{ encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env: composeEnv }
1822
);
1923
} catch (e) {
2024
console.log("Failed to get report from container:", e.message);
@@ -29,7 +33,7 @@ try {
2933
const rawLog = execFileSync(
3034
"docker",
3135
["compose", "-f", composeFile, "exec", "builder", "cat", "/var/log/haproxy/current"],
32-
{ encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }
36+
{ encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env: composeEnv }
3337
);
3438
process.stdout.write(rawLog);
3539
} catch {

setup/action.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ inputs:
88
buildcage_version:
99
description: "Image tag"
1010
required: false
11+
builder_name:
12+
description: "Name of the builder container"
13+
required: false
14+
default: 'buildcage'
1115
proxy_mode:
1216
description: "audit or restrict"
1317
required: false
@@ -41,14 +45,6 @@ inputs:
4145
description: "Deprecated: specify port per rule in allowed_https_rules (e.g., 'example.com:8443')"
4246
required: false
4347
default: '443'
44-
port:
45-
description: "BuildKit endpoint port on localhost"
46-
required: false
47-
default: '1234'
48-
49-
outputs:
50-
port:
51-
description: "BuildKit endpoint port"
5248

5349
runs:
5450
using: node24

setup/compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
services:
22
builder:
3+
container_name: ${BUILDER_NAME:-buildcage}
34
image: ${BUILDCAGE_IMAGE}:${BUILDCAGE_VERSION:-1}
45
# Instead of privileged: true, grant only the minimum privileges required
56
# to run BuildKit and iptables. This avoids granting full device access
@@ -23,8 +24,6 @@ services:
2324
cgroup: host
2425
volumes:
2526
- /sys/fs/cgroup:/sys/fs/cgroup:rw
26-
ports:
27-
- "${PORT:-1234}:1234"
2827
environment:
2928
- PROXY_MODE=${PROXY_MODE:-restrict}
3029
- ALLOWED_HTTPS_RULES=${ALLOWED_HTTPS_RULES:-}
@@ -33,7 +32,7 @@ services:
3332
- EXTERNAL_RESOLVER=${EXTERNAL_RESOLVER:-1.1.1.1,8.8.8.8}
3433
restart: unless-stopped
3534
healthcheck:
36-
test: ["CMD", "sh", "-c", "curl -sf --unix-socket /var/run/haproxy-health.sock http://localhost/health && nc -z 127.0.0.1 1234"]
35+
test: ["CMD", "sh", "-c", "curl -sf --unix-socket /var/run/haproxy-health.sock http://localhost/health"]
3736
interval: 30s
3837
timeout: 5s
3938
retries: 10

setup/main.mjs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { execFileSync } from "node:child_process";
2-
import { appendFileSync } from "node:fs";
32
import { join, dirname } from "node:path";
43
import { fileURLToPath } from "node:url";
54
import { buildLegacyRules } from "./lib/legacy-rules.mjs";
@@ -43,13 +42,13 @@ function main() {
4342

4443
const composeEnv = {
4544
...env,
45+
BUILDER_NAME: env.INPUT_BUILDER_NAME || "buildcage",
4646
PROXY_MODE: env.INPUT_PROXY_MODE || "restrict",
4747
ALLOWED_HTTPS_RULES: rules.httpsRules.join('\n'),
4848
ALLOWED_HTTP_RULES: rules.httpRules.join('\n'),
4949
ALLOWED_IP_RULES: rules.ipRules.join('\n'),
5050
BUILDCAGE_IMAGE: image.repository,
5151
BUILDCAGE_VERSION: image.tag,
52-
PORT: env.INPUT_PORT || "1234",
5352
};
5453

5554
execFileSync(
@@ -68,9 +67,6 @@ function main() {
6867
{ stdio: "inherit", env: composeEnv }
6968
);
7069

71-
// Set action output
72-
const port = env.INPUT_PORT || "1234";
73-
appendFileSync(env.GITHUB_OUTPUT, `port=${port}\n`);
7470
}
7571

7672
/**

0 commit comments

Comments
 (0)