Skip to main content

Go

Source code: github.com/oras-project/oras-go

Introduction

The ORAS Go client library provides the ability to replicate artifacts between different Targets.
Furthermore, the version v2 is a registry client conforming image-spec v1.1.0-rc.5 and distribution-spec v1.1.0-rc3.

Using the ORAS Go client library, you can develop your own registry client:

myclient push artifacts.example.com/myartifact:1.0 ./mything.thang

Usage

The package oras.land/oras-go/v2 can quickly be imported in other Go-based tools that wish to benefit from the ability to store arbitrary content in container registries.

  1. Get the oras.land/oras-go/v2 package
go get oras.land/oras-go/v2
  1. Import and use the v2 package
import "oras.land/oras-go/v2"
  1. Run
go mod tidy

The API documentation and examples are available at pkg.go.dev.

Quick Start

Push files to a remote repository

See example.

Pull files from a remote repository

See example.

Pull a docker or OCI image from a remote repository

See example.

Pull an Image using the Docker credential store

You can create a Docker credential store using oras-credential-go.

This enables you to use credentials that are saved by docker login in the client.

package main

import (
"context"
"fmt"

credentials "github.com/oras-project/oras-credentials-go"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/oci"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/retry"
)

func pullImage() error {

// 0. Create an OCI layout store
store, err := oci.New("/tmp/oci-layout-root")
if err != nil {
return err
}

// 1. Connect to a remote repository
ctx := context.Background()
reg := "docker.io"
repo, err := remote.NewRepository(reg + "/user/my-repo")
if err != nil {
return err
}

// 2. Get credentials from the docker credential store
storeOpts := credentials.StoreOptions{}
credStore, err := credentials.NewStoreFromDocker(storeOpts)
if err != nil {
return err
}

// Prepare the auth client for the registry and credential store
repo.Client = &auth.Client{
Client: retry.DefaultClient,
Cache: auth.DefaultCache,
Credential: credentials.Credential(credStore), // Use the credential store
}

// 3. Copy from the remote repository to the OCI layout store
tag := "latest"
manifestDescriptor, err := oras.Copy(ctx, repo, tag, store, tag, oras.DefaultCopyOptions)
if err != nil {
return err
}

fmt.Println("manifest pulled:", manifestDescriptor.Digest, manifestDescriptor.MediaType)

// 3. Fetch from OCI layout store to verify
fetched, err := content.FetchAll(ctx, store, manifestDescriptor)
if err != nil {
return err
}
fmt.Printf("manifest content:\n%s", fetched)
return nil
}

func main() {
if err := pullImage(); err != nil {
panic(err)
}
}