使用golang docker API模拟`docker run` [英] Emulating `docker run` using the golang docker API

查看:690
本文介绍了使用golang docker API模拟`docker run`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能达到相同的效果?

  sudo docker run -it --rm --name my-python-container -v$ PWD:/ usr / src / myapp -w / usr / src / myapp python:2-slim python test.py 

使用Golang的Docker API吗?



要么 https://github.com/fsouza/go-dockerclient https: //github.com/samalba/dockerclient 很好。使用 github。解析方案 com / fsouza / go-dockerclient ,您必须首先创建一个容器,使用 CreateContainerOptions 可以通过命令行添加相同的选项。

  container,err:= client.CreateContainer(createContainerOptions)

获得容器后, a href =http://godoc.org/github.com/fsouza/go-dockerclient#Client.StartContainer =noreferrer>启动它,在 HostConfig

  client.StartContainer(container.ID,hostConfig)

要连接到容器的std io流,您需要使用 client.AttachToContainer ,并在 AttachToContinerOptions 中分配适当的流。

How can I achieve the equivalent of

sudo docker run -it --rm --name my-python-container -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:2-slim python test.py

using the Docker API for Golang?

Either https://github.com/fsouza/go-dockerclient or https://github.com/samalba/dockerclient is fine.

解决方案

Using github.com/fsouza/go-dockerclient, you have to first create a container, using the CreateContainerOptions to add the same options that you can via the command line.

container, err := client.CreateContainer(createContainerOptions)

Once you have the container, you start it, with any extra options or overrides in the HostConfig

client.StartContainer(container.ID, hostConfig)

To connect to the std io streams of a container, you need to use client.AttachToContainer, and assign the appropriate stream in the AttachToContinerOptions.

这篇关于使用golang docker API模拟`docker run`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