Fix Bare Docker access: HTTP unix socket support for dockerode
Release rolling / release (push) Has been cancelled

bare-http1 ignores socketPath and always dials TCP localhost:80, so
dockerode got HTTP 301→HTTPS and TLS errors. Patch the Bare http Agent
to connect via bare-net IPC when socketPath is set, matching Node.
This commit is contained in:
Raven Scott
2026-07-11 15:46:46 -04:00
parent a8ee0f19b7
commit 6bdb64b1fd
3 changed files with 95 additions and 7 deletions
+58
View File
@@ -0,0 +1,58 @@
/**
* Node-compatible http for Bare — adds Unix socket support.
*
* bare-http1's Agent always uses bare-tcp and ignores `socketPath`.
* dockerode/docker-modem requires:
* http.request({ socketPath: '/var/run/docker.sock', path: '/version', ... })
*
* Without this, requests go to TCP localhost:80 → reverse-proxy 301 to HTTPS
* → CERTIFICATE_VERIFY_FAILED (exactly the peardock-server bare failure mode).
*/
'use strict'
const http = require('bare-http1')
const net = require('bare-net')
function patchAgent(Agent) {
if (!Agent || Agent.prototype.__peardockSocketPathPatched) return Agent
const origCreate = Agent.prototype.createConnection
const origGetName = Agent.prototype.getName
Agent.prototype.createConnection = function createConnection(opts, callback) {
const o = opts || {}
const sock = o.socketPath || (typeof o.path === 'string' && o.path.startsWith('/') ? o.path : null)
if (sock) {
// bare-net: path → IPC/unix socket (not TCP)
const socket = net.createConnection({ path: sock })
if (typeof callback === 'function') socket.once('connect', () => callback(null, socket))
return socket
}
if (typeof origCreate === 'function') {
return origCreate.call(this, o, callback)
}
return net.createConnection(o, callback)
}
Agent.prototype.getName = function getName(opts) {
const o = opts || {}
if (o.socketPath) return `unix:${o.socketPath}:`
if (typeof origGetName === 'function') return origGetName.call(this, o)
return `${o.host || 'localhost'}:${o.port || 80}`
}
Agent.prototype.__peardockSocketPathPatched = true
return Agent
}
patchAgent(http.Agent)
if (http.globalAgent && http.globalAgent.constructor) {
patchAgent(http.globalAgent.constructor)
}
// Ensure global agent instance also has patched methods (prototype patch covers it)
module.exports = http
// Common Node re-exports
module.exports.default = http