Skip to content

Recipes

Alice edited this page Feb 1, 2018 · 5 revisions

There are some clever things you can do with Cali commands beyond the basic running of containers, and this wiki page is a growing collection of these. Contributions to this wiki are welcome.

Set entrypoint

Setting the entrypoint in a Docker image makes the image run the given command, taking any additional parameters as flags for that command. Docker images provided by the community frequently don't use entrypoints like this, so we often build our own Docker images on top of the community ones with an entrypoint set.

However, we don't actually need to build a new image in order to set an entrypoint for Cali. We can just set it in the task configuration:

func init() {
	task := command.Task("maven")
	task.Conf.Entrypoint = []string{"mvn"}
}

Ports

Exposing ports for a container can be done with some extra code using go-connections.

import "github.com/docker/go-connections/nat"

func init() {
	command.Flags().StringP("port", "p", "8000", "Port to expose on host")
	command.BindFlags()
	task.SetInitFunc(func(t *cali.Task, args []string) {
		task.HostConf.PortBindings = nat.PortMap{
			nat.Port("8000/tcp"): []nat.PortBinding{
				{HostIP: "0.0.0.0", HostPort: cli.FlagValues().GetString("port")},
			},
		}
	})
}

Global flags

cali.CLI is also a cali.Command, which means you can set global flags for all commands to be able to use.

var (
    // Define this here, then all other files in cmd can add subcommands to it
    cli = cali.NewCli("lucli")
)

func init() {
    cli.SetShort("Example CLI tool")
    cli.SetLong("A nice long description of what your tool actually does")

    cli.Flags().StringP("bob", "r", "default", "Which bob should we use?")
    cli.BindFlags()
}

Mount volumes

It's really useful to be able to mount the user's dotfiles inside the containers you are using. So here's how you can mount the .netrc file for container applications to use.

import "path"
import "os/user"
func init() {
	u, err := user.Current()
	if err != nil {
		log.Fatalf("Failed to find uid for user: %s", err)
	}

	// Mount ~/.netrc for authentication
	netRc := path.Join(u.HomeDir, ".netrc")
	netRcBind, err := task.Bind(netRc, "/root/.netrc")
	if err != nil {
		log.Fatalf("Unable to bind ~/.netrc: %s", err)
	}
	task.AddBind(netRcBind)
}

Cali, the Meowmaid

Clone this wiki locally