/**
* Simple test script for DockerClient and Container
*/
import { dockerClient } from './docker/DockerClient.js';
import { Container } from './docker/Container.js';
async function testDocker() {
console.log('π§ͺ Testing Docker connection...\n');
try {
// 1. Ping Docker
console.log('1οΈβ£ Pinging Docker...');
const isPingOk = await dockerClient.ping();
console.log(` β
Docker is ${isPingOk ? 'RUNNING' : 'NOT RUNNING'}\n`);
if (!isPingOk) {
throw new Error('Docker is not running');
}
// 2. Get Docker info
console.log('2οΈβ£ Getting Docker info...');
const info = await dockerClient.getInfo();
console.log(` β
Docker version: ${info.ServerVersion}`);
console.log(` β
Operating system: ${info.OperatingSystem}`);
console.log(` β
Architecture: ${info.Architecture}\n`);
// 3. Pull Python image
console.log('3οΈβ£ Pulling python:3.11-slim image (this may take a minute)...');
await dockerClient.pullImage('python:3.11-slim', (progress) => {
if (progress.status === 'Downloading' || progress.status === 'Extracting') {
process.stdout.write(` π¦ ${progress.status}...\r`);
}
});
console.log(' β
Image pulled successfully\n');
// 4. Create container
console.log('4οΈβ£ Creating container...');
const dockerContainer = await dockerClient.createContainer({
image: 'python:3.11-slim',
language: 'python',
memory: '256m',
cpus: '0.5',
});
const container = new Container(dockerContainer, 'python');
console.log(` β
Container created: ${container.id.substring(0, 12)}\n`);
// 5. Start container
console.log('5οΈβ£ Starting container...');
await container.start();
console.log(' β
Container started\n');
// 6. Execute simple command
console.log('6οΈβ£ Executing Python code...');
const result = await container.exec(['python', '-c', 'print("Hello from MCP Sandbox!")']);
console.log(` β
Exit code: ${result.exitCode}`);
console.log(` β
Output: ${result.stdout.trim()}`);
console.log(` β
Duration: ${result.duration}ms\n`);
// 7. Test file operations
console.log('7οΈβ£ Testing file operations...');
await container.putFile('/tmp/test.txt', 'Hello World!');
const fileContent = await container.getFile('/tmp/test.txt');
console.log(` β
File written and read: ${fileContent.toString().trim()}\n`);
// 8. Get container stats
console.log('8οΈβ£ Getting container stats...');
const stats = await container.stats();
console.log(` β
Memory usage: ${stats.memoryPeakMB.toFixed(2)} MB`);
console.log(` β
CPU time: ${stats.cpuTimeMs.toFixed(2)} ms\n`);
// 9. Stop container
console.log('9οΈβ£ Stopping container...');
await container.stop();
console.log(' β
Container stopped\n');
// 10. Remove container
console.log('π Removing container...');
await container.remove();
console.log(' β
Container removed\n');
console.log('π All tests passed!\n');
} catch (error: any) {
console.error('β Test failed:', error.message);
process.exit(1);
}
}
testDocker();