github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/docs/reference/commandline/exec.md (about) 1 --- 2 title: "exec" 3 description: "The exec command description and usage" 4 keywords: "command, container, run, execute" 5 --- 6 7 <!-- This file is maintained within the docker/cli GitHub 8 repository at https://github.com/docker/cli/. Make all 9 pull requests against that repo. If you see this file in 10 another repository, consider it read-only there, as it will 11 periodically be overwritten by the definitive file. Pull 12 requests which include edits to this file in other repositories 13 will be rejected. 14 --> 15 16 # exec 17 18 ```markdown 19 Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] 20 21 Run a command in a running container 22 23 Options: 24 -d, --detach Detached mode: run command in the background 25 --detach-keys Override the key sequence for detaching a container 26 -e, --env=[] Set environment variables 27 --help Print usage 28 -i, --interactive Keep STDIN open even if not attached 29 --privileged Give extended privileges to the command 30 -t, --tty Allocate a pseudo-TTY 31 -u, --user Username or UID (format: <name|uid>[:<group|gid>]) 32 -w, --workdir Working directory inside the container 33 ``` 34 35 ## Description 36 37 The `docker exec` command runs a new command in a running container. 38 39 The command started using `docker exec` only runs while the container's primary 40 process (`PID 1`) is running, and it is not restarted if the container is 41 restarted. 42 43 COMMAND will run in the default directory of the container. If the 44 underlying image has a custom directory specified with the WORKDIR directive 45 in its Dockerfile, this will be used instead. 46 47 COMMAND should be an executable, a chained or a quoted command 48 will not work. Example: `docker exec -ti my_container "echo a && echo b"` will 49 not work, but `docker exec -ti my_container sh -c "echo a && echo b"` will. 50 51 ## Examples 52 53 ### Run `docker exec` on a running container 54 55 First, start a container. 56 57 ```bash 58 $ docker run --name ubuntu_bash --rm -i -t ubuntu bash 59 ``` 60 61 This will create a container named `ubuntu_bash` and start a Bash session. 62 63 Next, execute a command on the container. 64 65 ```bash 66 $ docker exec -d ubuntu_bash touch /tmp/execWorks 67 ``` 68 69 This will create a new file `/tmp/execWorks` inside the running container 70 `ubuntu_bash`, in the background. 71 72 Next, execute an interactive `bash` shell on the container. 73 74 ```bash 75 $ docker exec -it ubuntu_bash bash 76 ``` 77 78 This will create a new Bash session in the container `ubuntu_bash`. 79 80 Next, set an environment variable in the current bash session. 81 82 ```bash 83 $ docker exec -it -e VAR=1 ubuntu_bash bash 84 ``` 85 86 This will create a new Bash session in the container `ubuntu_bash` with environment 87 variable `$VAR` set to "1". Note that this environment variable will only be valid 88 on the current Bash session. 89 90 By default `docker exec` command runs in the same working directory set when container was created. 91 92 ```bash 93 $ docker exec -it ubuntu_bash pwd 94 / 95 ``` 96 97 You can select working directory for the command to execute into 98 99 ```bash 100 $ docker exec -it -w /root ubuntu_bash pwd 101 /root 102 ``` 103 104 105 ### Try to run `docker exec` on a paused container 106 107 If the container is paused, then the `docker exec` command will fail with an error: 108 109 ```bash 110 $ docker pause test 111 112 test 113 114 $ docker ps 115 116 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 117 1ae3b36715d2 ubuntu:latest "bash" 17 seconds ago Up 16 seconds (Paused) test 118 119 $ docker exec test ls 120 121 FATA[0000] Error response from daemon: Container test is paused, unpause the container before exec 122 123 $ echo $? 124 1 125 ```