当前位置: 首页>>代码示例>>Golang>>正文


Golang Container.Start方法代码示例

本文整理汇总了Golang中github.com/opencontainers/runc/libcontainer.Container.Start方法的典型用法代码示例。如果您正苦于以下问题:Golang Container.Start方法的具体用法?Golang Container.Start怎么用?Golang Container.Start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/opencontainers/runc/libcontainer.Container的用法示例。


在下文中一共展示了Container.Start方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: runProcess

// runProcess will create a new process in the specified container
// by executing the process specified in the 'config'.
func runProcess(container libcontainer.Container, config *specs.Process, listenFDs []*os.File, console string, pidFile string, detach bool) (int, error) {
	process := newProcess(*config)

	// Add extra file descriptors if needed
	if len(listenFDs) > 0 {
		process.Env = append(process.Env, fmt.Sprintf("LISTEN_FDS=%d", len(listenFDs)), "LISTEN_PID=1")
		process.ExtraFiles = append(process.ExtraFiles, listenFDs...)
	}

	rootuid, err := container.Config().HostUID()
	if err != nil {
		return -1, err
	}

	tty, err := setupIO(process, rootuid, console, config.Terminal, detach)
	if err != nil {
		return -1, err
	}

	handler := newSignalHandler(tty)
	defer handler.Close()

	if err := container.Start(process); err != nil {
		if tty != nil {
			tty.Close()
		}
		return -1, err
	}

	if pidFile != "" {
		if err := createPidFile(pidFile, process); err != nil {
			process.Signal(syscall.SIGKILL)
			process.Wait()
			if tty != nil {
				tty.Close()
			}
			return -1, err
		}
	}
	if detach {
		return 0, nil
	}

	return handler.forward(process)
}
开发者ID:hustcat,项目名称:runc,代码行数:47,代码来源:utils.go

示例2: runCmdInDir

// runCmdInDir runs the given command inside a container under dir
func runCmdInDir(im *schema.ImageManifest, cmd, dir string, jail bool, mounts []*configs.Mount) error {
	exePath, err := osext.Executable()
	if err != nil {
		return fmt.Errorf("error getting path to the current executable: %v", err)
	}
	factory, err := libcontainer.New(dir, libcontainer.InitArgs(exePath, "init"))
	if err != nil {
		return fmt.Errorf("error creating a container factory: %v", err)
	}

	// The containter ID doesn't really matter here... using a UUID
	containerID := uuid.NewV4().String()

	var container libcontainer.Container
	if jail {
		config := &configs.Config{}
		if err := json.Unmarshal([]byte(LibcontainerDefaultConfig), config); err != nil {
			return fmt.Errorf("error unmarshalling default config: %v", err)
		}
		config.Rootfs = dir
		config.Readonlyfs = false
		container, err = factory.Create(containerID, config)
		if err != nil {
			return fmt.Errorf("error creating a container: %v", err)
		}
	} else {
		container, err = factory.Create(containerID, &configs.Config{
			Rootfs: dir,
			Mounts: mounts,
			Cgroups: &configs.Cgroup{
				Name:            containerID,
				Parent:          "system",
				AllowAllDevices: false,
				AllowedDevices:  configs.DefaultAllowedDevices,
			},
		})
		if err != nil {
			return fmt.Errorf("error creating a container: %v", err)
		}
	}

	process := &libcontainer.Process{
		Args:   strings.Fields(cmd),
		User:   "root",
		Stdin:  os.Stdin,
		Stdout: os.Stdout,
		Stderr: os.Stderr,
	}

	if im.App != nil {
		process.Env = util.ACIEnvironmentToList(im.App.Environment)
	}
	process.Env = []string{"PATH=/usr/bin:/sbin/:/bin"}

	if err := container.Start(process); err != nil {
		return fmt.Errorf("error starting the process inside the container: %v", err)
	}

	_, err = process.Wait()
	if err != nil {
		return fmt.Errorf("error running the process: %v", err)
	}

	if err := container.Destroy(); err != nil {
		return fmt.Errorf("error destroying the container: %v", err)
	}

	return nil
}
开发者ID:klizhentas,项目名称:acbuild,代码行数:70,代码来源:exec.go


注:本文中的github.com/opencontainers/runc/libcontainer.Container.Start方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。