Docker APIs and SDKs

Follow Us

Our Communities

Chapter 6: Docker APIs and SDKs

In today’s world, where automation is key and everything is becoming programmatically managed, it is important to understand how Docker can be controlled and manipulated using APIs and SDKs. This chapter dives into the vast world of Docker APIs and SDKs, where you will learn to interact with Docker beyond the command-line interface.

We will start with an introduction to Docker API and its significance, followed by exploring the basics of working with Docker API. This includes making HTTP requests to the API, interpreting responses, and using the API to manage Docker objects such as containers and images.

We then introduce the Docker SDK for Python, which provides a more high-level, Pythonic way of interacting with Docker. We’ll build a Docker management application using the Docker SDK to showcase its capabilities and ease of use.

The chapter will also address critical aspects of Docker API usage, such as rate limiting and security. Rate limiting is important to prevent abuse of the API, and you will learn how to monitor and adjust your API usage accordingly. For security, we’ll delve into how to protect your Docker API using TLS and certificate-based authentication.

Finally, we’ll explore some advanced topics in Docker API, such as Docker’s Swarm mode, system events, exec API, build API, plugin API, and networking API. These powerful features offer a deeper level of control over Docker and open up endless possibilities for automation and integration.

By the end of this chapter, you’ll have a solid understanding of Docker APIs and SDKs, enabling you to automate Docker operations, integrate Docker with other applications or services, and secure your Docker API usage. So, let’s dive in and get started!

Introduction to Docker API

The Docker API, or Application Programming Interface, is a set of rules that allows one piece of software application to interact with another. It enables software developers to perform operations on Docker objects such as containers, images, networks, and volumes programmatically.

The Docker API is essentially a contract provided by Docker that developers can use to interact with the Docker Engine. This means that instead of using the Docker CLI (Command Line Interface) to create, start, stop, and otherwise manage your Docker components, you can use the Docker API to perform these tasks directly from within your own software applications. This can be especially useful for automating workflows and integrating Docker into larger software systems.

How Docker API Works

The Docker API operates on the client-server model. The Docker daemon acts as the server and listens for API requests, which can be sent from the Docker CLI (which acts as the client), the Docker SDKs (Software Development Kits), or directly from your own applications using HTTP/HTTPS protocols.

Once the Docker daemon receives an API request, it performs the requested operation (such as creating a new container or stopping a running container), and sends back a response to the client. This response typically includes information about the result of the operation, such as whether it was successful or any error messages if it was not.

The Docker API is built on the REST architectural style, which means that it uses standard HTTP methods like GET, POST, DELETE, and so on, to perform operations on Docker resources.

Understanding Docker API Versioning

Docker API uses versioning to maintain backward compatibility and to introduce new features. Each version of the Docker API introduces new endpoints, changes to existing endpoints, or updates to the request or response models. When making an API request, you can specify the version of the API that you want to use in the request URL.

The Docker API uses semantic versioning, which means that the version number is composed of three parts: the major version, the minor version, and the patch version. The major version number is increased when there are incompatible changes, the minor version number is increased when new features are added in a backward-compatible manner, and the patch version number is increased when backward-compatible bug fixes are made.

When you’re developing applications that use the Docker API, it’s important to keep track of the API version that you’re using, to ensure compatibility and to take advantage of new features.

Working with Docker API

Once you have a good understanding of what Docker API is and how it works, the next step is to learn how to work with it. In this section, we will delve deeper into making API requests to Docker, understanding Docker API endpoints, and managing containers using the Docker API.

Making API Requests to Docker

Docker API uses the REST architectural style, meaning it communicates with HTTP methods. You can make API requests to Docker using any programming language or tool that can send HTTP requests, such as curl, Python’s requests library, Postman, etc. Here’s an example of a basic GET request to the Docker API using curl:

curl --unix-socket /var/run/docker.sock http:/v1.24/containers/json

