Initial commit

This commit is contained in:
nemunaire 2021-05-02 13:47:08 +02:00
commit b03bc93a2f
5 changed files with 246 additions and 0 deletions

63
engine/docker/docker.go Normal file
View file

@ -0,0 +1,63 @@
package docker
import (
"context"
"io"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
oci "github.com/opencontainers/image-spec/specs-go/v1"
)
func Ps() ([]types.Container, error) {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, err
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
return nil, err
}
return containers, nil
}
func RunHello() (io.ReadCloser, error) {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, err
}
ctnr, err := cli.ContainerCreate(
context.Background(),
&container.Config{
AttachStdout: true,
AttachStderr: true,
Image: "hello-world",
},
&container.HostConfig{},
&network.NetworkingConfig{},
&oci.Platform{
Architecture: "amd64",
OS: "linux",
},
"",
)
if err != nil {
return nil, err
}
err = cli.ContainerStart(context.Background(), ctnr.ID, types.ContainerStartOptions{})
if err != nil {
return nil, err
}
return cli.ContainerLogs(context.Background(), ctnr.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
}