How to Use Docker SDK for Python: Simple Guide
Use the
docker Python package to interact with Docker from your Python code. First, install it with pip install docker, then create a docker.from_env() client to manage containers, images, and more.Syntax
The Docker SDK for Python uses a client object to interact with Docker. You create it using docker.from_env(), which connects to your local Docker daemon. You can then call methods like client.containers.run() to start containers or client.images.pull() to download images.
Key parts:
docker.from_env(): Creates a client connected to Docker.client.containers: Access container-related actions.client.images: Access image-related actions.
python
import docker client = docker.from_env() # Run a container container = client.containers.run('hello-world', detach=True) # Pull an image image = client.images.pull('alpine')
Example
This example shows how to run a simple container that prints a message and then fetch its logs using the Docker SDK for Python.
python
import docker client = docker.from_env() # Run the 'hello-world' container and wait for it to finish container = client.containers.run('hello-world', detach=True) container.wait() # Wait for container to finish # Get container logs logs = container.logs().decode('utf-8') print(logs) # Clean up container container.remove()
Output
Hello from Docker!\nThis message shows that your installation appears to be working correctly.\n...
Common Pitfalls
Common mistakes when using Docker SDK for Python include:
- Not installing the
dockerpackage with pip. - Trying to connect without Docker daemon running locally.
- Forgetting to decode logs from bytes to string.
- Not detaching containers when you want to interact with them asynchronously.
Always handle exceptions to catch connection or API errors.
python
import docker client = docker.from_env() # Wrong: Not detaching, blocks the program # container = client.containers.run('alpine', 'echo hello') # Right: Detach to get container object container = client.containers.run('alpine', 'echo hello', detach=True) container.wait() print(container.logs().decode('utf-8'))
Output
hello
Quick Reference
Here is a quick cheat sheet for common Docker SDK for Python commands:
| Command | Description |
|---|---|
| docker.from_env() | Create a Docker client connected to local Docker daemon |
| client.containers.run(image, command, detach=True) | Run a container from an image with a command |
| client.containers.list() | List running containers |
| client.images.pull(image_name) | Download a Docker image |
| container.logs() | Get logs from a container |
| container.wait() | Wait for container to finish |
| container.remove() | Remove a container |
Key Takeaways
Install the Docker SDK for Python with 'pip install docker' before use.
Create a client with 'docker.from_env()' to connect to your local Docker daemon.
Use 'client.containers.run()' with 'detach=True' to run containers asynchronously.
Always decode container logs from bytes to string using '.decode('utf-8')'.
Handle exceptions to manage connection or API errors gracefully.