Adapters
The Adapters are a way of separating the logic for making HTTP requests from the main Docker client library. This allows the Docker client to be more flexible and adapt to different environments.
Motivation
The goal of Adapters is to allow Docker users to work with various methods for making HTTP calls. This can be useful in various scenarios, such as:
- Connecting to a Docker daemon running on a different host.
- Connecting to a Docker daemon running on a Unix socket.
- Connecting to a Docker daemon running over a TCP socket.
- Using a proxy server to access a Docker daemon.
- Implementing custom authentication mechanisms.
Usage
The docker.DockerClient
class provides a _create_adapter
method that returns an instance of an adapter. There are multiple adapter classes available, each implementing different methods of making HTTP requests.
Available Adapters
- HTTPAdapter: This is the default adapter, which uses the standard Python
urllib3
library for HTTP communication. It is used in most cases, and is the most flexible and customizable adapter. - UnixSocketAdapter: This adapter is specifically designed for connecting to Docker daemons running on a Unix socket. It uses the
httplib2
library with theunix_socket
module to make requests. - TCPSocketAdapter: This adapter is designed for connecting to Docker daemons running over a TCP socket. It uses the
httplib2
library with thetcp_socket
module to make requests. - SSLAdapter: This adapter adds SSL support to the HTTPAdapter. It uses the
urllib3
library with SSL support to make requests.
Example: HTTPAdapter
import docker
# Create a Docker client with the default HTTPAdapter.
client = docker.DockerClient()
# Make an HTTP request to the Docker daemon.
response = client.images.list()
# Print the response.
print(response)
Example: UnixSocketAdapter
import docker
# Create a Docker client with the UnixSocketAdapter.
client = docker.DockerClient(base_url='unix://var/run/docker.sock')
# Make an HTTP request to the Docker daemon.
response = client.images.list()
# Print the response.
print(response)
Example: TCPSocketAdapter
import docker
# Create a Docker client with the TCPSocketAdapter.
client = docker.DockerClient(base_url='tcp://127.0.0.1:2376')
# Make an HTTP request to the Docker daemon.
response = client.images.list()
# Print the response.
print(response)
Example: SSLAdapter
import docker
# Create a Docker client with the SSLAdapter.
client = docker.DockerClient(base_url='https://127.0.0.1:2376', tls=True)
# Make an HTTP request to the Docker daemon.
response = client.images.list()
# Print the response.
print(response)
Example: Custom Adapter
import docker
# Create a custom adapter class.
class MyCustomAdapter(docker.adapters.HTTPAdapter):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Add custom logic here.
# Create a Docker client with the custom adapter.
client = docker.DockerClient(adapter=MyCustomAdapter())
# Make an HTTP request to the Docker daemon.
response = client.images.list()
# Print the response.
print(response)
Notes
Adapters are an important part of the Docker client library, allowing for flexibility and customization. Using the right adapter can improve performance, security, and reliability.