github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/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 Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...] 14 15 Run a command in a running container 16 17 -d, --detach Detached mode: run command in the background 18 --detach-keys Specify the escape key sequence used to detach a container 19 --help Print usage 20 -i, --interactive Keep STDIN open even if not attached 21 --privileged Give extended Linux capabilities to the command 22 -t, --tty Allocate a pseudo-TTY 23 -u, --user= Username or UID (format: <name|uid>[:<group|gid>]) 24 25 The `docker exec` command runs a new command in a running container. 26 27 The command started using `docker exec` only runs while the container's primary 28 process (`PID 1`) is running, and it is not restarted if the container is 29 restarted. 30 31 If the container is paused, then the `docker exec` command will fail with an error: 32 33 $ docker pause test 34 test 35 $ docker ps 36 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 37 1ae3b36715d2 ubuntu:latest "bash" 17 seconds ago Up 16 seconds (Paused) test 38 $ docker exec test ls 39 FATA[0000] Error response from daemon: Container test is paused, unpause the container before exec 40 $ echo $? 41 1 42 43 ## Examples 44 45 $ docker run --name ubuntu_bash --rm -i -t ubuntu bash 46 47 This will create a container named `ubuntu_bash` and start a Bash session. 48 49 $ docker exec -d ubuntu_bash touch /tmp/execWorks 50 51 This will create a new file `/tmp/execWorks` inside the running container 52 `ubuntu_bash`, in the background. 53 54 $ docker exec -it ubuntu_bash bash 55 56 This will create a new Bash session in the container `ubuntu_bash`.