github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/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`                            |          |         | 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="workdir"></a> Set the working directory for the exec process (--workdir, -w)
   100  
   101  By default `docker exec` command runs in the same working directory set when
   102  the container was created.
   103  
   104  ```console
   105  $ docker exec -it mycontainer pwd
   106  /
   107  ```
   108  
   109  You can specify an alternative working directory for the command to execute
   110  using the `--workdir` option (or the `-w` shorthand):
   111  
   112  ```console
   113  $ docker exec -it -w /root mycontainer pwd
   114  /root
   115  ```
   116  
   117  ### Try to run `docker exec` on a paused container
   118  
   119  If the container is paused, then the `docker exec` command fails with an error:
   120  
   121  ```console
   122  $ docker pause mycontainer
   123  mycontainer
   124  
   125  $ docker ps
   126  
   127  CONTAINER ID   IMAGE     COMMAND     CREATED          STATUS                   PORTS     NAMES
   128  482efdf39fac   alpine    "/bin/sh"   17 seconds ago   Up 16 seconds (Paused)             mycontainer
   129  
   130  $ docker exec mycontainer sh
   131  
   132  Error response from daemon: Container mycontainer is paused, unpause the container before exec
   133  
   134  $ echo $?
   135  1
   136  ```