This request returns a list of all Docker containers in the JSON format. The --unix-socket /var/run/docker.sock part tells curl to connect to the Docker daemon through the Unix socket file at /var/run/docker.sock, and http:/v1.24/containers/json specifies the API endpoint (containers) and the API version (v1.24).

Docker API Endpoints

API endpoints are the specific functionalities provided by the API. In Docker API, there are endpoints for managing containers, images, networks, volumes, and more. Each endpoint corresponds to a specific URI (Uniform Resource Identifier) and one or more HTTP methods.

For example, the /containers endpoint is used to manage Docker containers. You can use the GET method with the /containers/json URI to list all containers, the POST method with the /containers/create URI to create a new container, and so on. The full list of Docker API endpoints and their functionalities is available in the Docker API documentation.

Managing Containers Using Docker API

Using the Docker API, you can manage the entire lifecycle of a Docker container. Let’s see some examples:

Creating a container: This is a POST request to the /containers/create endpoint.

curl -X POST -H "Content-Type: application/json" \
--unix-socket /var/run/docker.sock \
-d '{"Image": "ubuntu:latest", "Cmd": ["echo", "hello world"]}' \

Starting a container: This is a POST request to the /containers/{id}/start endpoint.

curl -X POST --unix-socket /var/run/docker.sock http:/v1.24/containers/{id}/start

Stopping a container: This is a POST request to the /containers/{id}/stop endpoint.

curl -X POST --unix-socket /var/run/docker.sock http:/v1.24/containers/{id}/stop

Removing a container: This is a DELETE request to the /containers/{id} endpoint.

curl -X DELETE --unix-socket /var/run/docker.sock http:/v1.24/containers/{id}

Docker SDK for Python

The Docker SDK for Python is a Python library that provides a way to use the Docker API in a Pythonic, idiomatic way. It offers a high-level object-oriented interface as well as low-level direct access to Docker’s REST API.

Overview of Docker SDK for Python

Docker SDK for Python enables you to automate Docker tasks, such as creating and managing containers, pulling and pushing images, managing networks and volumes, and so on. It’s especially useful for building applications that need to interact with Docker, and for scripting and automation tasks.

With Docker SDK for Python, you don’t have to manually craft HTTP requests and parse HTTP responses. Instead, you can use Python objects, methods, and exceptions, which makes your code simpler, easier to read, and less prone to errors.

Setting up Docker SDK for Python

To install the Docker SDK for Python, you can use pip, the Python package installer. Here’s the command:

pip install docker

This command installs the docker package, which includes the Docker SDK for Python.

Managing Docker with Python SDK

Here’s an example of how to use Docker SDK for Python to create, start, and stop a Docker container.

import docker

# Create a Docker client
client = docker.from_env()

# Pull an image

# Create a container
container = client.containers.create('ubuntu:latest', command='/bin/sleep 30')

# Start the container

# Wait for the container to finish

# Stop and remove the container

In this example, docker.from_env() creates a Docker client that connects to the Docker daemon. The client’s images.pull() method pulls an image from Docker Hub, containers.create() creates a container, start() starts the container, wait() waits for the container to finish, and remove() stops and removes the container.

Note that this is a basic example. Docker SDK for Python supports many more operations and options, such as managing container networks, volumes, and logs, inspecting containers and images, pushing images to Docker registries, and so on.

In the following sections, we will dive deeper into building applications with the Docker SDK for Python, rate limiting and securing the Docker API, and some advanced topics.

Creating a Docker Management Application

Now that we have an understanding of the Docker SDK for Python, let’s create a simple Docker management application using it. This application will allow users to list, start, stop, and remove Docker containers.

Setting Up

Before we start coding, ensure that Python and Docker are properly installed on your system. Also, you’ll need to install the Docker SDK for Python using the pip command:

pip install docker

The Code

Here’s the code for our Docker management application:

import docker
from flask import Flask, jsonify

# Create a Flask web server
app = Flask(__name__)

# Create a Docker client
client = docker.from_env()

