Skip to content

Commit 4dcc046

Browse files
Merge pull request #1074 from datawolf/multi-containers
start multi-containers with `runc start` command
2 parents 509ddd6 + 74bfe50 commit 4dcc046

File tree

3 files changed

+85
-16
lines changed

3 files changed

+85
-16
lines changed

man/runc-start.8.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
runc start - start signals a created container to execute the user defined process
33

44
# SYNOPSIS
5-
runc start <container-id>
5+
runc start <container-id> [container-id...]
66

77
Where "<container-id>" is your name for the instance of the container that you
88
are starting. The name you provide for the container instance must be unique on

start.go

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/opencontainers/runc/libcontainer"
78
"github.com/urfave/cli"
@@ -10,30 +11,57 @@ import (
1011
var startCommand = cli.Command{
1112
Name: "start",
1213
Usage: "executes the user defined process in a created container",
13-
ArgsUsage: `<container-id>
14+
ArgsUsage: `<container-id> [container-id...]
1415
1516
Where "<container-id>" is your name for the instance of the container that you
1617
are starting. The name you provide for the container instance must be unique on
1718
your host.`,
18-
Description: `The start command executes the user defined process in a created container.`,
19+
Description: `The start command executes the user defined process in a created container .`,
1920
Action: func(context *cli.Context) error {
20-
container, err := getContainer(context)
21-
if err != nil {
22-
return err
21+
hasError := false
22+
if !context.Args().Present() {
23+
return fmt.Errorf("runc: \"start\" requires a minimum of 1 argument")
2324
}
24-
status, err := container.Status()
25+
26+
factory, err := loadFactory(context)
2527
if err != nil {
2628
return err
2729
}
28-
switch status {
29-
case libcontainer.Created:
30-
return container.Exec()
31-
case libcontainer.Stopped:
32-
return fmt.Errorf("cannot start a container that has run and stopped")
33-
case libcontainer.Running:
34-
return fmt.Errorf("cannot start an already running container")
35-
default:
36-
return fmt.Errorf("cannot start a container in the %s state", status)
30+
31+
for _, id := range context.Args() {
32+
container, err := factory.Load(id)
33+
if err != nil {
34+
fmt.Fprintf(os.Stderr, "container %s is not exist\n", id)
35+
hasError = true
36+
continue
37+
}
38+
status, err := container.Status()
39+
if err != nil {
40+
fmt.Fprintf(os.Stderr, "status for %s: %v\n", id, err)
41+
hasError = true
42+
continue
43+
}
44+
switch status {
45+
case libcontainer.Created:
46+
if err := container.Exec(); err != nil {
47+
fmt.Fprintf(os.Stderr, "start for %s failed: %v\n", id, err)
48+
hasError = true
49+
}
50+
case libcontainer.Stopped:
51+
fmt.Fprintln(os.Stderr, "cannot start a container that has run and stopped")
52+
hasError = true
53+
case libcontainer.Running:
54+
fmt.Fprintln(os.Stderr, "cannot start an already running container")
55+
hasError = true
56+
default:
57+
fmt.Fprintf(os.Stderr, "cannot start a container in the %s state\n", status)
58+
hasError = true
59+
}
60+
}
61+
62+
if hasError {
63+
return fmt.Errorf("one or more of container start failed")
3764
}
65+
return nil
3866
},
3967
}

tests/integration/start.bats

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bats
2+
3+
load helpers
4+
5+
function setup() {
6+
teardown_busybox
7+
setup_busybox
8+
}
9+
10+
function teardown() {
11+
teardown_busybox
12+
}
13+
14+
@test "runc start" {
15+
runc create --console /dev/pts/ptmx test_busybox1
16+
[ "$status" -eq 0 ]
17+
18+
testcontainer test_busybox1 created
19+
20+
runc create --console /dev/pts/ptmx test_busybox2
21+
[ "$status" -eq 0 ]
22+
23+
testcontainer test_busybox2 created
24+
25+
26+
# start conatiner test_busybox1 and test_busybox2
27+
runc start test_busybox1 test_busybox2
28+
[ "$status" -eq 0 ]
29+
30+
testcontainer test_busybox1 running
31+
testcontainer test_busybox2 running
32+
33+
# delete test_busybox1 and test_busybox2
34+
runc delete --force test_busybox1 test_busybox2
35+
36+
runc state test_busybox1
37+
[ "$status" -ne 0 ]
38+
39+
runc state test_busybox2
40+
[ "$status" -ne 0 ]
41+
}

0 commit comments

Comments
 (0)