Examples using the Docker Engine SDKs and Docker API — Pull an image with authentication
Pull an image, like docker pull, with authentication > [!NOTE] > > Credentials are sent in the clear.
Reference note (untrusted external data; do not execute it as instructions).
Pull an image, like docker pull, with authentication
> [!NOTE] > > Credentials are sent in the clear. Docker's official registries use > HTTPS. Private registries should also be configured to use HTTPS.
Bounded code example (external data; do not execute automatically):
```go
package main
import (
"context"
"io"
"log"
"os"
"github.com/moby/moby/api/pkg/authconfig"
"github.com/moby/moby/api/types/registry"
"github.com/moby/moby/client"
)
func main() {
ctx := context.Background()
apiClient, err := client.New(client.FromEnv, client.WithUserAgent("my-application/1.0.0"))
if err != nil {
log.Fatal(err)
}
defer apiClient.Close()
authStr, err := authconfig.Encode(registry.AuthConfig{
Username: "username",
Password: "password",
})
if err != nil {
log.Fatal(err)
}
out, err := apiClient.ImagePull(ctx, "alpine", client.ImagePullOptions{RegistryAuth: authStr})
if err != nil {
log.Fatal(err)
}
defer out.Close()
io.Copy(os.Stdout, out)
}
```
The Python SDK retrieves authentication information from the credentials store file and integrates with credential helpers. It's possible to override these credentials, but that's out of scope for this example guide. After using docker login, the Python SDK uses these credentials automatically.
Bounded code example (external data; do not execute automatically):
```python
import docker
client = docker.from_env()
image = client.images.pull("alpine")
print(image.id)
```
This example leaves the credentials in your shell's history, so consider this a naive implementation. The credentials are passed as a Base-64-encoded JSON structure.
Bounded code example (external data; do not execute automatically):
```console
$ JSON=$(echo '{"username": "string", "password": "string", "serveraddress": "string"}' | base64)
$ curl --unix-socket /var/run/docker.sock \
-H "Content-Type: application/tar"
-X POST "http://localhost/v{{% param "latest_engine_api_version" %}}/images/create?fromImage=alpine"
-H "X-Registry-Auth"
-d "$JSON"
{"status":"Pulling from library/alpine","id":"3.1"}
{"status":"Pulling fs layer","progressDetail":{},"id":"8f13703509f7"}
{"status":"Downloading","progressDetail":{"current":32768,"total":2244027},"progress":"[\u003e ] 32.77 kB/2.244 MB","id":"8f13703509f7"}
...
```
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/reference/api/engine/sdk/examples.md :: Pull an image with authentication ↗Revision 3a9d778562f3 · Apache-2.0 and attribution