# Route to list containers
@app.route('/containers', methods=['GET'])
def list_containers():
    containers = client.containers.list(all=True)
    return jsonify([container.short_id for container in containers])

# Route to start a container
@app.route('/containers/<id>/start', methods=['POST'])
def start_container(id):
    container = client.containers.get(id)
    return '', 204

# Route to stop a container
@app.route('/containers/<id>/stop', methods=['POST'])
def stop_container(id):
    container = client.containers.get(id)
    return '', 204

# Route to remove a container
@app.route('/containers/<id>', methods=['DELETE'])
def remove_container(id):
    container = client.containers.get(id)
    return '', 204

# Run the server
if __name__ == '__main__':

In this code, we’re using Flask, a lightweight Python web framework, to create a web server. This server exposes several HTTP routes that correspond to Docker operations: listing, starting, stopping, and removing containers.

Each route handler uses the Docker SDK for Python to perform the corresponding operation. For example, the list_containers handler calls client.containers.list(all=True) to get a list of all containers, and returns their short IDs in the JSON format.

Running the Application

To run the application, simply execute the Python file. Make sure that your Docker daemon is running. You can then use any HTTP client (like curl or Postman) to send requests to your server.

For example, to list all containers, send a GET request to http://localhost:5000/containers. To start a container with a specific ID, send a POST request to http://localhost:5000/containers/<id>/start.

Next Steps

This is a basic Docker management application. There are many more features you can add, such as creating and managing Docker images, networks, and volumes, inspecting containers and images, streaming container logs, and so on. This application can serve as a starting point for building more complex Docker management applications, Docker-based CI/CD systems, Docker orchestrators, and more.

Docker API Rate Limiting

Rate limiting is an important concept when working with APIs. It is used to control the number of requests a client can make to an API within a certain period of time. This is done mainly to prevent abuse, protect the API server from being overwhelmed by too many requests, and ensure fair usage.

When interacting with Docker API, particularly the Docker Hub API, it’s important to be aware that Docker imposes rate limits on the number of requests you can make.

Understanding Docker’s Rate Limits

As of my knowledge cut-off in September 2021, Docker Hub applies rate limiting based on the type of user:

  • For anonymous users, the limit is 100 pulls per 6 hours.
  • For authenticated free users, the limit is 200 pulls per 6 hours.
  • Docker Pro and Docker Team subscribers do not have any rate limits.

Please note that these numbers may have changed after my knowledge cut-off. Always refer to Docker’s official documentation for the most accurate and up-to-date information.

Handling Rate Limits in Your Applications

When you make a request to Docker API, the HTTP response headers include fields that indicate the current rate limit status:

  • RateLimit-Limit: The maximum number of requests you can make in the current time window.
  • RateLimit-Remaining: The number of requests you can still make in the current time window.

By monitoring these headers, your application can adjust its request rate to avoid hitting the rate limit. For example, if RateLimit-Remaining is low, your application can decide to delay some non-essential requests until the next time window.

In addition, if you exceed the rate limit, Docker API responds with the 429 Too Many Requests status code. Your application should be prepared to handle this error by retrying the request after some delay.

Securing Docker API

The Docker API provides a wide range of control over Docker functionalities, which can pose a security risk if not properly secured. Unauthorized access to the Docker API can potentially lead to a takeover of the host system where Docker is running. Therefore, it’s essential to apply security measures when using Docker API.

Here are some steps to secure your Docker API:

1. Enable TLS: Transport Layer Security (TLS) ensures communication to and from the Docker API is encrypted, preventing eavesdropping. Docker supports TLS by default. However, you’ll need to generate and specify the required certificates.

2. Use Certificate Authentication: By issuing client-side certificates, you can restrict Docker API access to clients that have been issued a valid certificate. This is a form of mutual TLS (mTLS) where both the client and server verify each other’s identity.

