Tools: Docker Has a Free API — Here's How to Manage Containers Programmatically

Tools: Docker Has a Free API — Here's How to Manage Containers Programmatically

Enable the API

List Containers

Using Dockerode (Node.js SDK)

Create and Start Container

Container Logs

Execute Command in Container

Image Management Docker provides a REST API that lets you manage containers, images, networks, and volumes programmatically. It runs on a Unix socket and is available on every Docker installation. The Docker daemon exposes its API on a Unix socket by default: Need to extract or automate web content at scale? Check out my web scraping tools on Apify — no coding required. Or email me at [email protected] for custom solutions. Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

$ -weight: 500;">curl --unix-socket /var/run/-weight: 500;">docker.sock http://localhost/v1.44/info | jq .Containers -weight: 500;">curl --unix-socket /var/run/-weight: 500;">docker.sock http://localhost/v1.44/info | jq .Containers -weight: 500;">curl --unix-socket /var/run/-weight: 500;">docker.sock http://localhost/v1.44/info | jq .Containers const response = await fetch("http://localhost/v1.44/containers/json?all=true", { socketPath: "/var/run/-weight: 500;">docker.sock" }); const containers = await response.json(); containers.forEach(c => console.log(`${c.Names[0]}: ${c.State}`)); const response = await fetch("http://localhost/v1.44/containers/json?all=true", { socketPath: "/var/run/-weight: 500;">docker.sock" }); const containers = await response.json(); containers.forEach(c => console.log(`${c.Names[0]}: ${c.State}`)); const response = await fetch("http://localhost/v1.44/containers/json?all=true", { socketPath: "/var/run/-weight: 500;">docker.sock" }); const containers = await response.json(); containers.forEach(c => console.log(`${c.Names[0]}: ${c.State}`)); -weight: 500;">npm -weight: 500;">install dockerode -weight: 500;">npm -weight: 500;">install dockerode -weight: 500;">npm -weight: 500;">install dockerode import Docker from "dockerode"; const -weight: 500;">docker = new Docker(); // List running containers const containers = await -weight: 500;">docker.listContainers(); containers.forEach(c => { console.log(`${c.Names[0]} — ${c.Image} — ${c.State}`); }); import Docker from "dockerode"; const -weight: 500;">docker = new Docker(); // List running containers const containers = await -weight: 500;">docker.listContainers(); containers.forEach(c => { console.log(`${c.Names[0]} — ${c.Image} — ${c.State}`); }); import Docker from "dockerode"; const -weight: 500;">docker = new Docker(); // List running containers const containers = await -weight: 500;">docker.listContainers(); containers.forEach(c => { console.log(`${c.Names[0]} — ${c.Image} — ${c.State}`); }); // Pull image await -weight: 500;">docker.pull("nginx:latest"); // Create container const container = await -weight: 500;">docker.createContainer({ Image: "nginx:latest", name: "my-nginx", ExposedPorts: { "80/tcp": {} }, HostConfig: { PortBindings: { "80/tcp": [{ HostPort: "8080" }] }, RestartPolicy: { Name: "unless-stopped" } }, Env: ["NGINX_HOST=localhost"] }); await container.-weight: 500;">start(); console.log("Container started!"); // Pull image await -weight: 500;">docker.pull("nginx:latest"); // Create container const container = await -weight: 500;">docker.createContainer({ Image: "nginx:latest", name: "my-nginx", ExposedPorts: { "80/tcp": {} }, HostConfig: { PortBindings: { "80/tcp": [{ HostPort: "8080" }] }, RestartPolicy: { Name: "unless-stopped" } }, Env: ["NGINX_HOST=localhost"] }); await container.-weight: 500;">start(); console.log("Container started!"); // Pull image await -weight: 500;">docker.pull("nginx:latest"); // Create container const container = await -weight: 500;">docker.createContainer({ Image: "nginx:latest", name: "my-nginx", ExposedPorts: { "80/tcp": {} }, HostConfig: { PortBindings: { "80/tcp": [{ HostPort: "8080" }] }, RestartPolicy: { Name: "unless-stopped" } }, Env: ["NGINX_HOST=localhost"] }); await container.-weight: 500;">start(); console.log("Container started!"); const container = -weight: 500;">docker.getContainer("my-nginx"); // Get logs const logs = await container.logs({ stdout: true, stderr: true, timestamps: true, tail: 50 }); console.log(logs.toString()); // Stream logs in real-time const stream = await container.logs({ follow: true, stdout: true }); stream.on("data", (chunk) => process.stdout.write(chunk)); const container = -weight: 500;">docker.getContainer("my-nginx"); // Get logs const logs = await container.logs({ stdout: true, stderr: true, timestamps: true, tail: 50 }); console.log(logs.toString()); // Stream logs in real-time const stream = await container.logs({ follow: true, stdout: true }); stream.on("data", (chunk) => process.stdout.write(chunk)); const container = -weight: 500;">docker.getContainer("my-nginx"); // Get logs const logs = await container.logs({ stdout: true, stderr: true, timestamps: true, tail: 50 }); console.log(logs.toString()); // Stream logs in real-time const stream = await container.logs({ follow: true, stdout: true }); stream.on("data", (chunk) => process.stdout.write(chunk)); const exec = await container.exec({ Cmd: ["ls", "-la", "/usr/share/nginx/html"], AttachStdout: true }); const { output } = await exec.-weight: 500;">start(); output.pipe(process.stdout); const exec = await container.exec({ Cmd: ["ls", "-la", "/usr/share/nginx/html"], AttachStdout: true }); const { output } = await exec.-weight: 500;">start(); output.pipe(process.stdout); const exec = await container.exec({ Cmd: ["ls", "-la", "/usr/share/nginx/html"], AttachStdout: true }); const { output } = await exec.-weight: 500;">start(); output.pipe(process.stdout); // List images const images = await -weight: 500;">docker.listImages(); images.forEach(img => { console.log(`${img.RepoTags?.[0]} — ${(img.Size / 1e6).toFixed(0)}MB`); }); // Build from Dockerfile const stream = await -weight: 500;">docker.buildImage({ context: "./app", src: ["Dockerfile", "package.json", "src/"] }, { t: "my-app:latest" }); // List images const images = await -weight: 500;">docker.listImages(); images.forEach(img => { console.log(`${img.RepoTags?.[0]} — ${(img.Size / 1e6).toFixed(0)}MB`); }); // Build from Dockerfile const stream = await -weight: 500;">docker.buildImage({ context: "./app", src: ["Dockerfile", "package.json", "src/"] }, { t: "my-app:latest" }); // List images const images = await -weight: 500;">docker.listImages(); images.forEach(img => { console.log(`${img.RepoTags?.[0]} — ${(img.Size / 1e6).toFixed(0)}MB`); }); // Build from Dockerfile const stream = await -weight: 500;">docker.buildImage({ context: "./app", src: ["Dockerfile", "package.json", "src/"] }, { t: "my-app:latest" });