Overview
The Docker connector operates in two modes:- Image Mode - Creates a new container from an image, executes operations, saves to a new image, and removes the container
- Container Mode - Executes operations against an existing running container
The Docker connector is great for testing pyinfra operations locally without needing remote SSH hosts.
Connector Data
str
required
Docker image name or existing container ID.
str
Platform to use for Docker image (e.g.,
linux/amd64, linux/arm64).str
Architecture to use for Docker image (e.g.,
amd64, arm64).Basic Usage
From Docker Image
From Existing Container
Inventory Examples
Single Container
Multiple Containers
Containers with Data
Cross-Platform Containers
Creating Docker Images
When using image mode, pyinfra automatically:- Creates a new container from the image
- Executes operations
- Commits changes to a new image
- Removes the temporary container
Image Mode vs Container Mode
Image Mode
Used when specifying an image name:- Create a new container:
docker run -d ubuntu:22.04 tail -f /dev/null - Execute operations
- Optionally save to new image
- Remove container
Container Mode
Used when specifying a container ID:- Use existing container (starts it if stopped)
- Execute operations
- Leave container running
Using Podman
The Docker connector works with Podman too:Complete Example
Here’s a complete example creating a custom web server image:Testing with Docker
The Docker connector is ideal for testing deployments locally:Multiple Distributions
Test operations across multiple Linux distributions:Limitations
Troubleshooting
Container Not Found
Permission Denied
Ensure Docker daemon is accessible:Platform Issues
Specify platform explicitly:Differences from DockerSSH
- docker connector - Executes commands in local Docker containers
- dockerssh connector - SSHs into Docker containers running on remote hosts
Source Reference
Location:src/pyinfra/connectors/docker.py:40
Key Properties
docker_cmd- Command to use (default: “docker”, can be “podman”)handles_execution- This connector handles command executioncontainer_id- ID of running container
Key Methods
connect()- Create/connect to containerdisconnect()- Stop/cleanup containerrun_shell_command()- Execute command in containerput_file()- Copy file into containerget_file()- Copy file from container
