samalba / dockerclient

Docker client library in Go
http://www.docker.com/
Apache License 2.0
322 stars 140 forks source link

Example for docker run -v and -e #204

Closed aminjam closed 7 years ago

aminjam commented 8 years ago

I am looking for an example of using --volume with ContainerConfig. I see there is a Volumes map[string]struct{} in ContainerConfig, but how do I use it?

Also I can't find any example for using -e option. I have tried setting Env array item to be NAME=AJ. What should be the format for Env slice in ContainerConfig?

odewahn commented 8 years ago

I had the same problem with volumes. Here's an example based on https://github.com/drone/drone/blob/c613571bd641ce96b7bb80bf6395e561d3feb6aa/engine/runner/docker/util.go

    var binds []string
    volumes := map[string]struct{}{}

    // Set up a map where key is host dir and the value is the name on the container
    volumeDefinitions := make(map[string]string)
    volumeDefinitions["/Users/odewahn/Desktop/interferometry-demo"] = "/mymac"

    for hostPath, containerPath := range volumeDefinitions {
        binds = append(binds, hostPath+":"+containerPath)
        volumes[containerPath] = struct{}{}
    }

    // Define Binds in the HostConfig
    hostConfig := &dockerclient.HostConfig{
        Binds: binds,
    }

    // Define volumes in the ContainerConfig
    containerConfig := &dockerclient.ContainerConfig{
        Image:       "odewahn/interferometry-demo",
        Volumes:     volumes,
        AttachStdin: true,
        Tty:         true}

        // Create the container
    containerID, err := docker.CreateContainer(containerConfig, "foobar", nil)