{"library":"docker-pycreds","code":"import dockerpycreds\nimport os\n\n# Configure the credential store backend. Common options include:\n# 'secretservice' (Linux), 'osxkeychain' (macOS), 'wincred' (Windows)\n# If you have a custom credential helper, use its name (e.g., 'ecr-login')\n# Ensure the corresponding 'docker-credential-<backend>' executable is in your PATH.\nstore_backend = os.environ.get('DOCKER_CREDS_STORE_BACKEND', 'secretservice')\nstore = dockerpycreds.Store(store_backend)\n\n# Define credentials from environment variables for security in examples\nserver_url = os.environ.get('DOCKER_SERVER_URL', 'https://index.docker.io/v1/')\nusername = os.environ.get('DOCKER_USERNAME', 'testuser')\nsecret = os.environ.get('DOCKER_SECRET', 'testpassword')\n\nprint(f\"Attempting to store credentials for {username} on {server_url} using '{store_backend}'...\")\ntry:\n    store.store(\n        server=server_url,\n        username=username,\n        secret=secret\n    )\n    print(\"Credentials stored successfully. Note: This might have prompted for system keyring access.\")\nexcept Exception as e:\n    print(f\"Failed to store credentials: {e}. Ensure the credential helper is correctly configured and accessible.\")\n\nprint(\"\\nListing all known Docker credential servers:\")\ntry:\n    creds_list = store.list()\n    if creds_list:\n        for server, user in creds_list.items():\n            print(f\"  Server: {server}, Username: {user}\")\n    else:\n        print(\"  No credentials found.\")\nexcept Exception as e:\n    print(f\"Failed to list credentials: {e}. Listing may require user interaction or specific backend support.\")\n\n# Example of getting credentials (often requires explicit interaction with the helper)\n# This is commented out as it frequently requires external CLI calls or user prompts\n# which are hard to automate in a simple quickstart.\n# try:\n#     retrieved_username, retrieved_secret = store.get(server_url)\n#     print(f\"\\nRetrieved credentials for {server_url}: Username: {retrieved_username}, Secret: {'*' * len(retrieved_secret)}\")\n# except Exception as e:\n#     print(f\"Failed to retrieve credentials: {e}. This often requires direct Docker CLI interaction or specific credential helper setup.\")","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}