summaryrefslogtreecommitdiff
path: root/devdocs/docker/engine%2Freference%2Fcommandline%2Fexec%2Findex.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/docker/engine%2Freference%2Fcommandline%2Fexec%2Findex.html
new repository
Diffstat (limited to 'devdocs/docker/engine%2Freference%2Fcommandline%2Fexec%2Findex.html')
-rw-r--r--devdocs/docker/engine%2Freference%2Fcommandline%2Fexec%2Findex.html42
1 files changed, 42 insertions, 0 deletions
diff --git a/devdocs/docker/engine%2Freference%2Fcommandline%2Fexec%2Findex.html b/devdocs/docker/engine%2Freference%2Fcommandline%2Fexec%2Findex.html
new file mode 100644
index 00000000..9e9de22f
--- /dev/null
+++ b/devdocs/docker/engine%2Freference%2Fcommandline%2Fexec%2Findex.html
@@ -0,0 +1,42 @@
+<h1>docker exec</h1> <p><br></p> <p>Run a command in a running container</p> <h2 id="usage">Usage</h2> <div class="highlight"><pre class="highlight" data-language="">$ docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
+</pre></div> <p>Refer to the <a href="#options">options section</a> for an overview of available <a href="#options"><code class="language-plaintext highlighter-rouge">OPTIONS</code></a> for this command.</p> <h2 id="description">Description</h2> <p name="extended-description">The <code class="language-plaintext highlighter-rouge">docker exec</code> command runs a new command in a running container.</p> <p>The command started using <code class="language-plaintext highlighter-rouge">docker exec</code> only runs while the container’s primary process (<code class="language-plaintext highlighter-rouge">PID 1</code>) is running, and it is not restarted if the container is restarted.</p> <p>COMMAND will run in the default directory of the container. If the underlying image has a custom directory specified with the WORKDIR directive in its Dockerfile, this will be used instead.</p> <p>COMMAND should be an executable, a chained or a quoted command will not work. Example: <code class="language-plaintext highlighter-rouge">docker exec -ti my_container "echo a &amp;&amp; echo b"</code> will not work, but <code class="language-plaintext highlighter-rouge">docker exec -ti my_container sh -c "echo a &amp;&amp; echo b"</code> will.</p> <p>For example uses of this command, refer to the <a href="#examples">examples section</a> below.</p> <h2 id="options">Options</h2> <table> <thead> <tr> <td>Name, shorthand</td> <td>Default</td> <td>Description</td> </tr> </thead> <tbody> <tr> <td>
+<code class="language-plaintext highlighter-rouge">--detach</code> , <code class="language-plaintext highlighter-rouge">-d</code>
+</td> <td></td> <td>Detached mode: run command in the background</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--detach-keys</code></td> <td></td> <td>Override the key sequence for detaching a container</td> </tr> <tr> <td>
+<code class="language-plaintext highlighter-rouge">--env</code> , <code class="language-plaintext highlighter-rouge">-e</code>
+</td> <td></td> <td>Set environment variables</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--env-file</code></td> <td></td> <td>Read in a file of environment variables</td> </tr> <tr> <td>
+<code class="language-plaintext highlighter-rouge">--interactive</code> , <code class="language-plaintext highlighter-rouge">-i</code>
+</td> <td></td> <td>Keep STDIN open even if not attached</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--privileged</code></td> <td></td> <td>Give extended privileges to the command</td> </tr> <tr> <td>
+<code class="language-plaintext highlighter-rouge">--tty</code> , <code class="language-plaintext highlighter-rouge">-t</code>
+</td> <td></td> <td>Allocate a pseudo-TTY</td> </tr> <tr> <td>
+<code class="language-plaintext highlighter-rouge">--user</code> , <code class="language-plaintext highlighter-rouge">-u</code>
+</td> <td></td> <td>Username or UID (format: &lt;name|uid&gt;[:&lt;group|gid&gt;])</td> </tr> <tr> <td>
+<code class="language-plaintext highlighter-rouge">--workdir</code> , <code class="language-plaintext highlighter-rouge">-w</code>
+</td> <td></td> <td>Working directory inside the container</td> </tr> </tbody> </table> <h2 id="examples">Examples</h2> <h3 id="run-docker-exec-on-a-running-container">Run <code class="language-plaintext highlighter-rouge">docker exec</code> on a running container</h3> <p>First, start a container.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --name ubuntu_bash --rm -i -t ubuntu bash
+</pre></div> <p>This will create a container named <code class="language-plaintext highlighter-rouge">ubuntu_bash</code> and start a Bash session.</p> <p>Next, execute a command on the container.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker exec -d ubuntu_bash touch /tmp/execWorks
+</pre></div> <p>This will create a new file <code class="language-plaintext highlighter-rouge">/tmp/execWorks</code> inside the running container <code class="language-plaintext highlighter-rouge">ubuntu_bash</code>, in the background.</p> <p>Next, execute an interactive <code class="language-plaintext highlighter-rouge">bash</code> shell on the container.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker exec -it ubuntu_bash bash
+</pre></div> <p>This will create a new Bash session in the container <code class="language-plaintext highlighter-rouge">ubuntu_bash</code>.</p> <p>Next, set an environment variable in the current bash session.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker exec -it -e VAR=1 ubuntu_bash bash
+</pre></div> <p>This will create a new Bash session in the container <code class="language-plaintext highlighter-rouge">ubuntu_bash</code> with environment variable <code class="language-plaintext highlighter-rouge">$VAR</code> set to “1”. Note that this environment variable will only be valid on the current Bash session.</p> <p>By default <code class="language-plaintext highlighter-rouge">docker exec</code> command runs in the same working directory set when container was created.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker exec -it ubuntu_bash pwd
+/
+</pre></div> <p>You can select working directory for the command to execute into</p> <div class="highlight"><pre class="highlight" data-language="">$ docker exec -it -w /root ubuntu_bash pwd
+/root
+</pre></div> <h3 id="try-to-run-docker-exec-on-a-paused-container">Try to run <code class="language-plaintext highlighter-rouge">docker exec</code> on a paused container</h3> <p>If the container is paused, then the <code class="language-plaintext highlighter-rouge">docker exec</code> command will fail with an error:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker pause test
+
+test
+
+$ docker ps
+
+CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
+1ae3b36715d2 ubuntu:latest "bash" 17 seconds ago Up 16 seconds (Paused) test
+
+$ docker exec test ls
+
+FATA[0000] Error response from daemon: Container test is paused, unpause the container before exec
+
+$ echo $?
+1
+</pre></div> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2019 Docker, Inc.<br>Licensed under the Apache License, Version 2.0.<br>Docker and the Docker logo are trademarks or registered trademarks of Docker, Inc. in the United States and/or other countries.<br>Docker, Inc. and other parties may also have trademark rights in other terms used herein.<br>
+ <a href="https://docs.docker.com/engine/reference/commandline/exec/" class="_attribution-link">https://docs.docker.com/engine/reference/commandline/exec/</a>
+ </p>
+</div>