github.com/sld880311/docker@v0.0.0-20200524143708-d5593973a475/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/docker Github 8 repository at https://github.com/docker/docker/. 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 ``` 33 34 ## Description 35 36 The `docker exec` command runs a new command in a running container. 37 38 The command started using `docker exec` only runs while the container's primary 39 process (`PID 1`) is running, and it is not restarted if the container is 40 restarted. 41 42 ## Examples 43 44 ### Run `docker exec` on a running container 45 46 First, start a container. 47 48 ```bash 49 $ docker run --name ubuntu_bash --rm -i -t ubuntu bash 50 ``` 51 52 This will create a container named `ubuntu_bash` and start a Bash session. 53 54 Next, execute a command on the container. 55 56 ```bash 57 $ docker exec -d ubuntu_bash touch /tmp/execWorks 58 ``` 59 60 This will create a new file `/tmp/execWorks` inside the running container 61 `ubuntu_bash`, in the background. 62 63 Next, execute an interactive `bash` shell on the container. 64 65 ```bash 66 $ docker exec -it ubuntu_bash bash 67 ``` 68 69 This will create a new Bash session in the container `ubuntu_bash`. 70 71 ### Try to run `docker exec` on a paused container 72 73 If the container is paused, then the `docker exec` command will fail with an error: 74 75 ```bash 76 $ docker pause test 77 78 test 79 80 $ docker ps 81 82 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 83 1ae3b36715d2 ubuntu:latest "bash" 17 seconds ago Up 16 seconds (Paused) test 84 85 $ docker exec test ls 86 87 FATA[0000] Error response from daemon: Container test is paused, unpause the container before exec 88 89 $ echo $? 90 1 91 ```