Skip to content

Commit 35eea81

Browse files
authored
Merge pull request #4340 from apostasie/2025-06-as-a-lib-examples
Add example of using nerdctl as a library
2 parents 4c0dd00 + d53e06b commit 35eea81

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Using nerdctl as a library
2+
3+
This directory contains examples showing how to implement a cli communicating with containerd, using nerdctl as a library.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"encoding/json"
22+
"fmt"
23+
"os"
24+
25+
"github.com/containerd/nerdctl/v2/pkg/api/types"
26+
"github.com/containerd/nerdctl/v2/pkg/clientutil"
27+
nerdctl "github.com/containerd/nerdctl/v2/pkg/cmd/container"
28+
"github.com/containerd/nerdctl/v2/pkg/config"
29+
"github.com/containerd/nerdctl/v2/pkg/containerutil"
30+
"github.com/containerd/nerdctl/v2/pkg/logging"
31+
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
32+
)
33+
34+
func main() {
35+
// Implement logging
36+
if len(os.Args) == 3 && os.Args[1] == logging.MagicArgv1 {
37+
err := logging.Main(os.Args[2])
38+
if err != nil {
39+
fmt.Println(err)
40+
return
41+
}
42+
}
43+
44+
// Get options
45+
globalOpt := types.GlobalCommandOptions(*config.New())
46+
47+
// Rootless
48+
_ = rootlessutil.ParentMain(globalOpt.HostGatewayIP)
49+
50+
// Printout options for debug
51+
f, _ := json.MarshalIndent(globalOpt, "", " ")
52+
fmt.Printf("%s\n", f)
53+
54+
// Create container options
55+
createOpt := types.ContainerCreateOptions{
56+
GOptions: globalOpt,
57+
// TODO: this example should implement oci-hook as well instead of relying on nerdctl
58+
NerdctlCmd: "/usr/local/bin/nerdctl",
59+
Name: "my-container",
60+
Label: []string{},
61+
Cgroupns: "private",
62+
InRun: true,
63+
Rm: false,
64+
Pull: "missing",
65+
LogDriver: "json-file",
66+
StopSignal: "SIGTERM",
67+
Restart: "unless-stopped",
68+
Interactive: true,
69+
}
70+
71+
// Create client
72+
client, ctx, cancel, err := clientutil.NewClient(context.Background(), globalOpt.Namespace, globalOpt.Address)
73+
if err != nil {
74+
fmt.Println(err)
75+
return
76+
}
77+
defer cancel()
78+
79+
// Create network manager
80+
networkManager, err := containerutil.NewNetworkingOptionsManager(createOpt.GOptions, types.NetworkOptions{
81+
NetworkSlice: []string{"bridge"},
82+
}, client)
83+
84+
if err != nil {
85+
fmt.Println(err)
86+
return
87+
}
88+
89+
// Create container
90+
container, _, err := nerdctl.Create(ctx, client, []string{"debian"}, networkManager, createOpt)
91+
if err != nil {
92+
fmt.Println(err)
93+
return
94+
}
95+
96+
// Start container
97+
err = nerdctl.Start(ctx, client, []string{"my-container"}, types.ContainerStartOptions{
98+
Attach: true,
99+
Stdout: os.Stdout,
100+
})
101+
102+
if err != nil {
103+
fmt.Println(err)
104+
return
105+
}
106+
107+
cc, _ := json.MarshalIndent(container, "", " ")
108+
fmt.Println(string(cc))
109+
}

0 commit comments

Comments
 (0)