Back to articles
Docker Has a Free API — Here's How to Manage Containers Programmatically

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

via Dev.to JavaScriptAlex Spinov

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. Enable the API The Docker daemon exposes its API on a Unix socket by default: curl --unix-socket /var/run/docker.sock http://localhost/v1.44/info | jq .Containers List Containers const response = await fetch ( " http://localhost/v1.44/containers/json?all=true " , { socketPath : " /var/run/docker.sock " }); const containers = await response . json (); containers . forEach ( c => console . log ( ` ${ c . Names [ 0 ]} : ${ c . State } ` )); Using Dockerode (Node.js SDK) npm install dockerode import Docker from " dockerode " ; const docker = new Docker (); // List running containers const containers = await docker . listContainers (); containers . forEach ( c => { console . log ( ` ${ c . Names [ 0 ]} — ${ c . Image } — ${ c . State } ` ); }); Create and Start Container // Pull image await docker . pull ( " nginx

Continue reading on Dev.to JavaScript

Opens in a new tab

Read Full Article
2 views

Related Articles