本文整理汇总了Golang中github.com/samalba/dockerclient.DockerClient.CreateContainer方法的典型用法代码示例。如果您正苦于以下问题:Golang DockerClient.CreateContainer方法的具体用法?Golang DockerClient.CreateContainer怎么用?Golang DockerClient.CreateContainer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/samalba/dockerclient.DockerClient
的用法示例。
在下文中一共展示了DockerClient.CreateContainer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Exec
func (job *Job) Exec(docker *dockerclient.DockerClient,
w io.Writer) error {
containerConfig := &dockerclient.ContainerConfig{
Image: job.Image,
Cmd: []string{"/bin/bash", "-c", job.Cmd},
AttachStdout: true,
AttachStderr: true,
}
cid, err := docker.CreateContainer(containerConfig,
fmt.Sprintf("spot-trader-job-%d", job.ID))
if err != nil {
return fmt.Errorf("creating container: %v", err)
}
defer func() {
if err = docker.RemoveContainer(cid, false, false); err != nil {
log.Printf("error removing container: %v", err)
}
}()
options := &dockerclient.AttachOptions{Stdout: true, Stderr: true, Stream: true}
stdout, err := docker.AttachContainer(cid, options)
if err != nil {
return fmt.Errorf("attaching: %v", err)
}
err = docker.StartContainer(cid, nil)
if err != nil {
return fmt.Errorf("starting container: %v", err)
}
done := make(chan struct{})
go func() {
err := copyDockerOut(w, w, stdout)
if err != nil {
log.Println(err)
}
stdout.Close()
close(done)
}()
<-done
err = docker.StopContainer(cid, 5)
if err != nil {
log.Printf("error stopping container: %v",
err)
}
return nil
}
示例2: createContainer
func createContainer(docker *dockerclient.DockerClient, env []string, ctx *ContainerContext) (string, error) {
//Make a collection of exposed ports
var exposedPorts map[string]struct{}
exposedPorts = make(map[string]struct{})
for k, _ := range ctx.PortContext {
exposedPorts[k] = struct{}{}
}
log.Printf("Creating container with environment %v", env)
//Build the Docker container config from the configuration provided by the caller
containerConfig := dockerclient.ContainerConfig{
Image: ctx.ImageName,
Labels: ctx.Labels,
ExposedPorts: exposedPorts,
Env: env,
}
//Create the container
return docker.CreateContainer(&containerConfig, "")
}
示例3: startDevice
func startDevice(devType string, sn string, server string, docker *dc.DockerClient) error {
// try to start the associated docker container
var cfg dc.ContainerConfig
cfg.Image = "device-" + devType
cfg.Env = []string{"DEVICE_SN=" + sn, "DEVICE_SERVER=" + server}
cfg.Tty = true
cfg.AttachStdout = true
cfg.AttachStdin = false
cfg.AttachStderr = true
id, err := docker.CreateContainer(&cfg, "device"+sn)
if err != nil {
return err
}
var host dc.HostConfig
err = docker.StartContainer(id, &host)
return err
}
示例4: createTupperware
func createTupperware(i int, uri *url.URL, docker *dockerclient.DockerClient) {
defer wg.Done()
logrus.Infof("Giving tupperware container %d some spears", i)
// create the command flags to pass to ab
cmd := []string{
"ab",
"-c",
strconv.Itoa(concurrency),
"-n",
strconv.Itoa(requests),
"-m",
strings.ToUpper(method),
"-s",
strconv.Itoa(timeout),
"-v",
strconv.Itoa(verbosity),
"-f",
protocol,
}
if authHeader != "" {
cmd = append(cmd, []string{"-A", authHeader}...)
}
if proxyAuth != "" {
cmd = append(cmd, []string{"-P", proxyAuth}...)
}
if contentType != "" {
cmd = append(cmd, []string{"-T", contentType}...)
}
if timelimit > 0 {
cmd = append(cmd, []string{"-t", strconv.Itoa(timelimit)}...)
}
if len(headers) > 0 {
for _, header := range headers {
cmd = append(cmd, []string{"-H", header}...)
}
}
if len(cookies) > 0 {
for _, cookie := range cookies {
cmd = append(cmd, []string{"-C", cookie}...)
}
}
// append the uri to the cmd string
// make sure there is a trailing slash if none given
if uri.Path == "" {
uri.Path = "/"
}
cmd = append(cmd, uri.String())
// create the container
containerConfig := &dockerclient.ContainerConfig{
Image: "jess/ab",
Entrypoint: []string{"top"},
}
name := fmt.Sprintf("tws_%d", i)
id, err := docker.CreateContainer(containerConfig, name)
if err != nil {
logrus.Errorf("Error while creating container (%s): %v", name, err)
return
}
containers = append(containers, id)
// start the container
hostConfig := &dockerclient.HostConfig{}
if err = docker.StartContainer(id, hostConfig); err != nil {
logrus.Errorf("Error while starting container (%s): %v", name, err)
return
}
// we have to start the container _before_ adding the new default gateway
// for outbound traffic, its unfortunate but yeah we need the pid of the process
if cidr != "" {
// get the pid of the container
info, err := docker.InspectContainer(id)
if err != nil {
logrus.Errorf("Error while inspecting container (%s): %v", name, err)
return
}
pid := info.State.Pid
nsPidPath := path.Join(netnsPath, strconv.Itoa(pid))
// defer removal of the pid from /var/run/netns
defer os.RemoveAll(nsPidPath)
// create a symlink from proc to the netns pid
procPidPath := path.Join("/proc", strconv.Itoa(pid), "ns", "net")
if err := os.Symlink(procPidPath, nsPidPath); err != nil {
logrus.Errorf("could not create symlink from %s to %s: %v", procPidPath, nsPidPath, err)
}
// create the veth pair and add to bridge
local, guest, err := ovs.CreateVethPair(bridge)
if err != nil {
logrus.Error(err)
return
}
//.........这里部分代码省略.........
示例5: startDocker
func startDocker(docker *dockerclient.DockerClient, s2 string, s3 string, s4 string) {
var dirpath string = "/tmp/shareDocker" + s3
os.RemoveAll(dirpath)
os.Mkdir(dirpath, 0777)
var filepath string = dirpath + "/config.ini"
//l4g.Info("startDocker -> %v", filepath)
var RS string = fmt.Sprintf("startDocker -> %v", filepath)
l4g.Info(RS)
writeSDLstr(RS)
f, _ := os.Create(filepath)
f.WriteString("ip_broker=rabbitmq\n")
f.Sync()
f.WriteString("\n")
f.Sync()
f.WriteString(s2)
f.Sync()
f.WriteString("\n")
defer f.Close()
//l4g.Info("Init the client")
RS = fmt.Sprintf("Init the client")
l4g.Info(RS)
writeSDLstr(RS)
l4g.Info(os.Getenv("DOCKER_HOST"))
//l4g.Info("DOCKER_HOST")
RS = fmt.Sprintf("DOCKER_HOST")
l4g.Info(RS)
writeSDLstr(RS)
var dockerName string = "docker" + s3
supprDocker(docker, dockerName)
//l4g.Info(" Create a container")
RS = fmt.Sprintf(" Create a container")
l4g.Info(RS)
writeSDLstr(RS)
containerConfig := &dockerclient.ContainerConfig{
Image: "hwgrepo:hwg",
User: "developer",
// Env: []string{"PATH=\/home\/developer\/sdl_sensor_broker\/"},
Cmd: []string{"sh"},
AttachStdin: false,
AttachStdout: false,
AttachStderr: false,
Tty: true,
OpenStdin: true,
StdinOnce: false,
}
containerId, err := docker.CreateContainer(containerConfig, dockerName)
if err != nil {
log.Fatal(err)
}
var port_tcp string = s3 + "/tcp"
portBinding := map[string][]dockerclient.PortBinding{port_tcp: {{"", s3}}}
var sharedDocker string = dirpath + ":/home/developer/sdl_sensor_broker/log"
l4g.Info(" Start the container")
hostConfig := &dockerclient.HostConfig{
Binds: []string{sharedDocker, "/tmp/.X11-unix:/tmp/.X11-unix:rw"},
Privileged: true,
PortBindings: portBinding,
Links: []string{"rabbitmq:rabbitmq"},
}
err = docker.StartContainer(containerId, hostConfig)
if err != nil {
log.Fatal(err)
}
execConfig := &dockerclient.ExecConfig{
AttachStdin: false,
AttachStdout: false,
AttachStderr: false,
Tty: true,
Cmd: []string{"sh", "runNoVNCplayer.sh", s4, s3},
Container: containerId,
Detach: true}
_, err = docker.Exec(execConfig)
if err != nil {
log.Fatal(err)
l4g.Info(" error exec container")
RS = fmt.Sprintf(" error exec container")
l4g.Info(RS)
writeSDLstr(RS)
}
}