yasko
yasko

Reputation: 397

docker stack deploy using the client api

I'm playing with docker's client api. I've seen how to run containers, pushing, pulling, etc. Now I want to deploy a stack with a compose file, but I don't think there's a one-func-call to do that (not in the client api anyway). I went trough docker's code and saw how they do it.

Is this the only way? I'm trying to keep the dependencies to a minimum, but if there aren't other options I guess I could live with it. Also I'm still pretty new to golang, so if someone could provide an example of how can I achieve docker stack deploy --compose-file file.yml though go code will be greatly appreciated.

Upvotes: 2

Views: 2683

Answers (1)

yasko
yasko

Reputation: 397

After some more research I think I have 3 options:

  1. just use os/exec package and exec.Command("docker", ...). This is ok, but it requires the docker client

  2. use the stuff provided by the docker/client package and implement the call yourself. This gives you the most control, but you need to implement composite calls (docker stack deploy) e.g. create images, networks, start containers, etc.

  3. use the commands provided by the docker/cli/command package. This way you also have access to some configs that could be overriden and you let the docker guys worry about composite calls.

I ended up using #3, here's my code:

import (
    "os"
    "github.com/docker/docker/cli/command"
    "github.com/docker/docker/cli/command/stack"
    "github.com/docker/docker/cli/flags"
)

func main() {
    cli := command.NewDockerCli(os.Stdin, os.Stdout, os.Stderr)
    cli.Initialize(flags.NewClientOptions())
    cmd := stack.NewStackCommand(cli)

    // the command package will pick up these, but you could override if you need to
    // cmd.SetArgs([]string{"deploy", "--compose-file", "compose.yml", "mystack"})

    cmd.Execute()
}

Upvotes: 4

Related Questions