Publish images
This document shows how to use pycontainer-build to push images to various container registries.
Prerequisites
Section titled “Prerequisites”- pycontainer-build installed from the repository checkout root:
python -m pip install --no-deps -e . - Authentication tokens for your target registry
GitHub Container Registry (GHCR)
Section titled “GitHub Container Registry (GHCR)”1. Generate GitHub Personal Access Token
Section titled “1. Generate GitHub Personal Access Token”Go to GitHub Settings → Developer settings → Personal access tokens → Tokens (classic)
Generate a token with write:packages scope.
2. Build and Push
Section titled “2. Build and Push”Option 1: Environment Variable (Automatic)
# Set token - automatically detectedexport GITHUB_TOKEN="ghp_your_token_here"
# Build and push - no auth flags needed!pycontainer build \ --tag ghcr.io/your-username/myapp:v1.0.0 \ --pushOption 2: Command Line Flags
# Pass credentials directlypycontainer build \ --tag ghcr.io/your-username/myapp:v1.0.0 \ --password "ghp_your_token_here" \ --pushOption 3: Use Docker Login
# Login with docker firstecho "ghp_your_token_here" | docker login ghcr.io -u YOUR_USERNAME --password-stdin
# pycontainer will automatically read ~/.docker/config.jsonpycontainer build --tag ghcr.io/your-username/myapp:v1.0.0 --pushDocker Hub
Section titled “Docker Hub”Automatic via Docker Login (Recommended)
Section titled “Automatic via Docker Login (Recommended)”# Login with docker (credentials saved to ~/.docker/config.json)docker login
# pycontainer automatically reads the credentialspycontainer build --tag your-username/myapp:latest --pushEnvironment Variables
Section titled “Environment Variables”export REGISTRY_USERNAME="your_dockerhub_username"export REGISTRY_PASSWORD="your_dockerhub_token"
pycontainer build \ --tag docker.io/your-username/myapp:latest \ --pushCommand Line Flags
Section titled “Command Line Flags”pycontainer build \ --tag your-username/myapp:latest \ --username your_dockerhub_username \ --password your_dockerhub_token \ --pushAzure Container Registry (ACR)
Section titled “Azure Container Registry (ACR)”Automatic via Azure CLI (Recommended)
Section titled “Automatic via Azure CLI (Recommended)”# Login to Azureaz login
# pycontainer automatically gets tokens via 'az acr login --expose-token'pycontainer build \ --tag myregistry.azurecr.io/myapp:v1 \ --pushUsing Service Principal
Section titled “Using Service Principal”export REGISTRY_USERNAME="your_service_principal_id"export REGISTRY_PASSWORD="your_service_principal_password"
pycontainer build \ --tag myregistry.azurecr.io/myapp:v1 \ --pushUsing Admin Credentials
Section titled “Using Admin Credentials”# Get admin password from Azure portal or CLIpycontainer build \ --tag myregistry.azurecr.io/myapp:v1 \ --username myregistry \ --password "admin_password_from_portal" \ --pushLocal Registry (for Testing)
Section titled “Local Registry (for Testing)”Start Local Registry
Section titled “Start Local Registry”docker run -d -p 5000:5000 --name registry registry:2Push to Local Registry
Section titled “Push to Local Registry”The registry:2 container serves plain HTTP, so opt that exact host in:
# No authentication needed for local registrypycontainer build \ --tag localhost:5000/myapp:test \ --insecure-registry localhost:5000 \ --pushWithout --insecure-registry, pycontainer uses HTTPS and the push fails
with a TLS/network error.
Verify Push
Section titled “Verify Push”# List repositoriescurl http://localhost:5000/v2/_catalog
# List tagscurl http://localhost:5000/v2/myapp/tags/listInsecure (HTTP) Registries and Custom CAs
Section titled “Insecure (HTTP) Registries and Custom CAs”Registry traffic uses HTTPS with certificate verification by default. Two explicit settings change how a registry is reached; neither disables certificate verification, and there is no skip-verify option.
Plain HTTP for specific hosts
Section titled “Plain HTTP for specific hosts”pycontainer build --tag registry.lan:5000/team/app:dev \ --insecure-registry registry.lan:5000 --push[build]insecure-registries = ["localhost:5000", "registry.lan:5000"]- Entries are exact
host[:port]values with no scheme, path, or credentials (IPv6 literals must be bracketed, e.g.[::1]:5000). Matching is case-insensitive and must equal the registry in the image reference, including the port. Invalid entries are configuration errors. - Listed hosts use HTTP only; there is no HTTPS probe or fallback. Unlisted hosts keep HTTPS and fail with the usual TLS/network error if the server only speaks HTTP.
- Each build logs
WARNING: using plain HTTP for insecure registry ...once per insecure host that it contacts (base-image pull or push). - Credentials are never sent over HTTP to non-loopback hosts. Only
localhost,127.0.0.0/8,::1, and IPv4-mapped loopback literals may receive Basic or bearer credentials over HTTP. For any other HTTP registry, configured credentials (CLI, environment, Docker config, or Azure CLI) cause an explicitRegistryAuthenticationError, and a token challenge from that registry is refused before any token request is made. Such registries work only anonymously. - HTTP traffic is unencrypted and unauthenticated: anyone on the network path can read or alter image content. Use it only on trusted networks.
--insecure-registry on the command line replaces, rather than extends, the
[build] list.
Additional CA for private HTTPS registries
Section titled “Additional CA for private HTTPS registries”pycontainer build --tag registry.corp:443/team/app:1.0 \ --registry-ca-file ./corp-root-ca.pem --push[build]registry-ca-file = "corp-root-ca.pem"- The PEM bundle is added to the system trust store for all registry HTTPS traffic in the build: registry API calls, token realms, and blob storage redirects. Hostname and chain verification remain enabled.
- Relative paths resolve from the current working directory. A missing, unreadable, or invalid file is a configuration error.
- There is one bundle per build; per-registry CA mapping and client certificates (mTLS) are not supported.
Redirect and transport rules
Section titled “Redirect and transport rules”- HTTPS responses may not redirect to HTTP (manifests, blobs, token realms, and upload locations); the downgrade is rejected before the HTTP target is contacted.
- An HTTPS registry must advertise an HTTPS token realm.
- Initial requests, token realms, upload locations and redirects reject unsupported schemes, malformed authorities/ports (including port zero), whitespace/control characters, invalid percent escapes, embedded credentials, and fragments. Error URLs omit userinfo, queries and fragments; unparseable URLs are reported with a fixed placeholder.
- Registry control requests retain
Authorization,Proxy-Authorization, andCookieon same-origin redirects. These headers are removed when a redirect changes scheme, host, or effective port (an omitted port equals its scheme default). Token-exchange and blob-storage redirects are always fetched without credentials. - Each client reuses one verifying TLS context and request opener. Its non-redirecting blob opener is created once under a lock on first pull, using that same context; push-only clients never construct it. Concurrent initial bearer challenges share one token acquisition and replay once. Rejection of the current bearer token is reported, not refreshed indefinitely.
- Manifest responses are read at most to the descriptor’s expected size, or
16 MiB when the size is unknown (never more than 16 MiB), including after a
bearer-challenge replay; larger responses fail with
RegistryRequestError.
Proxy policy
Section titled “Proxy policy”Registry connections are direct. Both request openers explicitly disable urllib
environment and system proxy discovery, so HTTP_PROXY, HTTPS_PROXY,
ALL_PROXY, and their lowercase variants do not route registry, token or
storage requests. Proxy URLs and proxy credentials are not supported.
This applies to both HTTP and HTTPS, including custom-CA connections.
Limitations
Section titled “Limitations”- Tests cover an in-process loopback Registry v2 stub over HTTP and a
test-generated CA over HTTPS, including token acquisition and manifest/blob
redirect targets succeeding with the CA and failing without it. Regressions
cover concurrent first-use, opener reuse and token challenge replay.
Compatibility with specific registry products
(for example
registry:2, Harbor, Nexus, or Artifactory) over HTTP or with private CAs has not been verified in CI. - The Poetry, Hatch, and VS Code integrations do not expose these options;
use the CLI or
pycontainer.toml.
Base-Image Registry Mirrors
Section titled “Base-Image Registry Mirrors”Base-image pulls can go through pull-through mirrors before the upstream
registry. Mirrors never apply to --push, which always goes straight to the
registry named in --tag.
pycontainer build --tag ghcr.io/me/app:v1 --base-image python:3.11-slim \ --registry-mirror docker.io=mirror.example.com \ --registry-mirror docker.io=backup.example.com:5000[build.registry_mirrors]"docker.io" = ["mirror.example.com", "backup.example.com:5000"]"ghcr.io" = ["ghcr-mirror.example.com"]- Keys and values are
host[:port]only: no scheme, path, or credentials.docker.io,index.docker.io, andregistry-1.docker.ioare the same upstream. A mirror keeps the upstream repository path; Docker Hub official images keep thelibrary/prefix (library/python). Path-prefixed mirrors (for example Harbor proxy projects) are not supported. - Mirrors are tried in order, then the upstream.
--registry-mirrorfor an upstream replaces that upstream’s list frompycontainer.toml; lists for other upstreams are kept. - Mirrors use the same transport as any registry: HTTPS with
--registry-ca-filetrust, or plain HTTP only when the mirror host is also listed in--insecure-registry. --verbosenames the host(s) that served the pull (Served by: ...).
Fallback rules
Section titled “Fallback rules”| Mirror response | Behavior |
|---|---|
| Connection failure (including TLS verification and an unreachable token realm) | Warn; skip this mirror for the rest of the pull |
| HTTP 5xx after the normal retry budget (registry or token realm) | Warn; skip this mirror for the rest of the pull |
| HTTP 401/403 (including token exchange) | Warn about mirror credentials; skip this mirror for the rest of the pull |
| HTTP 404 | Warn; try the next host for this object only |
| HTTP 429 or quota errors | Fail; no fallback |
| Digest or size mismatch | Fail closed; no fallback, nothing published |
| Refused credentials or token realm (for example credentials over HTTP) | Fail; no fallback |
An error from the upstream, after every mirror is exhausted, is reported as it would be without mirrors.
Credentials and integrity
Section titled “Credentials and integrity”- Each mirror gets credentials only for its own host: its exact Docker
config entry, Azure CLI for
*.azurecr.io, orGITHUB_TOKENforghcr.io.REGISTRY_TOKEN,REGISTRY_USERNAME/REGISTRY_PASSWORD,--registry-*flags, and upstream credentials are never sent to a mirror. Upstream credentials are resolved only if the upstream is actually contacted. - Manifests requested by digest and every config/layer blob are verified against their descriptor digest and size, whichever host serves them.
- A tag is resolved by the mirror that serves it. Pin base images by digest
(
python:3.11-slim@sha256:...) if a mirror should not be trusted to resolve tags.
Tests use separate in-process loopback registries with synthetic credentials; no real mirror product (Docker Hub mirror, Harbor, Nexus, Artifactory, ACR cache rules) has been verified in CI.
Programmatic Usage
Section titled “Programmatic Usage”from pycontainer.builder import ImageBuilderfrom pycontainer.config import BuildConfigimport os
# Configure buildconfig = BuildConfig( tag="ghcr.io/user/myapp:v1.0.0", context_dir="/path/to/app", env={"ENV": "production"})
# Build imagebuilder = ImageBuilder(config)builder.build()
# Push to registryauth_token = os.getenv('GITHUB_TOKEN')builder.push(auth_token=auth_token, show_progress=True)Progress Output
Section titled “Progress Output”When pushing, pycontainer shows progress:
Pushing to ghcr.io/user/myapp:v1 Pushing layer 1/1 (sha256:787962fa701...) Pushing config (sha256:4988c2ac0a9...) Pushing manifest (sha256:f1cd9542ad3...)✓ Pushed ghcr.io/user/myapp:v1Use --no-progress to suppress:
pycontainer build --tag myapp:v1 --push --no-progressTroubleshooting
Section titled “Troubleshooting”Authentication Errors
Section titled “Authentication Errors”If you see 401 Unauthorized:
- Verify your token has correct permissions
- Check token is set in environment or ~/.docker/config.json
- For GHCR, ensure token has
write:packagesscope
Network Errors
Section titled “Network Errors”If you see connection timeouts:
- Check registry URL is correct
- Verify network connectivity
- Try with
--verboseflag (coming in Phase 1.4)
Blob Already Exists
Section titled “Blob Already Exists”This is normal! pycontainer checks if blobs exist before uploading:
Pushing layer 1/1 (sha256:787962fa701...) Layer exists, skippedThis makes subsequent pushes much faster.
Registry errors, rate limits and quotas
Section titled “Registry errors, rate limits and quotas”Registry failures report the operation, registry host and HTTP status. Distribution error codes and messages are used to classify failures, but transfer response bodies are omitted because they can echo image metadata:
Manifest pull failed with HTTP 429 from registry-1.docker.io: (response details omitted)The registry rate limit was exceeded;it resets in about 3600 seconds (at 2026-11-20T18:00:00Z). Rate limit: 0 of100 remaining. Authenticate to the registry ...TOOMANYREQUESTS/ HTTP 429 (RegistryRateLimitError): the reset time comes fromRetry-After. Sign in (Docker Hub grants higher pull limits to authenticated users), wait for the reset, or use a registry mirror. Only safe reads (GET/HEAD) have at most two transient retries per request, with delays capped at 5 seconds; whenRetry-Afteris longer than that cap the build fails immediately instead of retrying into the same limit. Uploads, upload initiation and manifestPUTs are not retried for transient errors; a request refused with a 401 bearer challenge can be replayed once after authentication. Invalid or negative numericRetry-Aftervalues use bounded backoff. Very large intervals are reported in seconds without a UTC date when the date is out of range.- HTTP 413, 507,
*QUOTA*codes or quota messages (RegistryQuotaError): the error names the rejected upload size. Free space in the repository, raise its quota or reduce the image size. DENIED/UNAUTHORIZED/ HTTP 401 or 403: check that the credentials can pull or push the repository. Some registries also report exhausted quotas asDENIED.
Run with --verbose to log Docker Hub style RateLimit-Limit and
RateLimit-Remaining headers. A warning is printed once per registry client
when the remaining count drops to 10 (or 10% of a smaller limit) or fewer.
Token-exchange error bodies are treated as untrusted: at most five errors are
shown, messages are truncated, control characters are removed, and credentials
are replaced with [REDACTED]. Registry hosts and HTTP(S) URLs in error
details omit userinfo and query strings.
Deleting and Pruning Images
Section titled “Deleting and Pruning Images”To delete images or prune old semantic versions from a registry, see the Registry Cleanup Guide. Pruning is a dry run by default.
Authentication Priority
Section titled “Authentication Priority”pycontainer tries authentication methods in this order:
- Command line flags (
--username,--password) - Environment variables (
GITHUB_TOKEN,REGISTRY_USERNAME,REGISTRY_PASSWORD) - Docker config (
~/.docker/config.json) - Azure CLI (for ACR registries via
az acr login --expose-token)
This means you can:
- Override any auto-detected credentials with CLI flags
- Set environment variables for CI/CD
- Use
docker loginfor local development - Seamlessly integrate with Azure CLI
Coming Soon (Phase 1.4)
Section titled “Coming Soon (Phase 1.4)”- Layer caching for faster incremental builds
- Content-addressable storage with cache eviction
--no-cacheflag to force full rebuild