Recent Discussions
Docker attach websocket in typescrpt
Unanswered
West African Lion posted this in #questions
12 messages
0 views
West African LionOP
Anyone have code or know where I can find code that hosts a websocket (socket io, ws, uwebsocket, I literally do not care) in typescript/javascript that allows you to connect to a docker container with
docker attach (with dockerode, or something similar) and sends the ws client the logs from the container and forwards the client messages to the container?West African LionOP
I have spent hours on this, and I simply cannot get it to work with dockerode
@West African Lion I have spent hours on this, and I simply cannot get it to work with dockerode
Kromfohrländer
what are you tryna do? Get STDOUT or smth?
import { WebSocketServer } from 'ws';
import Docker from 'dockerode';
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', (ws, req) => {
const containerId = new URL(req.url!, 'ws://localhost').searchParams.get('containerId');
if (!containerId) return ws.close();
const container = docker.getContainer(containerId);
container.attach({ stream: true, stdin: true, stdout: true, stderr: true }, (err, stream) => {
if (err) return ws.close();
// Container output to WS
stream.on('data', (chunk: Buffer) => ws.send(chunk.toString()));
// WS input to Container stdin
ws.on('message', (data: Buffer) => stream.write(data));
ws.on('close', () => stream.end());
stream.on('end', () => ws.close());
});
});Then connect with
ws://localhost:8080?containerId=YOUR_CONTAINER_IDthis should be good enough as a snippet no?
ik theres a way to do it with express
but thats cringe
meaning the client can send commands to the ws and it will execute on the server
and the ws client receives all the docker logs
Kromfohrländer
Ohh nvm
West African LionOP
i got it to work with logs and docker exec but that’s not optimal
Loading...
Loading...