Skip to content

Commit 2bd9147

Browse files
authored
Merge pull request #8 from jdolitsky/readme-updates
example of how to use as module
2 parents e8f753d + 4fcce65 commit 2bd9147

File tree

3 files changed

+116
-10
lines changed

3 files changed

+116
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ debug
2020

2121
# Custom
2222
coverage.*
23+
hello.txt

README.md

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,107 @@
11
# OCI Registry As Storage
2-
`oras` can push/pull any files from/to any registry with OCI support.
2+
`oras` can push/pull any files from/to any registry with OCI image support.
33

4-
If you are using [docker/distribution](https://github.com/docker/distribution), please make sure the version is `2.7.0` or above.
4+
Registries with known support:
55

6-
## Push files to remote registry
6+
- [Distribution](https://github.com/docker/distribution) (open source, version 2.7+)
7+
- [Azure Container Registry](https://azure.microsoft.com/en-us/services/container-registry/)
8+
9+
For more backgound on this topic, please see
10+
[this post](https://www.opencontainers.org/blog/2018/10/11/oci-image-support-comes-to-open-source-docker-registry).
11+
12+
## CLI
13+
14+
`oras` is a CLI that allows you to push and pull files from
15+
any registry with OCI image support.
16+
17+
### Push files to remote registry
718
```
819
oras push localhost:5000/hello:latest hello.txt
920
```
1021

11-
## Pull files from remote registry
22+
### Pull files from remote registry
1223
```
1324
oras pull localhost:5000/hello:latest
1425
```
1526

16-
## Login Credentials
27+
### Login Credentials
1728
`oras` uses the local docker credential by default. Therefore, please run `docker login` in advance for any private registries.
1829

1930
`oras` also accepts explicit credentials via options. For example,
2031
```
2132
oras pull -u username -p password myregistry.io/myimage:latest
2233
```
2334

24-
## Running in Docker
25-
### Build the image
35+
### Running in Docker
36+
#### Build the image
2637
```
2738
docker build -t oras .
2839
```
2940

30-
### Run on Linux
41+
#### Run on Linux
3142
```
3243
docker run --rm -it -v $(pwd):/workplace oras pull localhost:5000/hello:latest
3344
```
3445

35-
### Run on Windows PowerShell
46+
#### Run on Windows PowerShell
3647
```
3748
docker run --rm -it -v ${pwd}:/workplace oras pull localhost:5000/hello:latest
3849
```
3950

40-
### Run on Windows Commands
51+
#### Run on Windows Commands
4152
```
4253
docker run --rm -it -v %cd%:/workplace oras pull localhost:5000/hello:latest
4354
```
55+
56+
## Go Module
57+
58+
The package `github.com/shizhMSFT/oras/pkg/oras` can quickly be imported in other Go-based tools that
59+
wish to benefit from the ability to store arbitrary content in container registries.
60+
61+
Example:
62+
63+
[Source](examples/simple_push_pull.go)
64+
65+
```go
66+
package main
67+
68+
import (
69+
"context"
70+
"fmt"
71+
"github.com/containerd/containerd/remotes/docker"
72+
"github.com/shizhMSFT/oras/pkg/oras"
73+
"io/ioutil"
74+
)
75+
76+
func check(e error) {
77+
if e != nil {
78+
panic(e)
79+
}
80+
}
81+
82+
func main() {
83+
ref := "localhost:5000/oras:test"
84+
fileName := "hello.txt"
85+
fileContent := []byte("Hello World!\n")
86+
87+
ctx := context.Background()
88+
resolver := docker.NewResolver(docker.ResolverOptions{})
89+
90+
// Push file(s) to registry
91+
pushContents := make(map[string][]byte)
92+
pushContents[fileName] = fileContent
93+
fmt.Printf("Pushing %s to %s... ", fileName, ref)
94+
err := oras.Push(ctx, resolver, ref, pushContents)
95+
check(err)
96+
fmt.Println("success!")
97+
98+
// Pull file(s) from registry and save to disk
99+
fmt.Printf("Pulling from %s and saving to %s... ", ref, fileName)
100+
pullContents, err := oras.Pull(ctx, resolver, ref)
101+
check(err)
102+
err = ioutil.WriteFile(fileName, pullContents[fileName], 0644)
103+
check(err)
104+
fmt.Println("success!")
105+
fmt.Printf("Try running 'cat %s'\n", fileName)
106+
}
107+
```

examples/simple_push_pull.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/containerd/containerd/remotes/docker"
7+
"github.com/shizhMSFT/oras/pkg/oras"
8+
"io/ioutil"
9+
)
10+
11+
func check(e error) {
12+
if e != nil {
13+
panic(e)
14+
}
15+
}
16+
17+
func main() {
18+
ref := "localhost:5000/oras:test"
19+
fileName := "hello.txt"
20+
fileContent := []byte("Hello World!\n")
21+
22+
ctx := context.Background()
23+
resolver := docker.NewResolver(docker.ResolverOptions{})
24+
25+
// Push file(s) to registry
26+
pushContents := make(map[string][]byte)
27+
pushContents[fileName] = fileContent
28+
fmt.Printf("Pushing %s to %s... ", fileName, ref)
29+
err := oras.Push(ctx, resolver, ref, pushContents)
30+
check(err)
31+
fmt.Println("success!")
32+
33+
// Pull file(s) from registry and save to disk
34+
fmt.Printf("Pulling from %s and saving to %s... ", ref, fileName)
35+
pullContents, err := oras.Pull(ctx, resolver, ref)
36+
check(err)
37+
err = ioutil.WriteFile(fileName, pullContents[fileName], 0644)
38+
check(err)
39+
fmt.Println("success!")
40+
fmt.Printf("Try running 'cat %s'\n", fileName)
41+
}

0 commit comments

Comments
 (0)