github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/docs/reference/commandline/exec.md (about) 1 <!--[metadata]> 2 +++ 3 title = "exec" 4 description = "The exec command description and usage" 5 keywords = ["command, container, run, execute"] 6 [menu.main] 7 parent = "smn_cli" 8 +++ 9 <![end-metadata]--> 10 11 # exec 12 13 ```markdown 14 Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] 15 16 Run a command in a running container 17 18 -d, --detach Detached mode: run command in the background 19 --detach-keys Override the key sequence for detaching a container 20 --help Print usage 21 -i, --interactive Keep STDIN open even if not attached 22 --privileged Give extended privileges to the command 23 -t, --tty Allocate a pseudo-TTY 24 -u, --user Username or UID (format: <name|uid>[:<group|gid>]) 25 ``` 26 27 The `docker exec` command runs a new command in a running container. 28 29 The command started using `docker exec` only runs while the container's primary 30 process (`PID 1`) is running, and it is not restarted if the container is 31 restarted. 32 33 If the container is paused, then the `docker exec` command will fail with an error: 34 35 $ docker pause test 36 test 37 $ docker ps 38 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 39 1ae3b36715d2 ubuntu:latest "bash" 17 seconds ago Up 16 seconds (Paused) test 40 $ docker exec test ls 41 FATA[0000] Error response from daemon: Container test is paused, unpause the container before exec 42 $ echo $? 43 1 44 45 ## Examples 46 47 $ docker run --name ubuntu_bash --rm -i -t ubuntu bash 48 49 This will create a container named `ubuntu_bash` and start a Bash session. 50 51 $ docker exec -d ubuntu_bash touch /tmp/execWorks 52 53 This will create a new file `/tmp/execWorks` inside the running container 54 `ubuntu_bash`, in the background. 55 56 $ docker exec -it ubuntu_bash bash 57 58 This will create a new Bash session in the container `ubuntu_bash`.