Skip to main content
The Docker connector allows pyinfra to target Docker containers as inventory. You can create new containers from images, execute operations, and save the results as new images.

Overview

The Docker connector operates in two modes:
  1. Image Mode - Creates a new container from an image, executes operations, saves to a new image, and removes the container
  2. 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:
  1. Creates a new container from the image
  2. Executes operations
  3. Commits changes to a new image
  4. Removes the temporary container

Image Mode vs Container Mode

Image Mode

Used when specifying an image name:
Pyinfra will:
  • 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:
Pyinfra will:
  • 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

The Docker connector has some limitations:
  • No systemd support in containers by default
  • Some privileged operations may fail
  • Container must have a shell available
  • File permissions may differ from regular hosts

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
For remote Docker containers, use the dockerssh connector.

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 execution
  • container_id - ID of running container

Key Methods

  • connect() - Create/connect to container
  • disconnect() - Stop/cleanup container
  • run_shell_command() - Execute command in container
  • put_file() - Copy file into container
  • get_file() - Copy file from container