feat(DockerHost): Add DockerHost.ping() to check Docker daemon availability and document health-check usage

This commit is contained in:
2025-11-18 17:30:04 +00:00
parent b8a26bf3bd
commit 08af9fec14
4 changed files with 65 additions and 6 deletions

View File

@@ -38,11 +38,9 @@ import { DockerHost } from '@apiclient.xyz/docker';
const docker = new DockerHost({});
await docker.start();
// Or connect to remote Docker host via TCP
const remoteDocker = new DockerHost({
socketPath: 'tcp://192.168.1.100:2375',
});
await remoteDocker.start();
// Check if Docker is accessible
await docker.ping();
console.log('✅ Docker is running');
// List all containers
const containers = await docker.getContainers();
@@ -100,6 +98,48 @@ await docker.start();
await docker.stop();
```
#### Health Check / Ping Docker
Check if the Docker daemon is running and accessible:
```typescript
// Ping Docker daemon
try {
await docker.ping();
console.log('✅ Docker is running and accessible');
} catch (error) {
console.error('❌ Docker is not accessible:', error.message);
}
// Use in health check function
async function isDockerHealthy(): Promise<boolean> {
try {
await docker.ping();
return true;
} catch (error) {
return false;
}
}
// Example: Wait for Docker to be ready
async function waitForDocker(timeoutMs = 10000): Promise<void> {
const startTime = Date.now();
while (Date.now() - startTime < timeoutMs) {
try {
await docker.ping();
console.log('✅ Docker is ready');
return;
} catch (error) {
console.log('⏳ Waiting for Docker...');
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
throw new Error('Docker did not become available within timeout');
}
```
### 📦 Container Management
#### List All Containers
@@ -681,6 +721,7 @@ if (webService && webService.Spec.Mode.Replicated) {
## 🔑 Key Concepts
- **DockerHost**: Main entry point for Docker API communication
- **Health Checks**: Use `ping()` method to verify Docker daemon accessibility
- **Socket Path Priority**: Constructor option → `DOCKER_HOST` env → CI mode → default socket
- **Swarm Mode Required**: Services and secrets require Docker Swarm to be activated
- **Type Safety**: Full TypeScript support with comprehensive interfaces