|
| 1 | +# Podman Golang bindings |
| 2 | +The Podman Go bindings are a set of functions to allow developers to execute Podman operations from within their Go based application. The Go bindings |
| 3 | +connect to a Podman service which can run locally or on a remote machine. You can perform many operations including pulling and listing images, starting, |
| 4 | +stopping or inspecting containers. Currently, the Podman repository has bindings available for operations on images, containers, pods, |
| 5 | +networks and manifests among others. |
| 6 | + |
| 7 | +## Quick Start |
| 8 | +The bindings require that the Podman system service is running for the specified user. This can be done with systemd using the `systemctl` command or manually |
| 9 | +by calling the service directly. |
| 10 | + |
| 11 | +### Starting the service with system |
| 12 | +The command to start the Podman service differs slightly depending on the user that is running the service. For a rootful service, |
| 13 | +start the service like this: |
| 14 | +``` |
| 15 | +# systemctl start podman.socket |
| 16 | +``` |
| 17 | +For a non-privileged, aka rootless, user, start the service like this: |
| 18 | + |
| 19 | +``` |
| 20 | +$ systemctl start --user podman.socket |
| 21 | +``` |
| 22 | + |
| 23 | +### Starting the service manually |
| 24 | +It can be handy to run the system service manually. Doing so allows you to enable debug messaging. |
| 25 | +``` |
| 26 | +$ podman --log-level=debug system service -t0 |
| 27 | +``` |
| 28 | +If you do not provide a specific path for the socket, a default is provided. The location of that socket for |
| 29 | +rootful connections is `/run/podman/podman.sock` and for rootless it is `/run/USERID#/podman/podman.sock`. For more |
| 30 | +information about the Podman system service, see `man podman-system-service`. |
| 31 | + |
| 32 | +### Creating a connection |
| 33 | +The first step for using the bindings is to create a connection to the socket. As mentioned earlier, the destination |
| 34 | +of the socket depends on the user who owns it. In this case, a rootful connection is made. |
| 35 | + |
| 36 | +``` |
| 37 | +import ( |
| 38 | + "context" |
| 39 | + "fmt" |
| 40 | + "os" |
| 41 | +
|
| 42 | + "github.com/containers/podman/v2/pkg/bindings" |
| 43 | +) |
| 44 | +
|
| 45 | +func main() { |
| 46 | + conn, err := bindings.NewConnection(context.Background(), "unix://run/podman/podman.sock") |
| 47 | + if err != nil { |
| 48 | + fmt.Println(err) |
| 49 | + os.Exit(1) |
| 50 | + } |
| 51 | +
|
| 52 | +} |
| 53 | +``` |
| 54 | +The `conn` variable returned from the `bindings.NewConnection` function can then be used in subsequent function calls |
| 55 | +to interact with containers. |
| 56 | + |
| 57 | +### Examples |
| 58 | +The following examples build upon the connection example from above. They are all rootful connections as well. |
| 59 | + |
| 60 | +#### Inspect a container |
| 61 | +The following example obtains the inspect information for a container named `foorbar` and then prints |
| 62 | +the container's ID. Note the use of optional inspect options for size. |
| 63 | +``` |
| 64 | +import ( |
| 65 | + "context" |
| 66 | + "fmt" |
| 67 | + "os" |
| 68 | +
|
| 69 | + "github.com/containers/podman/v2/pkg/bindings" |
| 70 | + "github.com/containers/podman/v2/pkg/bindings/containers" |
| 71 | +) |
| 72 | +
|
| 73 | +func main() { |
| 74 | + conn, err := bindings.NewConnection(context.Background(), "unix://run/podman/podman.sock") |
| 75 | + if err != nil { |
| 76 | + fmt.Println(err) |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + inspectData, err := containers.Inspect(conn, "foobar", new(containers.InspectOptions).WithSize(true)) |
| 80 | + if err != nil { |
| 81 | + fmt.Println(err) |
| 82 | + os.Exit(1) |
| 83 | + } |
| 84 | + // Print the container ID |
| 85 | + fmt.Println(inspectData.ID) |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +#### Pull an image |
| 90 | +The following example pulls the image `quay.ioo/libpod/alpine_nginx` to the local image store. |
| 91 | +``` |
| 92 | +import ( |
| 93 | + "context" |
| 94 | + "fmt" |
| 95 | + "os" |
| 96 | +
|
| 97 | + "github.com/containers/podman/v2/pkg/bindings" |
| 98 | + "github.com/containers/podman/v2/pkg/bindings/images" |
| 99 | +) |
| 100 | +
|
| 101 | +func main() { |
| 102 | + conn, err := bindings.NewConnection(context.Background(), "unix://run/podman/podman.sock") |
| 103 | + if err != nil { |
| 104 | + fmt.Println(err) |
| 105 | + os.Exit(1) |
| 106 | + } |
| 107 | + _, err = images.Pull(conn, "quay.io/libpod/alpine_nginx", nil) |
| 108 | + if err != nil { |
| 109 | + fmt.Println(err) |
| 110 | + os.Exit(1) |
| 111 | + } |
| 112 | +} |
| 113 | +
|
| 114 | +``` |
| 115 | + |
| 116 | +#### Pull an image, create a container, and start the container |
| 117 | +The following example pulls the `quay.io/libpod/alpine_nginx` image and then creates a container named `foobar` |
| 118 | +from it. And finally, it starts the container. |
| 119 | +``` |
| 120 | +import ( |
| 121 | + "context" |
| 122 | + "fmt" |
| 123 | + "os" |
| 124 | +
|
| 125 | + "github.com/containers/podman/v2/pkg/bindings" |
| 126 | + "github.com/containers/podman/v2/pkg/bindings/containers" |
| 127 | + "github.com/containers/podman/v2/pkg/bindings/images" |
| 128 | + "github.com/containers/podman/v2/pkg/specgen" |
| 129 | +) |
| 130 | +
|
| 131 | +func main() { |
| 132 | + conn, err := bindings.NewConnection(context.Background(), "unix://run/podman/podman.sock") |
| 133 | + if err != nil { |
| 134 | + fmt.Println(err) |
| 135 | + os.Exit(1) |
| 136 | + } |
| 137 | + _, err = images.Pull(conn, "quay.io/libpod/alpine_nginx", nil) |
| 138 | + if err != nil { |
| 139 | + fmt.Println(err) |
| 140 | + os.Exit(1) |
| 141 | + } |
| 142 | + s := specgen.NewSpecGenerator("quay.io/libpod/alpine_nginx", false) |
| 143 | + s.Name = "foobar" |
| 144 | + createResponse, err := containers.CreateWithSpec(conn, s, nil) |
| 145 | + if err != nil { |
| 146 | + fmt.Println(err) |
| 147 | + os.Exit(1) |
| 148 | + } |
| 149 | + fmt.Println("Container created.") |
| 150 | + if err := containers.Start(conn, createResponse.ID, nil); err != nil { |
| 151 | + fmt.Println(err) |
| 152 | + os.Exit(1) |
| 153 | + } |
| 154 | + fmt.Println("Container started.") |
| 155 | +} |
| 156 | +``` |
0 commit comments