3. Limit API Exposure: By default, Docker listens on a Unix socket that doesn’t require network access. If you need to expose the API over a network, ensure it’s only accessible by trusted clients. If possible, use a network firewall or a similar control to limit access to the API.

4. Regularly Update Docker: Docker frequently releases updates that include security patches. Regularly updating Docker ensures that you benefit from these enhancements and that your Docker API is as secure as possible.

5. Control API Permissions: Where possible, restrict which API endpoints a client can access. This reduces the impact if an unauthorized client gains access.

6. Monitor API Activity: Regular monitoring and logging of API activity help identify suspicious behavior and mitigate potential security issues. Docker includes logging capabilities that can be integrated with standard log management solutions.

Remember, Docker security, like most aspects of IT security, is about layers. Implementing multiple security measures will provide more robust protection for your Docker API than relying on a single method.

Advanced Topics in Docker API

In the previous sections, we covered the basic usage of Docker API and some of its main aspects such as rate limiting and security. However, the Docker API offers many more advanced functionalities, which we’ll outline in this section:

1. Swarm Mode:

Docker API includes endpoints for managing Docker in Swarm mode. This mode allows you to orchestrate Docker services across multiple Docker nodes. The API allows you to manage Swarm services, tasks, nodes, and secrets, among other resources.

2. System Events:

Docker API provides a system events endpoint that can be used to receive real-time events from the Docker daemon. These events include container start/stop events, image pull events, volume mount events, and more. By listening to these events, your application can react to changes in the Docker environment in real time.

3. Exec API:

The exec API allows you to create new processes inside running Docker containers. This is similar to running the docker exec command. This API can be used to interact with a container’s file system, run maintenance tasks, troubleshoot issues, and more.

4. Build API:

Docker API includes a build endpoint, which allows you to build Docker images programmatically by sending a tarball of the Dockerfile and the build context to the API. This functionality can be used to create custom build systems, automate build processes, and more.

5. Plugin API:

Docker API also provides endpoints for managing Docker plugins. Plugins are tools that provide additional functionalities to the Docker engine. With the API, you can install, enable, disable, and remove plugins.

6. Network API:

The Docker API includes endpoints for managing Docker networks. Docker networking allows containers to communicate with each other and with external networks. The API allows you to create, inspect, list, and remove networks.

Remember, these advanced functionalities of Docker API offer more control and flexibility over your Docker environment, but they also come with additional complexity and potential security risks. Always make sure to properly secure your API and limit its exposure to the minimum necessary.

Exercise & Labs

Exercise 1: Use Docker API to List Containers

Make a GET request to Docker API and list all running containers. The endpoint for this task is GET /containers/json. Document your findings and display the list in a human-readable format.

Exercise 2: Run and Stop a Container Using Docker API

Use Docker API to run a new container from the ‘hello-world’ Docker image. This will require a POST request to the /containers/create endpoint and another to the /containers/(id)/start endpoint. Then, stop the container using the /containers/(id)/stop endpoint.

Exercise 3: Inspect a Container Using Docker API

Pick a running Docker container and inspect it using Docker API. The endpoint for this is GET /containers/(id)/json. Document the information you retrieve from the API.

Exercise 4: Create a Docker Management Application

Create a simple Python application that uses the Docker SDK for Python to manage Docker containers. Your application should at least be able to start, stop, and list containers.

Exercise 5: Monitoring Docker API Usage

Set up monitoring for Docker API usage. Use the RateLimit-Limit and RateLimit-Remaining HTTP headers to track how many requests your IP can make in a given period and how many requests remain.

Exercise 6: Work with Docker API Security

Use TLS to secure the Docker API and restrict API access to clients with a valid client-side certificate.

Exercise 7: Experiment with Docker Swarm Mode

If you have access to multiple Docker hosts, set up a Docker Swarm cluster and experiment with the Swarm-related Docker API endpoints.

Remember, while working on these exercises, take note of your experiences, problems encountered, and how you solved them. This will help enhance your understanding of the Docker API.

Network and Data Management


Docker Performance