|
| 1 | +# Docker |
| 2 | + |
| 3 | +httpjail can run as a standalone proxy server in a Docker container, perfect for team-wide policy enforcement or testing. An example Dockerfile is provided in the [`examples/`](https://github.com/coder/httpjail/tree/main/examples) directory. |
| 4 | + |
| 5 | +## Building the Image |
| 6 | + |
| 7 | +The example Dockerfile downloads httpjail from GitHub releases and runs as a non-root user (UID 1000). Multi-arch builds are supported for `linux/amd64` and `linux/arm64`. |
| 8 | + |
| 9 | +**Build for your current platform:** |
| 10 | + |
| 11 | +```bash |
| 12 | +cd examples/ |
| 13 | +docker build -t httpjail:latest . |
| 14 | +``` |
| 15 | + |
| 16 | +**Build for a specific platform:** |
| 17 | + |
| 18 | +```bash |
| 19 | +# For amd64 (x86_64) |
| 20 | +docker build --platform linux/amd64 -t httpjail:amd64 . |
| 21 | + |
| 22 | +# For arm64 (aarch64) |
| 23 | +docker build --platform linux/arm64 -t httpjail:arm64 . |
| 24 | +``` |
| 25 | + |
| 26 | +**Build and push multi-arch image to a registry:** |
| 27 | + |
| 28 | +```bash |
| 29 | +# Create and use a new buildx builder (one-time setup) |
| 30 | +docker buildx create --name multiarch --use |
| 31 | + |
| 32 | +# Build and push for both architectures |
| 33 | +docker buildx build --platform linux/amd64,linux/arm64 \ |
| 34 | + -t your-registry/httpjail:latest \ |
| 35 | + --push . |
| 36 | + |
| 37 | +# Or build and load locally (single platform only) |
| 38 | +docker buildx build --platform linux/amd64 \ |
| 39 | + -t httpjail:latest \ |
| 40 | + --load . |
| 41 | +``` |
| 42 | + |
| 43 | +> **Note:** Multi-arch builds require [Docker Buildx](https://docs.docker.com/build/buildx/). The `--load` flag only works with single-platform builds; use `--push` for multi-platform images. |
| 44 | +
|
| 45 | +## Running the Container |
| 46 | + |
| 47 | +**Basic usage with default allow-all rule:** |
| 48 | + |
| 49 | +```bash |
| 50 | +docker run -d --name httpjail \ |
| 51 | + -p 8080:8080 -p 8443:8443 \ |
| 52 | + httpjail:latest |
| 53 | +``` |
| 54 | + |
| 55 | +**With persistent certificates:** |
| 56 | + |
| 57 | +```bash |
| 58 | +mkdir -p ./httpjail-certs |
| 59 | +docker run -d --name httpjail \ |
| 60 | + -p 8080:8080 -p 8443:8443 \ |
| 61 | + -v ./httpjail-certs:/home/httpjail/.config/httpjail \ |
| 62 | + httpjail:latest |
| 63 | +``` |
| 64 | + |
| 65 | +**With custom rules:** |
| 66 | + |
| 67 | +```bash |
| 68 | +# Create your custom rule file |
| 69 | +cat > my-rules.js <<'EOF' |
| 70 | +// Allow only specific domains |
| 71 | +const allowed = ['github.com', 'api.github.com', 'npmjs.org']; |
| 72 | +allowed.includes(r.host) |
| 73 | +EOF |
| 74 | + |
| 75 | +# Run with custom rules (overrides default rules.js) |
| 76 | +docker run -d --name httpjail \ |
| 77 | + -p 8080:8080 -p 8443:8443 \ |
| 78 | + -v ./httpjail-certs:/home/httpjail/.config/httpjail \ |
| 79 | + -v ./my-rules.js:/rules/rules.js:ro \ |
| 80 | + httpjail:latest |
| 81 | +``` |
| 82 | + |
| 83 | +**With additional verbosity:** |
| 84 | + |
| 85 | +```bash |
| 86 | +docker run -d --name httpjail \ |
| 87 | + -p 8080:8080 -p 8443:8443 \ |
| 88 | + httpjail:latest --server --js-file /rules/rules.js -vv --request-log /dev/stderr |
| 89 | +``` |
| 90 | + |
| 91 | +## Configuring Clients |
| 92 | + |
| 93 | +After starting the container, configure your applications to use the proxy: |
| 94 | + |
| 95 | +```bash |
| 96 | +export HTTP_PROXY=http://localhost:8080 |
| 97 | +export HTTPS_PROXY=http://localhost:8443 |
| 98 | +``` |
| 99 | + |
| 100 | +For HTTPS to work, clients need to trust the CA certificate. Extract it from the container: |
| 101 | + |
| 102 | +```bash |
| 103 | +# Extract CA certificate |
| 104 | +docker cp httpjail:/home/httpjail/.config/httpjail/ca-cert.pem ./ca-cert.pem |
| 105 | + |
| 106 | +# Configure client |
| 107 | +export SSL_CERT_FILE=$PWD/ca-cert.pem |
| 108 | + |
| 109 | +# Test |
| 110 | +curl https://github.com |
| 111 | +``` |
| 112 | + |
| 113 | +Alternatively, install the certificate system-wide: |
| 114 | + |
| 115 | +```bash |
| 116 | +# Linux |
| 117 | +sudo cp ca-cert.pem /usr/local/share/ca-certificates/httpjail.crt |
| 118 | +sudo update-ca-certificates |
| 119 | + |
| 120 | +# macOS |
| 121 | +sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain ca-cert.pem |
| 122 | +``` |
| 123 | + |
| 124 | +## Viewing Logs |
| 125 | + |
| 126 | +Request logs are sent to stdout by default (visible in `docker logs`): |
| 127 | + |
| 128 | +```bash |
| 129 | +docker logs -f httpjail |
| 130 | +``` |
| 131 | + |
| 132 | +Log format: `<timestamp> <+/-> <METHOD> <URL>` where `+` means allowed and `-` means blocked. |
| 133 | + |
| 134 | +## JavaScript Rule Examples |
| 135 | + |
| 136 | +The default rule (`true`) allows all traffic. Here are more useful examples: |
| 137 | + |
| 138 | +**Allowlist specific domains:** |
| 139 | + |
| 140 | +```javascript |
| 141 | +const allowed = ['github.com', 'api.github.com', 'npmjs.org']; |
| 142 | +allowed.includes(r.host) |
| 143 | +``` |
| 144 | + |
| 145 | +**Block specific paths:** |
| 146 | + |
| 147 | +```javascript |
| 148 | +// Allow all except admin paths |
| 149 | +!r.path.startsWith('/admin') |
| 150 | +``` |
| 151 | + |
| 152 | +**Size limits:** |
| 153 | + |
| 154 | +```javascript |
| 155 | +// Allow GET requests under 10MB |
| 156 | +if (r.method === 'GET') { |
| 157 | + ({allow: {max_tx_bytes: 10 * 1024 * 1024}}) |
| 158 | +} else { |
| 159 | + false // Block non-GET |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +**Custom deny messages:** |
| 164 | + |
| 165 | +```javascript |
| 166 | +if (r.host === 'malicious.com') { |
| 167 | + ({allow: false, deny_message: 'Blocked: Known malicious domain'}) |
| 168 | +} else { |
| 169 | + true |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +**Complex policies:** |
| 174 | + |
| 175 | +```javascript |
| 176 | +// Allow GitHub and NPM GET requests, deny everything else |
| 177 | +const trustedDomains = ['github.com', 'api.github.com', 'npmjs.org', 'registry.npmjs.org']; |
| 178 | +const isTrusted = trustedDomains.includes(r.host); |
| 179 | +const isSafeMethod = ['GET', 'HEAD'].includes(r.method); |
| 180 | + |
| 181 | +isTrusted && isSafeMethod |
| 182 | +``` |
| 183 | + |
| 184 | +See the [JavaScript rule engine](../guide/rule-engines/javascript.md) documentation for complete reference. |
| 185 | + |
| 186 | +## Security Notes |
| 187 | + |
| 188 | +- The container runs as non-root user (UID 1000) |
| 189 | +- Server mode does NOT provide network isolation (no namespaces) |
| 190 | +- Applications must be configured to use the proxy (HTTP_PROXY/HTTPS_PROXY) |
| 191 | +- The Docker image supports both `linux/amd64` (x86_64) and `linux/arm64` (aarch64) architectures |
| 192 | +- Certificates are auto-generated on first run if not provided via volume mount |
0 commit comments