本文整理汇总了Golang中github.com/docker/docker/runconfig.NewCommand函数的典型用法代码示例。如果您正苦于以下问题:Golang NewCommand函数的具体用法?Golang NewCommand怎么用?Golang NewCommand使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewCommand函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: commit
func (b *Builder) commit(id string, autoCmd *runconfig.Command, comment string) error {
if b.disableCommit {
return nil
}
if b.image == "" && !b.noBaseImage {
return fmt.Errorf("Please provide a source image with `from` prior to commit")
}
b.Config.Image = b.image
if id == "" {
cmd := b.Config.Cmd
if runtime.GOOS != "windows" {
b.Config.Cmd = runconfig.NewCommand("/bin/sh", "-c", "#(nop) "+comment)
} else {
b.Config.Cmd = runconfig.NewCommand("cmd", "/S /C", "REM (nop) "+comment)
}
defer func(cmd *runconfig.Command) { b.Config.Cmd = cmd }(cmd)
hit, err := b.probeCache()
if err != nil {
return err
}
if hit {
return nil
}
container, err := b.create()
if err != nil {
return err
}
id = container.ID
if err := container.Mount(); err != nil {
return err
}
defer container.Unmount()
}
container, err := b.Daemon.Get(id)
if err != nil {
return err
}
// Note: Actually copy the struct
autoConfig := *b.Config
autoConfig.Cmd = autoCmd
commitCfg := &daemon.ContainerCommitConfig{
Author: b.maintainer,
Pause: true,
Config: &autoConfig,
}
// Commit the container
image, err := b.Daemon.Commit(container, commitCfg)
if err != nil {
return err
}
b.image = image.ID
return nil
}
示例2: BenchmarkRunSequential
func BenchmarkRunSequential(b *testing.B) {
daemon := mkDaemon(b)
defer nuke(daemon)
for i := 0; i < b.N; i++ {
container, _, err := daemon.Create(&runconfig.Config{
Image: GetTestImage(daemon).ID,
Cmd: runconfig.NewCommand("echo", "-n", "foo"),
},
&runconfig.HostConfig{},
"",
)
if err != nil {
b.Fatal(err)
}
defer daemon.Rm(container)
output, err := container.Output()
if err != nil {
b.Fatal(err)
}
if string(output) != "foo" {
b.Fatalf("Unexpected output: %s", output)
}
if err := daemon.Rm(container); err != nil {
b.Fatal(err)
}
}
}
示例3: TestPostContainersWait
func TestPostContainersWait(t *testing.T) {
eng := NewTestEngine(t)
defer mkDaemonFromEngine(eng, t).Nuke()
containerID := createTestContainer(eng,
&runconfig.Config{
Image: unitTestImageID,
Cmd: runconfig.NewCommand("/bin/sleep", "1"),
OpenStdin: true,
},
t,
)
startContainer(eng, containerID, t)
setTimeout(t, "Wait timed out", 3*time.Second, func() {
r := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/containers/"+containerID+"/wait", bytes.NewReader([]byte{}))
if err != nil {
t.Fatal(err)
}
server.ServeRequest(eng, api.APIVERSION, r, req)
assertHttpNotError(r, t)
var apiWait engine.Env
if err := apiWait.Decode(r.Body); err != nil {
t.Fatal(err)
}
if apiWait.GetInt("StatusCode") != 0 {
t.Fatalf("Non zero exit code for sleep: %d\n", apiWait.GetInt("StatusCode"))
}
})
if containerRunning(eng, containerID, t) {
t.Fatalf("The container should be stopped after wait")
}
}
示例4: cmd
// CMD foo
//
// Set the default command to run in the container (which may be empty).
// Argument handling is the same as RUN.
//
func cmd(b *builder, args []string, attributes map[string]bool, original string) error {
if err := b.BuilderFlags.Parse(); err != nil {
return err
}
cmdSlice := handleJSONArgs(args, attributes)
if !attributes["json"] {
if runtime.GOOS != "windows" {
cmdSlice = append([]string{"/bin/sh", "-c"}, cmdSlice...)
} else {
cmdSlice = append([]string{"cmd", "/S /C"}, cmdSlice...)
}
}
b.Config.Cmd = runconfig.NewCommand(cmdSlice...)
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("CMD %q", cmdSlice)); err != nil {
return err
}
if len(args) != 0 {
b.cmdSet = true
}
return nil
}
示例5: TestPostContainersCopy
func TestPostContainersCopy(t *testing.T) {
eng := NewTestEngine(t)
defer mkDaemonFromEngine(eng, t).Nuke()
// Create a container and remove a file
containerID := createTestContainer(eng,
&runconfig.Config{
Image: unitTestImageID,
Cmd: runconfig.NewCommand("touch", "/test.txt"),
},
t,
)
containerRun(eng, containerID, t)
r := httptest.NewRecorder()
var copyData engine.Env
copyData.Set("Resource", "/test.txt")
copyData.Set("HostPath", ".")
jsonData := bytes.NewBuffer(nil)
if err := copyData.Encode(jsonData); err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", "/containers/"+containerID+"/copy", jsonData)
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/json")
server.ServeRequest(eng, api.APIVERSION, r, req)
assertHttpNotError(r, t)
if r.Code != http.StatusOK {
t.Fatalf("%d OK expected, received %d\n", http.StatusOK, r.Code)
}
found := false
for tarReader := tar.NewReader(r.Body); ; {
h, err := tarReader.Next()
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
if h.Name == "test.txt" {
found = true
break
}
}
if !found {
t.Fatalf("The created test file has not been found in the copied output")
}
}
示例6: create
func (b *Builder) create() (*daemon.Container, error) {
if b.image == "" && !b.noBaseImage {
return nil, fmt.Errorf("Please provide a source image with `from` prior to run")
}
b.Config.Image = b.image
config := *b.Config
// Create the Pod
podId := fmt.Sprintf("buildpod-%s", utils.RandStr(10, "alpha"))
podString, err := MakeBasicPod(podId, b.image, b.Config.Cmd.Slice())
if err != nil {
return nil, err
}
err = b.Hyperdaemon.CreatePod(podId, podString, false)
if err != nil {
return nil, err
}
// Get the container
var (
containerId = ""
c *daemon.Container
)
ps, ok := b.Hyperdaemon.PodList.GetStatus(podId)
if !ok {
return nil, fmt.Errorf("Cannot find pod %s", podId)
}
for _, i := range ps.Containers {
containerId = i.Id
}
c, err = b.Daemon.Get(containerId)
if err != nil {
glog.Error(err.Error())
return nil, err
}
b.TmpContainers[c.ID] = struct{}{}
b.TmpPods[podId] = struct{}{}
fmt.Fprintf(b.OutStream, " ---> Running in %s\n", stringid.TruncateID(c.ID))
if config.Cmd.Len() > 0 {
// override the entry point that may have been picked up from the base image
s := config.Cmd.Slice()
c.Path = s[0]
c.Args = s[1:]
} else {
config.Cmd = runconfig.NewCommand()
}
return c, nil
}
示例7: BenchmarkRunParallel
func BenchmarkRunParallel(b *testing.B) {
daemon := mkDaemon(b)
defer nuke(daemon)
var tasks []chan error
for i := 0; i < b.N; i++ {
complete := make(chan error)
tasks = append(tasks, complete)
go func(i int, complete chan error) {
container, _, err := daemon.Create(&runconfig.Config{
Image: GetTestImage(daemon).ID,
Cmd: runconfig.NewCommand("echo", "-n", "foo"),
},
&runconfig.HostConfig{},
"",
)
if err != nil {
complete <- err
return
}
defer daemon.Rm(container)
if err := container.Start(); err != nil {
complete <- err
return
}
if _, err := container.WaitStop(15 * time.Second); err != nil {
complete <- err
return
}
// if string(output) != "foo" {
// complete <- fmt.Errorf("Unexecpted output: %v", string(output))
// }
if err := daemon.Rm(container); err != nil {
complete <- err
return
}
complete <- nil
}(i, complete)
}
var errors []error
for _, task := range tasks {
err := <-task
if err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
b.Fatal(errors)
}
}
示例8: TestPostContainersStop
func TestPostContainersStop(t *testing.T) {
eng := NewTestEngine(t)
defer mkDaemonFromEngine(eng, t).Nuke()
containerID := createTestContainer(eng,
&runconfig.Config{
Image: unitTestImageID,
Cmd: runconfig.NewCommand("/bin/top"),
OpenStdin: true,
},
t,
)
startContainer(eng, containerID, t)
// Give some time to the process to start
containerWaitTimeout(eng, containerID, t)
if !containerRunning(eng, containerID, t) {
t.Errorf("Container should be running")
}
// Note: as it is a POST request, it requires a body.
req, err := http.NewRequest("POST", "/containers/"+containerID+"/stop?t=1", bytes.NewReader([]byte{}))
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
server.ServeRequest(eng, api.APIVERSION, r, req)
assertHttpNotError(r, t)
if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
}
if containerRunning(eng, containerID, t) {
t.Fatalf("The container hasn't been stopped")
}
req, err = http.NewRequest("POST", "/containers/"+containerID+"/stop?t=1", bytes.NewReader([]byte{}))
if err != nil {
t.Fatal(err)
}
r = httptest.NewRecorder()
server.ServeRequest(eng, api.APIVERSION, r, req)
// Stopping an already stopper container should return a 304
assertHttpNotError(r, t)
if r.Code != http.StatusNotModified {
t.Fatalf("%d NOT MODIFIER expected, received %d\n", http.StatusNotModified, r.Code)
}
}
示例9: ContainerExecCreate
func (d *Daemon) ContainerExecCreate(job *engine.Job) error {
if len(job.Args) != 1 {
return fmt.Errorf("Usage: %s [options] container command [args]", job.Name)
}
if strings.HasPrefix(d.execDriver.Name(), lxc.DriverName) {
return lxc.ErrExec
}
var name = job.Args[0]
container, err := d.getActiveContainer(name)
if err != nil {
return err
}
config, err := runconfig.ExecConfigFromJob(job)
if err != nil {
return err
}
cmd := runconfig.NewCommand(config.Cmd...)
entrypoint, args := d.getEntrypointAndArgs(runconfig.NewEntrypoint(), cmd)
processConfig := execdriver.ProcessConfig{
Tty: config.Tty,
Entrypoint: entrypoint,
Arguments: args,
User: config.User,
Privileged: config.Privileged,
}
execConfig := &execConfig{
ID: stringid.GenerateRandomID(),
OpenStdin: config.AttachStdin,
OpenStdout: config.AttachStdout,
OpenStderr: config.AttachStderr,
StreamConfig: StreamConfig{},
ProcessConfig: processConfig,
Container: container,
Running: false,
}
container.LogEvent("exec_create: " + execConfig.ProcessConfig.Entrypoint + " " + strings.Join(execConfig.ProcessConfig.Arguments, " "))
d.registerExecCommand(execConfig)
job.Printf("%s\n", execConfig.ID)
return nil
}
示例10: TestPostContainersStart
func TestPostContainersStart(t *testing.T) {
eng := NewTestEngine(t)
defer mkDaemonFromEngine(eng, t).Nuke()
containerID := createTestContainer(
eng,
&runconfig.Config{
Image: unitTestImageID,
Cmd: runconfig.NewCommand("/bin/cat"),
OpenStdin: true,
},
t,
)
hostConfigJSON, err := json.Marshal(&runconfig.HostConfig{})
req, err := http.NewRequest("POST", "/containers/"+containerID+"/start", bytes.NewReader(hostConfigJSON))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
r := httptest.NewRecorder()
server.ServeRequest(eng, api.APIVERSION, r, req)
assertHttpNotError(r, t)
if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
}
containerAssertExists(eng, containerID, t)
req, err = http.NewRequest("POST", "/containers/"+containerID+"/start", bytes.NewReader(hostConfigJSON))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
r = httptest.NewRecorder()
server.ServeRequest(eng, api.APIVERSION, r, req)
// Starting an already started container should return a 304
assertHttpNotError(r, t)
if r.Code != http.StatusNotModified {
t.Fatalf("%d NOT MODIFIER expected, received %d\n", http.StatusNotModified, r.Code)
}
containerAssertExists(eng, containerID, t)
containerKill(eng, containerID, t)
}
示例11: commit
func (b *Builder) commit(id string, autoCmd *runconfig.Command, comment string) error {
if b.disableCommit {
return nil
}
if b.image == "" && !b.noBaseImage {
return fmt.Errorf("Please provide a source image with `from` prior to commit")
}
b.Config.Image = b.image
if id == "" {
cmd := b.Config.Cmd
b.Config.Cmd = runconfig.NewCommand("/bin/sh", "-c", "#(nop) "+comment)
defer func(cmd *runconfig.Command) { b.Config.Cmd = cmd }(cmd)
hit, err := b.probeCache()
if err != nil {
return err
}
if hit {
return nil
}
container, err := b.create()
if err != nil {
return err
}
id = container.ID
if err := container.Mount(); err != nil {
return err
}
defer container.Unmount()
}
container, err := b.Daemon.Get(id)
if err != nil {
return err
}
// Note: Actually copy the struct
autoConfig := *b.Config
autoConfig.Cmd = autoCmd
// Commit the container
image, err := b.Daemon.Commit(container, "", "", "", b.maintainer, true, &autoConfig)
if err != nil {
return err
}
b.image = image.ID
return nil
}
示例12: ContainerExecCreate
func (d *Daemon) ContainerExecCreate(config *runconfig.ExecConfig) (string, error) {
// Not all drivers support Exec (LXC for example)
if err := checkExecSupport(d.execDriver.Name()); err != nil {
return "", err
}
container, err := d.getActiveContainer(config.Container)
if err != nil {
return "", err
}
cmd := runconfig.NewCommand(config.Cmd...)
entrypoint, args := d.getEntrypointAndArgs(runconfig.NewEntrypoint(), cmd)
user := config.User
if len(user) == 0 {
user = container.Config.User
}
processConfig := &execdriver.ProcessConfig{
Tty: config.Tty,
Entrypoint: entrypoint,
Arguments: args,
User: user,
Privileged: config.Privileged,
}
execConfig := &execConfig{
ID: stringid.GenerateNonCryptoID(),
OpenStdin: config.AttachStdin,
OpenStdout: config.AttachStdout,
OpenStderr: config.AttachStderr,
StreamConfig: StreamConfig{},
ProcessConfig: processConfig,
Container: container,
Running: false,
waitStart: make(chan struct{}),
}
d.registerExecCommand(execConfig)
container.LogEvent("exec_create: " + execConfig.ProcessConfig.Entrypoint + " " + strings.Join(execConfig.ProcessConfig.Arguments, " "))
return execConfig.ID, nil
}
示例13: cmd
// CMD foo
//
// Set the default command to run in the container (which may be empty).
// Argument handling is the same as RUN.
//
func cmd(b *Builder, args []string, attributes map[string]bool, original string) error {
cmdSlice := handleJsonArgs(args, attributes)
if !attributes["json"] {
cmdSlice = append([]string{"/bin/sh", "-c"}, cmdSlice...)
}
b.Config.Cmd = runconfig.NewCommand(cmdSlice...)
if err := b.commit("", b.Config.Cmd, fmt.Sprintf("CMD %q", cmdSlice)); err != nil {
return err
}
if len(args) != 0 {
b.cmdSet = true
}
return nil
}
示例14: create
func (b *Builder) create() (*daemon.Container, error) {
if b.image == "" && !b.noBaseImage {
return nil, fmt.Errorf("Please provide a source image with `from` prior to run")
}
b.Config.Image = b.image
hostConfig := &runconfig.HostConfig{
CpuShares: b.cpuShares,
CpuPeriod: b.cpuPeriod,
CpuQuota: b.cpuQuota,
CpusetCpus: b.cpuSetCpus,
CpusetMems: b.cpuSetMems,
CgroupParent: b.cgroupParent,
Memory: b.memory,
MemorySwap: b.memorySwap,
NetworkMode: "bridge",
}
config := *b.Config
// Create the container
c, warnings, err := b.Daemon.Create(b.Config, hostConfig, "")
if err != nil {
return nil, err
}
for _, warning := range warnings {
fmt.Fprintf(b.OutStream, " ---> [Warning] %s\n", warning)
}
b.TmpContainers[c.ID] = struct{}{}
fmt.Fprintf(b.OutStream, " ---> Running in %s\n", stringid.TruncateID(c.ID))
if config.Cmd.Len() > 0 {
// override the entry point that may have been picked up from the base image
s := config.Cmd.Slice()
c.Path = s[0]
c.Args = s[1:]
} else {
config.Cmd = runconfig.NewCommand()
}
return c, nil
}
示例15: TestPostContainersRestart
func TestPostContainersRestart(t *testing.T) {
eng := NewTestEngine(t)
defer mkDaemonFromEngine(eng, t).Nuke()
containerID := createTestContainer(eng,
&runconfig.Config{
Image: unitTestImageID,
Cmd: runconfig.NewCommand("/bin/top"),
OpenStdin: true,
},
t,
)
startContainer(eng, containerID, t)
// Give some time to the process to start
containerWaitTimeout(eng, containerID, t)
if !containerRunning(eng, containerID, t) {
t.Errorf("Container should be running")
}
req, err := http.NewRequest("POST", "/containers/"+containerID+"/restart?t=1", bytes.NewReader([]byte{}))
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
server.ServeRequest(eng, api.APIVERSION, r, req)
assertHttpNotError(r, t)
if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
}
// Give some time to the process to restart
containerWaitTimeout(eng, containerID, t)
if !containerRunning(eng, containerID, t) {
t.Fatalf("Container should be running")
}
containerKill(eng, containerID, t)
}