codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐳

Docker

27 / 60 topics
27Docker API43Docker API Advanced59Docker API Advanced Topics
Tutorials/Docker/Docker API
🐳Docker

Docker API

Updated 2026-05-15
10 min read

Docker API

Introduction

In this section, we will explore how to use the Docker API to automate various tasks. The Docker API provides a powerful way to interact with Docker containers and images programmatically. This can be particularly useful for automating deployment processes, managing container lifecycles, or integrating Docker into larger automation workflows.

Concept

The Docker API is accessible via HTTP on port 2375 by default (or 2376 if TLS is enabled). It supports a wide range of endpoints that allow you to manage containers, images, networks, and volumes. The API is RESTful, meaning it uses standard HTTP methods like GET, POST, PUT, and DELETE.

To interact with the Docker API, you can use tools like curl, or more advanced libraries in various programming languages such as Python's docker-py or Node.js's dockerode.

Examples

1. List All Containers

Let's start by listing all running containers using the Docker API.

Terminal
{`$ curl http://localhost:2375/containers/json`}
Output
{`[
  {
      "Id": "container_id_1",
      "Image": "image_name_1",
      "Command": "/bin/sh -c 'echo Hello World'",
      "Names": ["/container_name_1"],
      "State": "running"
  },
  {
      "Id": "container_id_2",
      "Image": "image_name_2",
      "Command": "/bin/sh -c 'echo Hello Docker'",
      "Names": ["/container_name_2"],
      "State": "exited"
  }
]`}

2. Start a New Container

Next, let's create and start a new container from an image.

Terminal
{`$ curl -X POST http://localhost:2375/containers/create --data '{"Image":"nginx", "Name":"my-nginx"}`}
Output
{`{
  "Id": "new_container_id",
  "Warnings": null
}`}
Terminal
{`$ curl -X POST http://localhost:2375/containers/new_container_id/start`}

3. Automate with Python

For more complex automation tasks, you can use a programming language like Python. Here's an example using the docker-py library.

First, install the library:

Terminal
{`$ pip install docker`}

Then, create a Python script to list all containers:

Python
1{`{`
2import docker
3
4client = docker.from_env()
5
6for container in client.containers.list():
7 print(f"Container ID: {container.id}, Image: {container.image.tags[0]}, Status: {container.status}")
8`}`}

4. Automate with Node.js

Similarly, you can use Node.js with the dockerode library.

First, install the library:

Terminal
{`$ npm install dockerode`}

Then, create a JavaScript script to list all containers:

JavaScript
1{`{`
2const Docker = require('dockerode');
3const docker = new Docker();
4
5docker.listContainers((err, containers) => {
6 if (err) throw err;
7
8 containers.forEach(container => {
9 console.log(`Container ID: ${container.Id}, Image: ${container.Image}, Status: ${container.Status}`);
10 });
11});
12`}`}

What's Next?

Now that you have a good understanding of how to use the Docker API for automation tasks, you might want to explore more advanced features such as managing networks and volumes. Additionally, integrating Docker into CI/CD pipelines can significantly enhance your development workflow.

For further exploration, consider checking out the official Docker API documentation and experimenting with different endpoints and configurations.


PreviousDocker EventsNext Docker CLI Advanced

Recommended Gear

Docker EventsDocker CLI Advanced