github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/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  COMMAND will run in the default directory of the container. It the
    43  underlying image has a custom directory specified with the WORKDIR directive
    44  in its Dockerfile, this will be used instead.
    45  
    46  COMMAND should be an executable, a chained or a quoted command
    47  will not work. Example: `docker exec -ti my_container "echo a && echo b"` will
    48  not work, but `docker exec -ti my_container sh -c "echo a && echo b"` will.
    49  
    50  ## Examples
    51  
    52  ### Run `docker exec` on a running container
    53  
    54  First, start a container.
    55  
    56  ```bash
    57  $ docker run --name ubuntu_bash --rm -i -t ubuntu bash
    58  ```
    59  
    60  This will create a container named `ubuntu_bash` and start a Bash session.
    61  
    62  Next, execute a command on the container.
    63  
    64  ```bash
    65  $ docker exec -d ubuntu_bash touch /tmp/execWorks
    66  ```
    67  
    68  This will create a new file `/tmp/execWorks` inside the running container
    69  `ubuntu_bash`, in the background.
    70  
    71  Next, execute an interactive `bash` shell on the container.
    72  
    73  ```bash
    74  $ docker exec -it ubuntu_bash bash
    75  ```
    76  
    77  This will create a new Bash session in the container `ubuntu_bash`.
    78  
    79  ### Try to run `docker exec` on a paused container
    80  
    81  If the container is paused, then the `docker exec` command will fail with an error:
    82  
    83  ```bash
    84  $ docker pause test
    85  
    86  test
    87  
    88  $ docker ps
    89  
    90  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
    91  1ae3b36715d2        ubuntu:latest       "bash"              17 seconds ago      Up 16 seconds (Paused)                       test
    92  
    93  $ docker exec test ls
    94  
    95  FATA[0000] Error response from daemon: Container test is paused, unpause the container before exec
    96  
    97  $ echo $?
    98  1
    99  ```