Created
July 8, 2014 19:11
-
-
Save rochacon/07b6ba4a5b8d96949cbe to your computer and use it in GitHub Desktop.
Simple way to clone a container keeping its environment variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/fsouza/go-dockerclient" | |
"log" | |
"os" | |
) | |
func main() { | |
if len(os.Args) < 3 { | |
fmt.Printf("Usage: %s [ORIGINAL_CONTAINER_ID/NAME] [COMMAND...]\n", os.Args[0]) | |
fmt.Printf("Example: %s my-app /bin/bash\n", os.Args[0]) | |
os.Exit(1) | |
} | |
dcli, err := docker.NewClient("unix:///var/run/docker.sock") | |
if err != nil { | |
log.Fatal(err) | |
} | |
containerID := os.Args[1] | |
cmd := os.Args[2:] | |
baseContainer, err := dcli.InspectContainer(containerID) | |
if err != nil { | |
log.Fatal(err) | |
} | |
cloned := docker.CreateContainerOptions{ | |
"", | |
&docker.Config{ | |
AttachStdin: true, | |
AttachStdout: true, | |
AttachStderr: true, | |
Cmd: cmd, | |
Env: baseContainer.Config.Env, | |
Image: baseContainer.Config.Image, | |
OpenStdin: true, | |
Tty: true, | |
}, | |
} | |
container, err := dcli.CreateContainer(cloned) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = dcli.StartContainer(container.ID, container.HostConfig) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("Now attach to the container with:\ndocker attach", container.ID) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment