docker-pycreds: Python bindings for the Docker credentials store API

0.4.0 · maintenance · verified Mon Apr 06

docker-pycreds provides Python bindings for the Docker credentials store API. It enables programmatic interaction with platform-specific credential helpers (e.g., `docker-credential-secretservice`) to securely store and retrieve Docker authentication tokens. The current version is 0.4.0, released on November 28, 2018. Due to its last update date, it has an infrequent release cadence and appears to be in maintenance mode.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a credential store and use it to store and list Docker credentials. It assumes a `docker-credential` helper for your platform is installed and configured. Storing credentials might trigger a system-level prompt for keyring access depending on the backend.

import dockerpycreds
import os

# Configure the credential store backend. Common options include:
# 'secretservice' (Linux), 'osxkeychain' (macOS), 'wincred' (Windows)
# If you have a custom credential helper, use its name (e.g., 'ecr-login')
# Ensure the corresponding 'docker-credential-<backend>' executable is in your PATH.
store_backend = os.environ.get('DOCKER_CREDS_STORE_BACKEND', 'secretservice')
store = dockerpycreds.Store(store_backend)

# Define credentials from environment variables for security in examples
server_url = os.environ.get('DOCKER_SERVER_URL', 'https://index.docker.io/v1/')
username = os.environ.get('DOCKER_USERNAME', 'testuser')
secret = os.environ.get('DOCKER_SECRET', 'testpassword')

print(f"Attempting to store credentials for {username} on {server_url} using '{store_backend}'...")
try:
    store.store(
        server=server_url,
        username=username,
        secret=secret
    )
    print("Credentials stored successfully. Note: This might have prompted for system keyring access.")
except Exception as e:
    print(f"Failed to store credentials: {e}. Ensure the credential helper is correctly configured and accessible.")

print("\nListing all known Docker credential servers:")
try:
    creds_list = store.list()
    if creds_list:
        for server, user in creds_list.items():
            print(f"  Server: {server}, Username: {user}")
    else:
        print("  No credentials found.")
except Exception as e:
    print(f"Failed to list credentials: {e}. Listing may require user interaction or specific backend support.")

# Example of getting credentials (often requires explicit interaction with the helper)
# This is commented out as it frequently requires external CLI calls or user prompts
# which are hard to automate in a simple quickstart.
# try:
#     retrieved_username, retrieved_secret = store.get(server_url)
#     print(f"\nRetrieved credentials for {server_url}: Username: {retrieved_username}, Secret: {'*' * len(retrieved_secret)}")
# except Exception as e:
#     print(f"Failed to retrieve credentials: {e}. This often requires direct Docker CLI interaction or specific credential helper setup.")

view raw JSON →