donglinyi4313 2015-09-23 17:38
浏览 50
已采纳

Libcontainer-在容器中运行多个进程

I am trying to implement something to the effect of docker run and docker exec using libcontainer.

I have been able to create a container and run a process inside it with the following code:

func Run(id string, s *specs.LinuxSpec, f *Factory) (int, error) {
    ...
    container, err := f.CreateContainer(id, config)
    if err != nil {
        return -1, err
    }

    process := newProcess(s.Process)
    tty, err := newTty(s.Process.Terminal, process, rootuid)
    defer tty.Close()
    if err != nil {
        return -1, err
    }

    defer func() {
        if derr := Destroy(container); derr != nil {
            err = derr
        }
    }()

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

    return handler.forward(process)
}

This works great (I believe), but the problem comes when I have to run another process(es) inside the same container. For example, a container is already running (the main process is running in foreground mode): How can I achieve what Docker allows you to do with docker exec?

I have the following code:

func Exec(container libcontainer.Container, process *libcontainer.Process, onData func(data []byte), onErr func(err error)) (int, error) {
    reader, writer := io.Pipe()
    process.Stdin = os.Stdin

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

    tty, err := newTty(true, process, rootuid)
    defer tty.Close()
    if err != nil {
        return -1, err
    }

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

    // Redirect process output
    process.Stdout = writer
    process.Stderr = writer

    // Todo: Fix this, it waits for the main process to exit before it starts
    if err := container.Start(process); err != nil {
        return -1, err
    }

    go func(reader io.Reader) {
        scanner := bufio.NewScanner(reader)
        for scanner.Scan() {
            onData(scanner.Bytes())
        }
        if err := scanner.Err(); err != nil {
            onErr(err)
        }
    }(reader)

    return handler.forward(process)
}

This also works, but the problem is: It waits for the main process to exit before it runs. Sometimes it runs, but my memory goes to 100% after calling that function 5 - 7 times running a simple whoami command.

I'm pretty sure I am doing something(s) wrong, I just don't know what. Or is my understanding of containers failing me?

I used this project as a reference: https://github.com/opencontainers/runc

  • 写回答

1条回答 默认 最新

  • drcrc28428 2015-09-23 18:24
    关注

    It's probably better to use docker as reference for your case, because it uses same libcontainer.Container objects for starting and exec new process in container. You can find code interacting with libcontainer here: https://github.com/docker/docker/tree/master/daemon/execdriver/native

    Also it's better to post whole code, so people could try and debug it to help you.

    EDIT: Here is example code for running multiple containers: https://gist.github.com/anonymous/407eb530c0cb6c87ec9f

    I runned it like

    go run procs.go path-to-busybox
    

    You can see with ps that there are indeed multiple processes in container. Feel free to ask if you have any questions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog