below are the files I currenly working on:
const express = require('express');
const http = require('http');
const { Server } = require("socket.io");
const Docker = require('dockerode');
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static('public'));
io.on('connection', (socket) => {
console.log('A user connected');
docker.createContainer({
Image: 'ubuntu',
Cmd: ['/bin/bash'],
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: true,
OpenStdin: true
}).then(container => {
return container.start().then(() => {
return container.attach({
stream: true,
stdin: true,
stdout: true,
stderr: true
});
}).then(stream => {
container.modem.demuxStream(stream, socket, socket);
stream.on('end', () => {
socket.emit('disconnect');
container.remove();
});
socket.on('data', (data) => {
stream.write(data);
});
});
}).catch(err => {
console.error(err);
});
socket.on('disconnect', () => {
console.log('User disconnected');
});
});
const port = 3000;
server.listen(port, () => console.log(`Server running on port ${port}`));
I've searched for a lot of information and even asked chatGPT, but I still can't solve the problem.
The problem is that the nodejs can create a container when a user gose to localhost:3000, but I can't enter my command into web terminal.
I have tried to print what the user entered into the web terminal, although the web terminal doesn't show anything. And indeed, it fetched the command that the user typed.
I expect this file to allow users to visit localhost:3000 and create a new container for themselves. Plus, I hope that the web terminal provides an interactive interface that can run commands such as 'top' and 'vim'.
Did you follow the instructions on the Get started page of Socket.IO? Express is an HTTP server you are supposed to have at least one mapping for let's say the web server's home page, something like:
Or maybe do you have a
public/index.htmlfile in your folder?What is this command returning:
curl -i http://localhost:3000/