diff --git a/cli/command/container/exec.go b/cli/command/container/exec.go index 46775f3048..c5e9004808 100644 --- a/cli/command/container/exec.go +++ b/cli/command/container/exec.go @@ -24,6 +24,7 @@ type execOptions struct { user string privileged bool env opts.ListOpts + workdir string container string command []string } @@ -57,6 +58,7 @@ func NewExecCommand(dockerCli command.Cli) *cobra.Command { flags.StringVarP(&options.user, "user", "u", "", "Username or UID (format: [:])") flags.BoolVarP(&options.privileged, "privileged", "", false, "Give extended privileges to the command") flags.VarP(&options.env, "env", "e", "Set environment variables") + flags.StringVarP(&options.workdir, "workdir", "w", "", "Working directory inside the container") flags.SetAnnotation("env", "version", []string{"1.25"}) return cmd @@ -190,6 +192,7 @@ func parseExec(opts execOptions, configFile *configfile.ConfigFile) *types.ExecC Cmd: opts.command, Detach: opts.detach, Env: opts.env.GetAll(), + WorkingDir: opts.workdir, } // If -d is not set, attach to everything by default diff --git a/contrib/completion/bash/docker b/contrib/completion/bash/docker index 5083e19449..0e63c5ab4d 100644 --- a/contrib/completion/bash/docker +++ b/contrib/completion/bash/docker @@ -1439,7 +1439,7 @@ _docker_container_exec() { case "$cur" in -*) - COMPREPLY=( $( compgen -W "--detach -d --detach-keys --env -e --help --interactive -i --privileged -t --tty -u --user" -- "$cur" ) ) + COMPREPLY=( $( compgen -W "--detach -d --detach-keys --env -e --help --interactive -i --privileged -t --tty -u --user --workdir -w" -- "$cur" ) ) ;; *) __docker_complete_containers_running diff --git a/contrib/completion/fish/docker.fish b/contrib/completion/fish/docker.fish index d825922776..178aa9c3ab 100644 --- a/contrib/completion/fish/docker.fish +++ b/contrib/completion/fish/docker.fish @@ -174,6 +174,7 @@ complete -c docker -A -f -n '__fish_seen_subcommand_from exec' -s d -l detach -d complete -c docker -A -f -n '__fish_seen_subcommand_from exec' -l help -d 'Print usage' complete -c docker -A -f -n '__fish_seen_subcommand_from exec' -s i -l interactive -d 'Keep STDIN open even if not attached' complete -c docker -A -f -n '__fish_seen_subcommand_from exec' -s t -l tty -d 'Allocate a pseudo-TTY' +complete -c docker -A -f -n '__fish_seen_subcommand_from exec' -s w -l workdir -d 'Working directory inside the container' complete -c docker -A -f -n '__fish_seen_subcommand_from exec' -a '(__fish_print_docker_containers running)' -d "Container" # export diff --git a/contrib/completion/zsh/_docker b/contrib/completion/zsh/_docker index b25dc7b197..26d5a196ee 100644 --- a/contrib/completion/zsh/_docker +++ b/contrib/completion/zsh/_docker @@ -745,6 +745,7 @@ __docker_container_subcommand() { "($help)--privileged[Give extended Linux capabilities to the command]" \ "($help -t --tty)"{-t,--tty}"[Allocate a pseudo-tty]" \ "($help -u --user)"{-u=,--user=}"[Username or UID]:user:_users" \ + "($help -w --workdir)"{-w=,--workdir=}"[Working directory inside the container]:directory:_directories" "($help -):containers:__docker_complete_running_containers" \ "($help -)*::command:->anycommand" && ret=0 case $state in diff --git a/docs/reference/commandline/exec.md b/docs/reference/commandline/exec.md index 63635b2f40..35072dad1f 100644 --- a/docs/reference/commandline/exec.md +++ b/docs/reference/commandline/exec.md @@ -29,6 +29,7 @@ Options: --privileged Give extended privileges to the command -t, --tty Allocate a pseudo-TTY -u, --user Username or UID (format: [:]) + -w, --workdir Working directory inside the container ``` ## Description @@ -86,6 +87,20 @@ This will create a new Bash session in the container `ubuntu_bash` with environm variable `$VAR` set to "1". Note that this environment variable will only be valid on the current Bash session. +By default `docker exec` command runs in the same working directory set when container was created. + +```bash +$ docker exec -it ubuntu_bash pwd +/ +``` + +You can select working directory for the command to execute into + +```bash +$ docker exec -it -w /root ubuntu_bash pwd +/root +``` + ### Try to run `docker exec` on a paused container