Skip to content

Commit

Permalink
Add docker exec run a command in privileged mode
Browse files Browse the repository at this point in the history
Signed-off-by: Lei Jitang <[email protected]>
  • Loading branch information
coolljt0725 committed Apr 11, 2015
1 parent 2cce479 commit 72a500e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion contrib/completion/bash/docker
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ _docker_events() {
_docker_exec() {
case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--detach -d --help --interactive -i -t --tty -u --user" -- "$cur" ) )
COMPREPLY=( $( compgen -W "--detach -d --help --interactive -i --privileged -t --tty -u --user" -- "$cur" ) )
;;
*)
__docker_containers_running
Expand Down
1 change: 1 addition & 0 deletions daemon/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (d *Daemon) ContainerExecCreate(job *engine.Job) error {
Entrypoint: entrypoint,
Arguments: args,
User: config.User,
Privileged: config.Privileged,
}

execConfig := &execConfig{
Expand Down
5 changes: 4 additions & 1 deletion daemon/execdriver/native/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/docker/libcontainer/utils"
)

// TODO(vishh): Add support for running in privileged mode.
func (d *driver) Exec(c *execdriver.Command, processConfig *execdriver.ProcessConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
active := d.activeContainers[c.ID]
if active == nil {
Expand All @@ -31,6 +30,10 @@ func (d *driver) Exec(c *execdriver.Command, processConfig *execdriver.ProcessCo
User: processConfig.User,
}

if processConfig.Privileged {
p.Capabilities = execdriver.GetAllCapabilities()
}

if processConfig.Tty {
config := active.Config()
rootuid, err := config.HostUID()
Expand Down
8 changes: 8 additions & 0 deletions docs/man/docker-exec.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ docker-exec - Run a command in a running container
[**-d**|**--detach**[=*false*]]
[**--help**]
[**-i**|**--interactive**[=*false*]]
[**--privileged**[=*false*]]
[**-t**|**--tty**[=*false*]]
[**-u**|**--user**[=*USER*]]
CONTAINER COMMAND [ARG...]
Expand All @@ -33,6 +34,13 @@ container is unpaused, and then run
**-i**, **--interactive**=*true*|*false*
Keep STDIN open even if not attached. The default is *false*.

**--privileged**=*true*|*false*
Give extended privileges to the process to run in a running container. The default is *false*.

By default, the process run by docker exec in a running container
have the same capabilities of the container. By setting --privileged will give
all the capabilities to the process.

**-t**, **--tty**=*true*|*false*
Allocate a pseudo-TTY. The default is *false*.

Expand Down
1 change: 1 addition & 0 deletions docs/sources/reference/commandline/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ You'll need two shells for this example.

-d, --detach=false Detached mode: run command in the background
-i, --interactive=false Keep STDIN open even if not attached
--privileged=false Give extended privileges to the command
-t, --tty=false Allocate a pseudo-TTY
-u, --user= Username or UID (format: <name|uid>[:<group|gid>])

Expand Down
28 changes: 28 additions & 0 deletions integration-cli/docker_cli_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,31 @@ func TestExecWithUser(t *testing.T) {

logDone("exec - with user")
}

func TestExecWithPrivileged(t *testing.T) {
defer deleteAllContainers()

runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "--cap-drop=ALL", "busybox", "top")
if out, _, err := runCommandWithOutput(runCmd); err != nil {
t.Fatal(out, err)
}

cmd := exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sda b 8 0")
out, _, err := runCommandWithOutput(cmd)
fmt.Printf("%s", out)
if err == nil || !strings.Contains(out, "Operation not permitted") {
t.Fatalf("exec mknod in --cap-drop=ALL container without --privileged should failed")
}

cmd = exec.Command(dockerBinary, "exec", "--privileged", "parent", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
out, _, err = runCommandWithOutput(cmd)
if err != nil {
t.Fatal(err, out)
}

if actual := strings.TrimSpace(out); actual != "ok" {
t.Fatalf("exec mknod in --cap-drop=ALL container with --privileged failed: %v, output: %q", err, out)
}

logDone("exec - exec command in a container with privileged")
}
21 changes: 10 additions & 11 deletions runconfig/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ type ExecConfig struct {
func ExecConfigFromJob(job *engine.Job) (*ExecConfig, error) {
execConfig := &ExecConfig{
User: job.Getenv("User"),
// TODO(vishh): Expose 'Privileged' once it is supported.
//Privileged: job.GetenvBool("Privileged"),
Privileged: job.GetenvBool("Privileged"),
Tty: job.GetenvBool("Tty"),
AttachStdin: job.GetenvBool("AttachStdin"),
AttachStderr: job.GetenvBool("AttachStderr"),
Expand All @@ -41,12 +40,13 @@ func ExecConfigFromJob(job *engine.Job) (*ExecConfig, error) {

func ParseExec(cmd *flag.FlagSet, args []string) (*ExecConfig, error) {
var (
flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached")
flTty = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY")
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run command in the background")
flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID (format: <name|uid>[:<group|gid>])")
execCmd []string
container string
flStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Keep STDIN open even if not attached")
flTty = cmd.Bool([]string{"t", "-tty"}, false, "Allocate a pseudo-TTY")
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run command in the background")
flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID (format: <name|uid>[:<group|gid>])")
flPrivileged = cmd.Bool([]string{"-privileged"}, false, "Give extended privileges to the command")
execCmd []string
container string
)
cmd.Require(flag.Min, 2)
if err := cmd.ParseFlags(args, true); err != nil {
Expand All @@ -57,9 +57,8 @@ func ParseExec(cmd *flag.FlagSet, args []string) (*ExecConfig, error) {
execCmd = parsedArgs[1:]

execConfig := &ExecConfig{
User: *flUser,
// TODO(vishh): Expose '-p' flag once it is supported.
Privileged: false,
User: *flUser,
Privileged: *flPrivileged,
Tty: *flTty,
Cmd: execCmd,
Container: container,
Expand Down

0 comments on commit 72a500e

Please sign in to comment.