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