github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/docs/reference/commandline/container_exec.md (about)

     1  # exec
     2  
     3  <!---MARKER_GEN_START-->
     4  Execute a command in a running container
     5  
     6  ### Aliases
     7  
     8  `docker container exec`, `docker exec`
     9  
    10  ### Options
    11  
    12  | Name                                      | Type     | Default | Description                                            |
    13  |:------------------------------------------|:---------|:--------|:-------------------------------------------------------|
    14  | `-d`, `--detach`                          |          |         | Detached mode: run command in the background           |
    15  | `--detach-keys`                           | `string` |         | Override the key sequence for detaching a container    |
    16  | [`-e`](#env), [`--env`](#env)             | `list`   |         | Set environment variables                              |
    17  | `--env-file`                              | `list`   |         | Read in a file of environment variables                |
    18  | `-i`, `--interactive`                     |          |         | Keep STDIN open even if not attached                   |
    19  | [`--privileged`](#privileged)             |          |         | Give extended privileges to the command                |
    20  | `-t`, `--tty`                             |          |         | Allocate a pseudo-TTY                                  |
    21  | `-u`, `--user`                            | `string` |         | Username or UID (format: `<name\|uid>[:<group\|gid>]`) |
    22  | [`-w`](#workdir), [`--workdir`](#workdir) | `string` |         | Working directory inside the container                 |
    23  
    24  
    25  <!---MARKER_GEN_END-->
    26  
    27  ## Description
    28  
    29  The `docker exec` command runs a new command in a running container.
    30  
    31  The command you specify with `docker exec` only runs while the container's
    32  primary process (`PID 1`) is running, and it isn't restarted if the container
    33  is restarted.
    34  
    35  The command runs in the default working directory of the container.
    36  
    37  The command must be an executable. A chained or a quoted command doesn't work.
    38  
    39  - This works: `docker exec -it my_container sh -c "echo a && echo b"`
    40  - This doesn't work: `docker exec -it my_container "echo a && echo b"`
    41  
    42  ## Examples
    43  
    44  ### Run `docker exec` on a running container
    45  
    46  First, start a container.
    47  
    48  ```console
    49  $ docker run --name mycontainer -d -i -t alpine /bin/sh
    50  ```
    51  
    52  This creates and starts a container named `mycontainer` from an `alpine` image
    53  with an `sh` shell as its main process. The `-d` option (shorthand for `--detach`)
    54  sets the container to run in the background, in detached mode, with a pseudo-TTY
    55  attached (`-t`). The `-i` option is set to keep `STDIN` attached (`-i`), which
    56  prevents the `sh` process from exiting immediately.
    57  
    58  Next, execute a command on the container.
    59  
    60  ```console
    61  $ docker exec -d mycontainer touch /tmp/execWorks
    62  ```
    63  
    64  This creates a new file `/tmp/execWorks` inside the running container
    65  `mycontainer`, in the background.
    66  
    67  Next, execute an interactive `sh` shell on the container.
    68  
    69  ```console
    70  $ docker exec -it mycontainer sh
    71  ```
    72  
    73  This starts a new shell session in the container `mycontainer`.
    74  
    75  ### <a name="env"></a> Set environment variables for the exec process (--env, -e)
    76  
    77  Next, set environment variables in the current bash session.
    78  
    79  The `docker exec` command inherits the environment variables that are set at the
    80  time the container is created. Use the `--env` (or the `-e` shorthand) to
    81  override global environment variables, or to set additional environment
    82  variables for the process started by `docker exec`.
    83  
    84  The following example creates a new shell session in the container `mycontainer`,
    85  with environment variables `$VAR_A` set to `1`, and `$VAR_B` set to `2`.
    86  These environment variables are only valid for the `sh` process started by that
    87  `docker exec` command, and aren't available to other processes running inside
    88  the container.
    89  
    90  ```console
    91  $ docker exec -e VAR_A=1 -e VAR_B=2 mycontainer env
    92  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    93  HOSTNAME=f64a4851eb71
    94  VAR_A=1
    95  VAR_B=2
    96  HOME=/root
    97  ```
    98  
    99  ### <a name="privileged"></a> Escalate container privileges (--privileged)
   100  
   101  See [`docker run --privileged`](container_run.md#privileged).
   102  
   103  ### <a name="workdir"></a> Set the working directory for the exec process (--workdir, -w)
   104  
   105  By default `docker exec` command runs in the same working directory set when
   106  the container was created.
   107  
   108  ```console
   109  $ docker exec -it mycontainer pwd
   110  /
   111  ```
   112  
   113  You can specify an alternative working directory for the command to execute
   114  using the `--workdir` option (or the `-w` shorthand):
   115  
   116  ```console
   117  $ docker exec -it -w /root mycontainer pwd
   118  /root
   119  ```
   120  
   121  ### Try to run `docker exec` on a paused container
   122  
   123  If the container is paused, then the `docker exec` command fails with an error:
   124  
   125  ```console
   126  $ docker pause mycontainer
   127  mycontainer
   128  
   129  $ docker ps
   130  
   131  CONTAINER ID   IMAGE     COMMAND     CREATED          STATUS                   PORTS     NAMES
   132  482efdf39fac   alpine    "/bin/sh"   17 seconds ago   Up 16 seconds (Paused)             mycontainer
   133  
   134  $ docker exec mycontainer sh
   135  
   136  Error response from daemon: Container mycontainer is paused, unpause the container before exec
   137  
   138  $ echo $?
   139  1
   140  ```