diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/docker/engine%2Freference%2Fcommandline%2Frun%2Findex.html | |
new repository
Diffstat (limited to 'devdocs/docker/engine%2Freference%2Fcommandline%2Frun%2Findex.html')
| -rw-r--r-- | devdocs/docker/engine%2Freference%2Fcommandline%2Frun%2Findex.html | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/devdocs/docker/engine%2Freference%2Fcommandline%2Frun%2Findex.html b/devdocs/docker/engine%2Freference%2Fcommandline%2Frun%2Findex.html new file mode 100644 index 00000000..908fe24a --- /dev/null +++ b/devdocs/docker/engine%2Freference%2Fcommandline%2Frun%2Findex.html @@ -0,0 +1,196 @@ +<h1>docker run</h1> <p><br></p> <p>Run a command in a new container</p> <h2 id="usage">Usage</h2> <div class="highlight"><pre class="highlight" data-language="">$ docker run [OPTIONS] IMAGE [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 run</code> command first <code class="language-plaintext highlighter-rouge">creates</code> a writeable container layer over the specified image, and then <code class="language-plaintext highlighter-rouge">starts</code> it using the specified command. That is, <code class="language-plaintext highlighter-rouge">docker run</code> is equivalent to the API <code class="language-plaintext highlighter-rouge">/containers/create</code> then <code class="language-plaintext highlighter-rouge">/containers/(id)/start</code>. A stopped container can be restarted with all its previous changes intact using <code class="language-plaintext highlighter-rouge">docker start</code>. See <code class="language-plaintext highlighter-rouge">docker ps -a</code> to view a list of all containers.</p> <p>The <code class="language-plaintext highlighter-rouge">docker run</code> command can be used in combination with <code class="language-plaintext highlighter-rouge">docker commit</code> to <a href="../commit/index"><em>change the command that a container runs</em></a>. There is additional detailed information about <code class="language-plaintext highlighter-rouge">docker run</code> in the <a href="../../run/index">Docker run reference</a>.</p> <p>For information on connecting a container to a network, see the <a href="https://docs.docker.com/network/">“<em>Docker network overview</em>”</a>.</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">--add-host</code></td> <td></td> <td>Add a custom host-to-IP mapping (host:ip)</td> </tr> <tr> <td> +<code class="language-plaintext highlighter-rouge">--attach</code> , <code class="language-plaintext highlighter-rouge">-a</code> +</td> <td></td> <td>Attach to STDIN, STDOUT or STDERR</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--blkio-weight</code></td> <td></td> <td>Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--blkio-weight-device</code></td> <td></td> <td>Block IO weight (relative device weight)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cap-add</code></td> <td></td> <td>Add Linux capabilities</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cap-drop</code></td> <td></td> <td>Drop Linux capabilities</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cgroup-parent</code></td> <td></td> <td>Optional parent cgroup for the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cgroupns</code></td> <td></td> <td> +<a href="https://docs.docker.com/engine/api/v1.41/" target="_blank" rel="noopener" class="_"><span class="badge badge-info" data-toggle="tooltip" title="Open the API reference (in a new window)">API 1.41+</span></a><br>Cgroup namespace to use (host|private) 'host': Run the container in the Docker host's cgroup namespace 'private': Run the container in its own private cgroup namespace '': Use the cgroup namespace as configured by the default-cgroupns-mode option on the daemon (default)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cidfile</code></td> <td></td> <td>Write the container ID to the file</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cpu-count</code></td> <td></td> <td>CPU count (Windows only)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cpu-percent</code></td> <td></td> <td>CPU percent (Windows only)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cpu-period</code></td> <td></td> <td>Limit CPU CFS (Completely Fair Scheduler) period</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cpu-quota</code></td> <td></td> <td>Limit CPU CFS (Completely Fair Scheduler) quota</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cpu-rt-period</code></td> <td></td> <td>Limit CPU real-time period in microseconds</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cpu-rt-runtime</code></td> <td></td> <td>Limit CPU real-time runtime in microseconds</td> </tr> <tr> <td> +<code class="language-plaintext highlighter-rouge">--cpu-shares</code> , <code class="language-plaintext highlighter-rouge">-c</code> +</td> <td></td> <td>CPU shares (relative weight)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cpus</code></td> <td></td> <td>Number of CPUs</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cpuset-cpus</code></td> <td></td> <td>CPUs in which to allow execution (0-3, 0,1)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--cpuset-mems</code></td> <td></td> <td>MEMs in which to allow execution (0-3, 0,1)</td> </tr> <tr> <td> +<code class="language-plaintext highlighter-rouge">--detach</code> , <code class="language-plaintext highlighter-rouge">-d</code> +</td> <td></td> <td>Run container in background and print container ID</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">--device</code></td> <td></td> <td>Add a host device to the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--device-cgroup-rule</code></td> <td></td> <td>Add a rule to the cgroup allowed devices list</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--device-read-bps</code></td> <td></td> <td>Limit read rate (bytes per second) from a device</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--device-read-iops</code></td> <td></td> <td>Limit read rate (IO per second) from a device</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--device-write-bps</code></td> <td></td> <td>Limit write rate (bytes per second) to a device</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--device-write-iops</code></td> <td></td> <td>Limit write rate (IO per second) to a device</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--disable-content-trust</code></td> <td><code class="language-plaintext highlighter-rouge">true</code></td> <td>Skip image verification</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--dns</code></td> <td></td> <td>Set custom DNS servers</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--dns-opt</code></td> <td></td> <td>Set DNS options</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--dns-option</code></td> <td></td> <td>Set DNS options</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--dns-search</code></td> <td></td> <td>Set custom DNS search domains</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--domainname</code></td> <td></td> <td>Container NIS domain name</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--entrypoint</code></td> <td></td> <td>Overwrite the default ENTRYPOINT of the image</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">--expose</code></td> <td></td> <td>Expose a port or a range of ports</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--gpus</code></td> <td></td> <td> +<a href="https://docs.docker.com/engine/api/v1.40/" target="_blank" rel="noopener" class="_"><span class="badge badge-info" data-toggle="tooltip" title="Open the API reference (in a new window)">API 1.40+</span></a><br>GPU devices to add to the container ('all' to pass all GPUs)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--group-add</code></td> <td></td> <td>Add additional groups to join</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--health-cmd</code></td> <td></td> <td>Command to run to check health</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--health-interval</code></td> <td></td> <td>Time between running the check (ms|s|m|h) (default 0s)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--health-retries</code></td> <td></td> <td>Consecutive failures needed to report unhealthy</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--health-start-period</code></td> <td></td> <td>Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--health-timeout</code></td> <td></td> <td>Maximum time to allow one check to run (ms|s|m|h) (default 0s)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--help</code></td> <td></td> <td>Print usage</td> </tr> <tr> <td> +<code class="language-plaintext highlighter-rouge">--hostname</code> , <code class="language-plaintext highlighter-rouge">-h</code> +</td> <td></td> <td>Container host name</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--init</code></td> <td></td> <td>Run an init inside the container that forwards signals and reaps processes</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">--io-maxbandwidth</code></td> <td></td> <td>Maximum IO bandwidth limit for the system drive (Windows only)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--io-maxiops</code></td> <td></td> <td>Maximum IOps limit for the system drive (Windows only)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--ip</code></td> <td></td> <td>IPv4 address (e.g., 172.30.100.104)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--ip6</code></td> <td></td> <td>IPv6 address (e.g., 2001:db8::33)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--ipc</code></td> <td></td> <td>IPC mode to use</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--isolation</code></td> <td></td> <td>Container isolation technology</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--kernel-memory</code></td> <td></td> <td>Kernel memory limit</td> </tr> <tr> <td> +<code class="language-plaintext highlighter-rouge">--label</code> , <code class="language-plaintext highlighter-rouge">-l</code> +</td> <td></td> <td>Set meta data on a container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--label-file</code></td> <td></td> <td>Read in a line delimited file of labels</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--link</code></td> <td></td> <td>Add link to another container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--link-local-ip</code></td> <td></td> <td>Container IPv4/IPv6 link-local addresses</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--log-driver</code></td> <td></td> <td>Logging driver for the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--log-opt</code></td> <td></td> <td>Log driver options</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--mac-address</code></td> <td></td> <td>Container MAC address (e.g., 92:d0:c6:0a:29:33)</td> </tr> <tr> <td> +<code class="language-plaintext highlighter-rouge">--memory</code> , <code class="language-plaintext highlighter-rouge">-m</code> +</td> <td></td> <td>Memory limit</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--memory-reservation</code></td> <td></td> <td>Memory soft limit</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--memory-swap</code></td> <td></td> <td>Swap limit equal to memory plus swap: '-1' to enable unlimited swap</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--memory-swappiness</code></td> <td><code class="language-plaintext highlighter-rouge">-1</code></td> <td>Tune container memory swappiness (0 to 100)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--mount</code></td> <td></td> <td>Attach a filesystem mount to the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--name</code></td> <td></td> <td>Assign a name to the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--net</code></td> <td></td> <td>Connect a container to a network</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--net-alias</code></td> <td></td> <td>Add network-scoped alias for the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--network</code></td> <td></td> <td>Connect a container to a network</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--network-alias</code></td> <td></td> <td>Add network-scoped alias for the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--no-healthcheck</code></td> <td></td> <td>Disable any container-specified HEALTHCHECK</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--oom-kill-disable</code></td> <td></td> <td>Disable OOM Killer</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--oom-score-adj</code></td> <td></td> <td>Tune host's OOM preferences (-1000 to 1000)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--pid</code></td> <td></td> <td>PID namespace to use</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--pids-limit</code></td> <td></td> <td>Tune container pids limit (set -1 for unlimited)</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--platform</code></td> <td></td> <td>Set platform if server is multi-platform capable</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--privileged</code></td> <td></td> <td>Give extended privileges to this container</td> </tr> <tr> <td> +<code class="language-plaintext highlighter-rouge">--publish</code> , <code class="language-plaintext highlighter-rouge">-p</code> +</td> <td></td> <td>Publish a container's port(s) to the host</td> </tr> <tr> <td> +<code class="language-plaintext highlighter-rouge">--publish-all</code> , <code class="language-plaintext highlighter-rouge">-P</code> +</td> <td></td> <td>Publish all exposed ports to random ports</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--pull</code></td> <td><code class="language-plaintext highlighter-rouge">missing</code></td> <td>Pull image before running ("always"|"missing"|"never")</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--read-only</code></td> <td></td> <td>Mount the container's root filesystem as read only</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--restart</code></td> <td><code class="language-plaintext highlighter-rouge">no</code></td> <td>Restart policy to apply when a container exits</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--rm</code></td> <td></td> <td>Automatically remove the container when it exits</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--runtime</code></td> <td></td> <td>Runtime to use for this container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--security-opt</code></td> <td></td> <td>Security Options</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--shm-size</code></td> <td></td> <td>Size of /dev/shm</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--sig-proxy</code></td> <td><code class="language-plaintext highlighter-rouge">true</code></td> <td>Proxy received signals to the process</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--stop-signal</code></td> <td><code class="language-plaintext highlighter-rouge">SIGTERM</code></td> <td>Signal to stop a container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--stop-timeout</code></td> <td></td> <td>Timeout (in seconds) to stop a container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--storage-opt</code></td> <td></td> <td>Storage driver options for the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--sysctl</code></td> <td></td> <td>Sysctl options</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--tmpfs</code></td> <td></td> <td>Mount a tmpfs directory</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">--ulimit</code></td> <td></td> <td>Ulimit options</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: <name|uid>[:<group|gid>])</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--userns</code></td> <td></td> <td>User namespace to use</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--uts</code></td> <td></td> <td>UTS namespace to use</td> </tr> <tr> <td> +<code class="language-plaintext highlighter-rouge">--volume</code> , <code class="language-plaintext highlighter-rouge">-v</code> +</td> <td></td> <td>Bind mount a volume</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--volume-driver</code></td> <td></td> <td>Optional volume driver for the container</td> </tr> <tr> <td><code class="language-plaintext highlighter-rouge">--volumes-from</code></td> <td></td> <td>Mount volumes from the specified container(s)</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="assign-name-and-allocate-pseudo-tty---name--it">Assign name and allocate pseudo-TTY (--name, -it)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run --name test -it debian + +root@d6c0fe130dba:/# exit 13 +$ echo $? +13 +$ docker ps -a | grep test +d6c0fe130dba debian:7 "/bin/bash" 26 seconds ago Exited (13) 17 seconds ago test +</pre></div> <p>This example runs a container named <code class="language-plaintext highlighter-rouge">test</code> using the <code class="language-plaintext highlighter-rouge">debian:latest</code> image. The <code class="language-plaintext highlighter-rouge">-it</code> instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive <code class="language-plaintext highlighter-rouge">bash</code> shell in the container. In the example, the <code class="language-plaintext highlighter-rouge">bash</code> shell is quit by entering <code class="language-plaintext highlighter-rouge">exit 13</code>. This exit code is passed on to the caller of <code class="language-plaintext highlighter-rouge">docker run</code>, and is recorded in the <code class="language-plaintext highlighter-rouge">test</code> container’s metadata.</p> <h3 id="capture-container-id---cidfile">Capture container ID (--cidfile)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run --cidfile /tmp/docker_test.cid ubuntu echo "test" +</pre></div> <p>This will create a container and print <code class="language-plaintext highlighter-rouge">test</code> to the console. The <code class="language-plaintext highlighter-rouge">cidfile</code> flag makes Docker attempt to create a new file and write the container ID to it. If the file exists already, Docker will return an error. Docker will close this file when <code class="language-plaintext highlighter-rouge">docker run</code> exits.</p> <h3 id="full-container-capabilities---privileged">Full container capabilities (--privileged)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run -t -i --rm ubuntu bash +root@bc338942ef20:/# mount -t tmpfs none /mnt +mount: permission denied +</pre></div> <p>This will <em>not</em> work, because by default, most potentially dangerous kernel capabilities are dropped; including <code class="language-plaintext highlighter-rouge">cap_sys_admin</code> (which is required to mount filesystems). However, the <code class="language-plaintext highlighter-rouge">--privileged</code> flag will allow it to run:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -t -i --privileged ubuntu bash +root@50e3f57e16e6:/# mount -t tmpfs none /mnt +root@50e3f57e16e6:/# df -h +Filesystem Size Used Avail Use% Mounted on +none 1.9G 0 1.9G 0% /mnt +</pre></div> <p>The <code class="language-plaintext highlighter-rouge">--privileged</code> flag gives <em>all</em> capabilities to the container, and it also lifts all the limitations enforced by the <code class="language-plaintext highlighter-rouge">device</code> cgroup controller. In other words, the container can then do almost everything that the host can do. This flag exists to allow special use-cases, like running Docker within Docker.</p> <h3 id="set-working-directory--w">Set working directory (-w)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run -w /path/to/dir/ -i -t ubuntu pwd +</pre></div> <p>The <code class="language-plaintext highlighter-rouge">-w</code> lets the command being executed inside directory given, here <code class="language-plaintext highlighter-rouge">/path/to/dir/</code>. If the path does not exist it is created inside the container.</p> <h3 id="set-storage-driver-options-per-container">Set storage driver options per container</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run -it --storage-opt size=120G fedora /bin/bash +</pre></div> <p>This (size) will allow to set the container rootfs size to 120G at creation time. This option is only available for the <code class="language-plaintext highlighter-rouge">devicemapper</code>, <code class="language-plaintext highlighter-rouge">btrfs</code>, <code class="language-plaintext highlighter-rouge">overlay2</code>, <code class="language-plaintext highlighter-rouge">windowsfilter</code> and <code class="language-plaintext highlighter-rouge">zfs</code> graph drivers. For the <code class="language-plaintext highlighter-rouge">devicemapper</code>, <code class="language-plaintext highlighter-rouge">btrfs</code>, <code class="language-plaintext highlighter-rouge">windowsfilter</code> and <code class="language-plaintext highlighter-rouge">zfs</code> graph drivers, user cannot pass a size less than the Default BaseFS Size. For the <code class="language-plaintext highlighter-rouge">overlay2</code> storage driver, the size option is only available if the backing fs is <code class="language-plaintext highlighter-rouge">xfs</code> and mounted with the <code class="language-plaintext highlighter-rouge">pquota</code> mount option. Under these conditions, user can pass any size less than the backing fs size.</p> <h3 id="mount-tmpfs---tmpfs">Mount tmpfs (--tmpfs)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run -d --tmpfs /run:rw,noexec,nosuid,size=65536k my_image +</pre></div> <p>The <code class="language-plaintext highlighter-rouge">--tmpfs</code> flag mounts an empty tmpfs into the container with the <code class="language-plaintext highlighter-rouge">rw</code>, <code class="language-plaintext highlighter-rouge">noexec</code>, <code class="language-plaintext highlighter-rouge">nosuid</code>, <code class="language-plaintext highlighter-rouge">size=65536k</code> options.</p> <h3 id="mount-volume--v---read-only">Mount volume (-v, --read-only)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run -v `pwd`:`pwd` -w `pwd` -i -t ubuntu pwd +</pre></div> <p>The <code class="language-plaintext highlighter-rouge">-v</code> flag mounts the current working directory into the container. The <code class="language-plaintext highlighter-rouge">-w</code> lets the command being executed inside the current working directory, by changing into the directory to the value returned by <code class="language-plaintext highlighter-rouge">pwd</code>. So this combination executes the command using the container, but inside the current working directory.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -v /doesnt/exist:/foo -w /foo -i -t ubuntu bash +</pre></div> <p>When the host directory of a bind-mounted volume doesn’t exist, Docker will automatically create this directory on the host for you. In the example above, Docker will create the <code class="language-plaintext highlighter-rouge">/doesnt/exist</code> folder before starting your container.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --read-only -v /icanwrite busybox touch /icanwrite/here +</pre></div> <p>Volumes can be used in combination with <code class="language-plaintext highlighter-rouge">--read-only</code> to control where a container writes files. The <code class="language-plaintext highlighter-rouge">--read-only</code> flag mounts the container’s root filesystem as read only prohibiting writes to locations other than the specified volumes for the container.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -t -i -v /var/run/docker.sock:/var/run/docker.sock -v /path/to/static-docker-binary:/usr/bin/docker busybox sh +</pre></div> <p>By bind-mounting the docker unix socket and statically linked docker binary (refer to <a href="../../../install/binaries/index#install-static-binaries">get the linux binary</a>), you give the container the full access to create and manipulate the host’s Docker daemon.</p> <p>On Windows, the paths must be specified using Windows-style semantics.</p> <div class="highlight"><pre class="highlight" data-language="">PS C:\> docker run -v c:\foo:c:\dest microsoft/nanoserver cmd /s /c type c:\dest\somefile.txt +Contents of file + +PS C:\> docker run -v c:\foo:d: microsoft/nanoserver cmd /s /c type d:\somefile.txt +Contents of file +</pre></div> <p>The following examples will fail when using Windows-based containers, as the destination of a volume or bind mount inside the container must be one of: a non-existing or empty directory; or a drive other than C:. Further, the source of a bind mount must be a local directory, not a file.</p> <div class="highlight"><pre class="highlight" data-language="">net use z: \\remotemachine\share +docker run -v z:\foo:c:\dest ... +docker run -v \\uncpath\to\directory:c:\dest ... +docker run -v c:\foo\somefile.txt:c:\dest ... +docker run -v c:\foo:c: ... +docker run -v c:\foo:c:\existing-directory-with-contents ... +</pre></div> <p>For in-depth information about volumes, refer to <a href="https://docs.docker.com/storage/volumes/">manage data in containers</a></p> <h3 id="add-bind-mounts-or-volumes-using-the---mount-flag">Add bind mounts or volumes using the --mount flag</h3> <p>The <code class="language-plaintext highlighter-rouge">--mount</code> flag allows you to mount volumes, host-directories and <code class="language-plaintext highlighter-rouge">tmpfs</code> mounts in a container.</p> <p>The <code class="language-plaintext highlighter-rouge">--mount</code> flag supports most options that are supported by the <code class="language-plaintext highlighter-rouge">-v</code> or the <code class="language-plaintext highlighter-rouge">--volume</code> flag, but uses a different syntax. For in-depth information on the <code class="language-plaintext highlighter-rouge">--mount</code> flag, and a comparison between <code class="language-plaintext highlighter-rouge">--volume</code> and <code class="language-plaintext highlighter-rouge">--mount</code>, refer to the <a href="../service_create/index#add-bind-mounts-volumes-or-memory-filesystems">service create command reference</a>.</p> <p>Even though there is no plan to deprecate <code class="language-plaintext highlighter-rouge">--volume</code>, usage of <code class="language-plaintext highlighter-rouge">--mount</code> is recommended.</p> <p>Examples:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --read-only --mount type=volume,target=/icanwrite busybox touch /icanwrite/here +</pre></div> <div class="highlight"><pre class="highlight" data-language="">$ docker run -t -i --mount type=bind,src=/data,dst=/data busybox sh +</pre></div> <h3 id="publish-or-expose-port--p---expose">Publish or expose port (-p, --expose)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash +</pre></div> <p>This binds port <code class="language-plaintext highlighter-rouge">8080</code> of the container to TCP port <code class="language-plaintext highlighter-rouge">80</code> on <code class="language-plaintext highlighter-rouge">127.0.0.1</code> of the host machine. You can also specify <code class="language-plaintext highlighter-rouge">udp</code> and <code class="language-plaintext highlighter-rouge">sctp</code> ports. The <a href="https://docs.docker.com/network/links/">Docker User Guide</a> explains in detail how to manipulate ports in Docker.</p> <p>Note that ports which are not bound to the host (i.e., <code class="language-plaintext highlighter-rouge">-p 80:80</code> instead of <code class="language-plaintext highlighter-rouge">-p 127.0.0.1:80:80</code>) will be accessible from the outside. This also applies if you configured UFW to block this specific port, as Docker manages its own iptables rules. <a href="https://docs.docker.com/network/iptables/">Read more</a></p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --expose 80 ubuntu bash +</pre></div> <p>This exposes port <code class="language-plaintext highlighter-rouge">80</code> of the container without publishing the port to the host system’s interfaces.</p> <h3 id="-set-the-pull-policy---pull"> + Set the pull policy (--pull)</h3> <p>Use the <code class="language-plaintext highlighter-rouge">--pull</code> flag to set the image pull policy when creating (and running) the container.</p> <p>The <code class="language-plaintext highlighter-rouge">--pull</code> flag can take one of these values:</p> <table> <thead> <tr> <th style="text-align: left">Value</th> <th style="text-align: left">Description</th> </tr> </thead> <tbody> <tr> <td style="text-align: left"> +<code class="language-plaintext highlighter-rouge">missing</code> (default)</td> <td style="text-align: left">Pull the image if it was not found in the image cache, or use the cached image otherwise.</td> </tr> <tr> <td style="text-align: left"><code class="language-plaintext highlighter-rouge">never</code></td> <td style="text-align: left">Do not pull the image, even if it’s missing, and produce an error if the image does not exist in the image cache.</td> </tr> <tr> <td style="text-align: left"><code class="language-plaintext highlighter-rouge">always</code></td> <td style="text-align: left">Always perform a pull before creating the container.</td> </tr> </tbody> </table> <p>When creating (and running) a container from an image, the daemon checks if the image exists in the local image cache. If the image is missing, an error is returned to the cli, allowing it to initiate a pull.</p> <p>The default (<code class="language-plaintext highlighter-rouge">missing</code>) is to only pull the image if it is not present in the daemon’s image cache. This default allows you to run images that only exist locally (for example, images you built from a Dockerfile, but that have not been pushed to a registry), and reduces networking.</p> <p>The <code class="language-plaintext highlighter-rouge">always</code> option always initiates a pull before creating the container. This option makes sure the image is up-to-date, and prevents you from using outdated images, but may not be suitable in situations where you want to test a locally built image before pushing (as pulling the image overwrites the existing image in the image cache).</p> <p>The <code class="language-plaintext highlighter-rouge">never</code> option disables (implicit) pulling images when creating containers, and only uses images that are available in the image cache. If the specified image is not found, an error is produced, and the container is not created. This option is useful in situations where networking is not available, or to prevent images from being pulled implicitly when creating containers.</p> <p>The following example shows <code class="language-plaintext highlighter-rouge">docker run</code> with the <code class="language-plaintext highlighter-rouge">--pull=never</code> option set, which produces en error as the image is missing in the image-cache:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --pull=never hello-world +docker: Error response from daemon: No such image: hello-world:latest. +</pre></div> <h3 id="set-environment-variables--e---env---env-file">Set environment variables (-e, --env, --env-file)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash +</pre></div> <p>Use the <code class="language-plaintext highlighter-rouge">-e</code>, <code class="language-plaintext highlighter-rouge">--env</code>, and <code class="language-plaintext highlighter-rouge">--env-file</code> flags to set simple (non-array) environment variables in the container you’re running, or overwrite variables that are defined in the Dockerfile of the image you’re running.</p> <p>You can define the variable and its value when running the container:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --env VAR1=value1 --env VAR2=value2 ubuntu env | grep VAR +VAR1=value1 +VAR2=value2 +</pre></div> <p>You can also use variables that you’ve exported to your local environment:</p> <div class="highlight"><pre class="highlight" data-language="">export VAR1=value1 +export VAR2=value2 + +$ docker run --env VAR1 --env VAR2 ubuntu env | grep VAR +VAR1=value1 +VAR2=value2 +</pre></div> <p>When running the command, the Docker CLI client checks the value the variable has in your local environment and passes it to the container. If no <code class="language-plaintext highlighter-rouge">=</code> is provided and that variable is not exported in your local environment, the variable won’t be set in the container.</p> <p>You can also load the environment variables from a file. This file should use the syntax <code class="language-plaintext highlighter-rouge"><variable>=value</code> (which sets the variable to the given value) or <code class="language-plaintext highlighter-rouge"><variable></code> (which takes the value from the local environment), and <code class="language-plaintext highlighter-rouge">#</code> for comments.</p> <div class="highlight"><pre class="highlight" data-language="">$ cat env.list +# This is a comment +VAR1=value1 +VAR2=value2 +USER + +$ docker run --env-file env.list ubuntu env | grep -E 'VAR|USER' +VAR1=value1 +VAR2=value2 +USER=jonzeolla +</pre></div> <h3 id="set-metadata-on-container--l---label---label-file">Set metadata on container (-l, --label, --label-file)</h3> <p>A label is a <code class="language-plaintext highlighter-rouge">key=value</code> pair that applies metadata to a container. To label a container with two labels:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -l my-label --label com.example.foo=bar ubuntu bash +</pre></div> <p>The <code class="language-plaintext highlighter-rouge">my-label</code> key doesn’t specify a value so the label defaults to an empty string (<code class="language-plaintext highlighter-rouge">""</code>). To add multiple labels, repeat the label flag (<code class="language-plaintext highlighter-rouge">-l</code> or <code class="language-plaintext highlighter-rouge">--label</code>).</p> <p>The <code class="language-plaintext highlighter-rouge">key=value</code> must be unique to avoid overwriting the label value. If you specify labels with identical keys but different values, each subsequent value overwrites the previous. Docker uses the last <code class="language-plaintext highlighter-rouge">key=value</code> you supply.</p> <p>Use the <code class="language-plaintext highlighter-rouge">--label-file</code> flag to load multiple labels from a file. Delimit each label in the file with an EOL mark. The example below loads labels from a labels file in the current directory:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --label-file ./labels ubuntu bash +</pre></div> <p>The label-file format is similar to the format for loading environment variables. (Unlike environment variables, labels are not visible to processes running inside a container.) The following example illustrates a label-file format:</p> <div class="highlight"><pre class="highlight" data-language="">com.example.label1="a label" + +# this is a comment +com.example.label2=another\ label +com.example.label3 +</pre></div> <p>You can load multiple label-files by supplying multiple <code class="language-plaintext highlighter-rouge">--label-file</code> flags.</p> <p>For additional information on working with labels, see <a href="https://docs.docker.com/config/labels-custom-metadata/"><em>Labels - custom metadata in Docker</em></a> in the Docker User Guide.</p> <h3 id="connect-a-container-to-a-network---network">Connect a container to a network (--network)</h3> <p>When you start a container use the <code class="language-plaintext highlighter-rouge">--network</code> flag to connect it to a network. This adds the <code class="language-plaintext highlighter-rouge">busybox</code> container to the <code class="language-plaintext highlighter-rouge">my-net</code> network.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -itd --network=my-net busybox +</pre></div> <p>You can also choose the IP addresses for the container with <code class="language-plaintext highlighter-rouge">--ip</code> and <code class="language-plaintext highlighter-rouge">--ip6</code> flags when you start the container on a user-defined network.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -itd --network=my-net --ip=10.10.9.75 busybox +</pre></div> <p>If you want to add a running container to a network use the <code class="language-plaintext highlighter-rouge">docker network connect</code> subcommand.</p> <p>You can connect multiple containers to the same network. Once connected, the containers can communicate easily using only another container’s IP address or name. For <code class="language-plaintext highlighter-rouge">overlay</code> networks or custom plugins that support multi-host connectivity, containers connected to the same multi-host network but launched from different Engines can also communicate in this way.</p> <blockquote> <p><strong>Note</strong></p> <p>Service discovery is unavailable on the default bridge network. Containers can communicate via their IP addresses by default. To communicate by name, they must be linked.</p> </blockquote> <p>You can disconnect a container from a network using the <code class="language-plaintext highlighter-rouge">docker network disconnect</code> command.</p> <h3 id="mount-volumes-from-container---volumes-from">Mount volumes from container (--volumes-from)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run --volumes-from 777f7dc92da7 --volumes-from ba8c0c54f0f2:ro -i -t ubuntu pwd +</pre></div> <p>The <code class="language-plaintext highlighter-rouge">--volumes-from</code> flag mounts all the defined volumes from the referenced containers. Containers can be specified by repetitions of the <code class="language-plaintext highlighter-rouge">--volumes-from</code> argument. The container ID may be optionally suffixed with <code class="language-plaintext highlighter-rouge">:ro</code> or <code class="language-plaintext highlighter-rouge">:rw</code> to mount the volumes in read-only or read-write mode, respectively. By default, the volumes are mounted in the same mode (read write or read only) as the reference container.</p> <p>Labeling systems like SELinux require that proper labels are placed on volume content mounted into a container. Without a label, the security system might prevent the processes running inside the container from using the content. By default, Docker does not change the labels set by the OS.</p> <p>To change the label in the container context, you can add either of two suffixes <code class="language-plaintext highlighter-rouge">:z</code> or <code class="language-plaintext highlighter-rouge">:Z</code> to the volume mount. These suffixes tell Docker to relabel file objects on the shared volumes. The <code class="language-plaintext highlighter-rouge">z</code> option tells Docker that two containers share the volume content. As a result, Docker labels the content with a shared content label. Shared volume labels allow all containers to read/write content. The <code class="language-plaintext highlighter-rouge">Z</code> option tells Docker to label the content with a private unshared label. Only the current container can use a private volume.</p> <h3 id="attach-to-stdinstdoutstderr--a">Attach to STDIN/STDOUT/STDERR (-a)</h3> <p>The <code class="language-plaintext highlighter-rouge">-a</code> flag tells <code class="language-plaintext highlighter-rouge">docker run</code> to bind to the container’s <code class="language-plaintext highlighter-rouge">STDIN</code>, <code class="language-plaintext highlighter-rouge">STDOUT</code> or <code class="language-plaintext highlighter-rouge">STDERR</code>. This makes it possible to manipulate the output and input as needed.</p> <div class="highlight"><pre class="highlight" data-language="">$ echo "test" | docker run -i -a stdin ubuntu cat - +</pre></div> <p>This pipes data into a container and prints the container’s ID by attaching only to the container’s <code class="language-plaintext highlighter-rouge">STDIN</code>.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -a stderr ubuntu echo test +</pre></div> <p>This isn’t going to print anything unless there’s an error because we’ve only attached to the <code class="language-plaintext highlighter-rouge">STDERR</code> of the container. The container’s logs still store what’s been written to <code class="language-plaintext highlighter-rouge">STDERR</code> and <code class="language-plaintext highlighter-rouge">STDOUT</code>.</p> <div class="highlight"><pre class="highlight" data-language="">$ cat somefile | docker run -i -a stdin mybuilder dobuild +</pre></div> <p>This is how piping a file into a container could be done for a build. The container’s ID will be printed after the build is done and the build logs could be retrieved using <code class="language-plaintext highlighter-rouge">docker logs</code>. This is useful if you need to pipe a file or something else into a container and retrieve the container’s ID once the container has finished running.</p> <h3 id="add-host-device-to-container---device">Add host device to container (--device)</h3> <div class="highlight"><pre class="highlight" data-language="">$ docker run --device=/dev/sdc:/dev/xvdc \ + --device=/dev/sdd --device=/dev/zero:/dev/nulo \ + -i -t \ + ubuntu ls -l /dev/{xvdc,sdd,nulo} + +brw-rw---- 1 root disk 8, 2 Feb 9 16:05 /dev/xvdc +brw-rw---- 1 root disk 8, 3 Feb 9 16:05 /dev/sdd +crw-rw-rw- 1 root root 1, 5 Feb 9 16:05 /dev/nulo +</pre></div> <p>It is often necessary to directly expose devices to a container. The <code class="language-plaintext highlighter-rouge">--device</code> option enables that. For example, a specific block storage device or loop device or audio device can be added to an otherwise unprivileged container (without the <code class="language-plaintext highlighter-rouge">--privileged</code> flag) and have the application directly access it.</p> <p>By default, the container will be able to <code class="language-plaintext highlighter-rouge">read</code>, <code class="language-plaintext highlighter-rouge">write</code> and <code class="language-plaintext highlighter-rouge">mknod</code> these devices. This can be overridden using a third <code class="language-plaintext highlighter-rouge">:rwm</code> set of options to each <code class="language-plaintext highlighter-rouge">--device</code> flag. If the container is running in privileged mode, then the permissions specified will be ignored.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc + +Command (m for help): q +$ docker run --device=/dev/sda:/dev/xvdc:r --rm -it ubuntu fdisk /dev/xvdc +You will not be able to write the partition table. + +Command (m for help): q + +$ docker run --device=/dev/sda:/dev/xvdc:rw --rm -it ubuntu fdisk /dev/xvdc + +Command (m for help): q + +$ docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc +fdisk: unable to open /dev/xvdc: Operation not permitted +</pre></div> <blockquote> <p><strong>Note</strong></p> <p>The <code class="language-plaintext highlighter-rouge">--device</code> option cannot be safely used with ephemeral devices. Block devices that may be removed should not be added to untrusted containers with <code class="language-plaintext highlighter-rouge">--device</code>.</p> </blockquote> <p>For Windows, the format of the string passed to the <code class="language-plaintext highlighter-rouge">--device</code> option is in the form of <code class="language-plaintext highlighter-rouge">--device=<IdType>/<Id></code>. Beginning with Windows Server 2019 and Windows 10 October 2018 Update, Windows only supports an IdType of <code class="language-plaintext highlighter-rouge">class</code> and the Id as a <a href="https://docs.microsoft.com/en-us/windows-hardware/drivers/install/overview-of-device-interface-classes">device interface class GUID</a>. Refer to the table defined in the <a href="https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/hardware-devices-in-containers">Windows container docs</a> for a list of container-supported device interface class GUIDs.</p> <p>If this option is specified for a process-isolated Windows container, <em>all</em> devices that implement the requested device interface class GUID are made available in the container. For example, the command below makes all COM ports on the host visible in the container.</p> <div class="highlight"><pre class="highlight" data-language="">PS C:\> docker run --device=class/86E0D1E0-8089-11D0-9CE4-08003E301F73 mcr.microsoft.com/windows/servercore:ltsc2019 +</pre></div> <blockquote> <p><strong>Note</strong></p> <p>The <code class="language-plaintext highlighter-rouge">--device</code> option is only supported on process-isolated Windows containers. This option fails if the container isolation is <code class="language-plaintext highlighter-rouge">hyperv</code> or when running Linux Containers on Windows (LCOW).</p> </blockquote> <h3 id="-using-dynamically-created-devices---device-cgroup-rule"> + Using dynamically created devices (--device-cgroup-rule)</h3> <p>Devices available to a container are assigned at creation time. The assigned devices will both be added to the cgroup.allow file and created into the container once it is run. This poses a problem when a new device needs to be added to running container.</p> <p>One of the solutions is to add a more permissive rule to a container allowing it access to a wider range of devices. For example, supposing our container needs access to a character device with major <code class="language-plaintext highlighter-rouge">42</code> and any number of minor number (added as new devices appear), the following rule would be added:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -d --device-cgroup-rule='c 42:* rmw' -name my-container my-image +</pre></div> <p>Then, a user could ask <code class="language-plaintext highlighter-rouge">udev</code> to execute a script that would <code class="language-plaintext highlighter-rouge">docker exec my-container mknod newDevX c 42 <minor></code> the required device when it is added.</p> <blockquote> <p><strong>Note</strong>: initially present devices still need to be explicitly added to the <code class="language-plaintext highlighter-rouge">docker run</code> / <code class="language-plaintext highlighter-rouge">docker create</code> command.</p> </blockquote> <h3 id="access-an-nvidia-gpu">Access an NVIDIA GPU</h3> <p>The <code class="language-plaintext highlighter-rouge">--gpus</code> flag allows you to access NVIDIA GPU resources. First you need to install <a href="https://nvidia.github.io/nvidia-container-runtime/">nvidia-container-runtime</a>. Visit <a href="https://docs.docker.com/config/containers/resource_constraints/">Specify a container’s resources</a> for more information.</p> <p>To use <code class="language-plaintext highlighter-rouge">--gpus</code>, specify which GPUs (or all) to use. If no value is provied, all available GPUs are used. The example below exposes all available GPUs.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -it --rm --gpus all ubuntu nvidia-smi +</pre></div> <p>Use the <code class="language-plaintext highlighter-rouge">device</code> option to specify GPUs. The example below exposes a specific GPU.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -it --rm --gpus device=GPU-3a23c669-1f69-c64e-cf85-44e9b07e7a2a ubuntu nvidia-smi +</pre></div> <p>The example below exposes the first and third GPUs.</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -it --rm --gpus '"device=0,2"' nvidia-smi +</pre></div> <h3 id="restart-policies---restart">Restart policies (--restart)</h3> <p>Use Docker’s <code class="language-plaintext highlighter-rouge">--restart</code> to specify a container’s <em>restart policy</em>. A restart policy controls whether the Docker daemon restarts a container after exit. Docker supports the following restart policies:</p> <table> <thead> <tr> <th style="text-align: left">Policy</th> <th style="text-align: left">Result</th> </tr> </thead> <tbody> <tr> <td style="text-align: left"><code class="language-plaintext highlighter-rouge">no</code></td> <td style="text-align: left">Do not automatically restart the container when it exits. This is the default.</td> </tr> <tr> <td style="text-align: left"><code class="language-plaintext highlighter-rouge">on-failure[:max-retries]</code></td> <td style="text-align: left">Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts.</td> </tr> <tr> <td style="text-align: left"><code class="language-plaintext highlighter-rouge">unless-stopped</code></td> <td style="text-align: left">Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted.</td> </tr> <tr> <td style="text-align: left"><code class="language-plaintext highlighter-rouge">always</code></td> <td style="text-align: left">Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container.</td> </tr> </tbody> </table> <div class="highlight"><pre class="highlight" data-language="">$ docker run --restart=always redis +</pre></div> <p>This will run the <code class="language-plaintext highlighter-rouge">redis</code> container with a restart policy of <strong>always</strong> so that if the container exits, Docker will restart it.</p> <p>More detailed information on restart policies can be found in the <a href="../../run/index#restart-policies---restart">Restart Policies (--restart)</a> section of the Docker run reference page.</p> <h3 id="add-entries-to-container-hosts-file---add-host">Add entries to container hosts file (--add-host)</h3> <p>You can add other hosts into a container’s <code class="language-plaintext highlighter-rouge">/etc/hosts</code> file by using one or more <code class="language-plaintext highlighter-rouge">--add-host</code> flags. This example adds a static address for a host named <code class="language-plaintext highlighter-rouge">docker</code>:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --add-host=docker:93.184.216.34 --rm -it alpine + +/ # ping docker +PING docker (93.184.216.34): 56 data bytes +64 bytes from 93.184.216.34: seq=0 ttl=37 time=93.052 ms +64 bytes from 93.184.216.34: seq=1 ttl=37 time=92.467 ms +64 bytes from 93.184.216.34: seq=2 ttl=37 time=92.252 ms +^C +--- docker ping statistics --- +4 packets transmitted, 4 packets received, 0% packet loss +round-trip min/avg/max = 92.209/92.495/93.052 ms +</pre></div> <p>Sometimes you need to connect to the Docker host from within your container. To enable this, pass the Docker host’s IP address to the container using the <code class="language-plaintext highlighter-rouge">--add-host</code> flag. To find the host’s address, use the <code class="language-plaintext highlighter-rouge">ip addr show</code> command.</p> <p>The flags you pass to <code class="language-plaintext highlighter-rouge">ip addr show</code> depend on whether you are using IPv4 or IPv6 networking in your containers. Use the following flags for IPv4 address retrieval for a network device named <code class="language-plaintext highlighter-rouge">eth0</code>:</p> <div class="highlight"><pre class="highlight" data-language="">$ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print $2}' | cut -d / -f 1 | sed -n 1p` +$ docker run --add-host=docker:${HOSTIP} --rm -it debian +</pre></div> <p>For IPv6 use the <code class="language-plaintext highlighter-rouge">-6</code> flag instead of the <code class="language-plaintext highlighter-rouge">-4</code> flag. For other network devices, replace <code class="language-plaintext highlighter-rouge">eth0</code> with the correct device name (for example <code class="language-plaintext highlighter-rouge">docker0</code> for the bridge device).</p> <h3 id="set-ulimits-in-container---ulimit">Set ulimits in container (--ulimit)</h3> <p>Since setting <code class="language-plaintext highlighter-rouge">ulimit</code> settings in a container requires extra privileges not available in the default container, you can set these using the <code class="language-plaintext highlighter-rouge">--ulimit</code> flag. <code class="language-plaintext highlighter-rouge">--ulimit</code> is specified with a soft and hard limit as such: <code class="language-plaintext highlighter-rouge"><type>=<soft limit>[:<hard limit>]</code>, for example:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --ulimit nofile=1024:1024 --rm debian sh -c "ulimit -n" +1024 +</pre></div> <blockquote> <p><strong>Note</strong></p> <p>If you do not provide a <code class="language-plaintext highlighter-rouge">hard limit</code>, the <code class="language-plaintext highlighter-rouge">soft limit</code> is used for both values. If no <code class="language-plaintext highlighter-rouge">ulimits</code> are set, they are inherited from the default <code class="language-plaintext highlighter-rouge">ulimits</code> set on the daemon. The <code class="language-plaintext highlighter-rouge">as</code> option is disabled now. In other words, the following script is not supported:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -it --ulimit as=1024 fedora /bin/bash +</pre></div> </blockquote> <p>The values are sent to the appropriate <code class="language-plaintext highlighter-rouge">syscall</code> as they are set. Docker doesn’t perform any byte conversion. Take this into account when setting the values.</p> <h4 id="for-nproc-usage">For <code class="language-plaintext highlighter-rouge">nproc</code> usage</h4> <p>Be careful setting <code class="language-plaintext highlighter-rouge">nproc</code> with the <code class="language-plaintext highlighter-rouge">ulimit</code> flag as <code class="language-plaintext highlighter-rouge">nproc</code> is designed by Linux to set the maximum number of processes available to a user, not to a container. For example, start four containers with <code class="language-plaintext highlighter-rouge">daemon</code> user:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -d -u daemon --ulimit nproc=3 busybox top + +$ docker run -d -u daemon --ulimit nproc=3 busybox top + +$ docker run -d -u daemon --ulimit nproc=3 busybox top + +$ docker run -d -u daemon --ulimit nproc=3 busybox top +</pre></div> <p>The 4th container fails and reports “[8] System error: resource temporarily unavailable” error. This fails because the caller set <code class="language-plaintext highlighter-rouge">nproc=3</code> resulting in the first three containers using up the three processes quota set for the <code class="language-plaintext highlighter-rouge">daemon</code> user.</p> <h3 id="stop-container-with-signal---stop-signal">Stop container with signal (--stop-signal)</h3> <p>The <code class="language-plaintext highlighter-rouge">--stop-signal</code> flag sets the system call signal that will be sent to the container to exit. This signal can be a signal name in the format <code class="language-plaintext highlighter-rouge">SIG<NAME></code>, for instance <code class="language-plaintext highlighter-rouge">SIGKILL</code>, or an unsigned number that matches a position in the kernel’s syscall table, for instance <code class="language-plaintext highlighter-rouge">9</code>.</p> <p>The default is <code class="language-plaintext highlighter-rouge">SIGTERM</code> if not specified.</p> <h3 id="optional-security-options---security-opt">Optional security options (--security-opt)</h3> <p>On Windows, this flag can be used to specify the <code class="language-plaintext highlighter-rouge">credentialspec</code> option. The <code class="language-plaintext highlighter-rouge">credentialspec</code> must be in the format <code class="language-plaintext highlighter-rouge">file://spec.txt</code> or <code class="language-plaintext highlighter-rouge">registry://keyname</code>.</p> <h3 id="stop-container-with-timeout---stop-timeout">Stop container with timeout (--stop-timeout)</h3> <p>The <code class="language-plaintext highlighter-rouge">--stop-timeout</code> flag sets the number of seconds to wait for the container to stop after sending the pre-defined (see <code class="language-plaintext highlighter-rouge">--stop-signal</code>) system call signal. If the container does not exit after the timeout elapses, it is forcibly killed with a <code class="language-plaintext highlighter-rouge">SIGKILL</code> signal.</p> <p>If <code class="language-plaintext highlighter-rouge">--stop-timeout</code> is set to <code class="language-plaintext highlighter-rouge">-1</code>, no timeout is applied, and the daemon will wait indefinitely for the container to exit.</p> <p>The default is determined by the daemon, and is 10 seconds for Linux containers, and 30 seconds for Windows containers.</p> <h3 id="specify-isolation-technology-for-container---isolation">Specify isolation technology for container (--isolation)</h3> <p>This option is useful in situations where you are running Docker containers on Windows. The <code class="language-plaintext highlighter-rouge">--isolation=<value></code> option sets a container’s isolation technology. On Linux, the only supported is the <code class="language-plaintext highlighter-rouge">default</code> option which uses Linux namespaces. These two commands are equivalent on Linux:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run -d busybox top +$ docker run -d --isolation default busybox top +</pre></div> <p>On Windows, <code class="language-plaintext highlighter-rouge">--isolation</code> can take one of these values:</p> <table> <thead> <tr> <th style="text-align: left">Value</th> <th style="text-align: left">Description</th> </tr> </thead> <tbody> <tr> <td style="text-align: left"><code class="language-plaintext highlighter-rouge">default</code></td> <td style="text-align: left">Use the value specified by the Docker daemon’s <code class="language-plaintext highlighter-rouge">--exec-opt</code> or system default (see below).</td> </tr> <tr> <td style="text-align: left"><code class="language-plaintext highlighter-rouge">process</code></td> <td style="text-align: left">Shared-kernel namespace isolation.</td> </tr> <tr> <td style="text-align: left"><code class="language-plaintext highlighter-rouge">hyperv</code></td> <td style="text-align: left">Hyper-V hypervisor partition-based isolation.</td> </tr> </tbody> </table> <p>The default isolation on Windows server operating systems is <code class="language-plaintext highlighter-rouge">process</code>, and <code class="language-plaintext highlighter-rouge">hyperv</code> on Windows client operating systems, such as Windows 10. Process isolation is more performant, but requires the image to</p> <p>On Windows server, assuming the default configuration, these commands are equivalent and result in <code class="language-plaintext highlighter-rouge">process</code> isolation:</p> <div class="highlight"><pre class="highlight" data-language="">PS C:\> docker run -d microsoft/nanoserver powershell echo process +PS C:\> docker run -d --isolation default microsoft/nanoserver powershell echo process +PS C:\> docker run -d --isolation process microsoft/nanoserver powershell echo process +</pre></div> <p>If you have set the <code class="language-plaintext highlighter-rouge">--exec-opt isolation=hyperv</code> option on the Docker <code class="language-plaintext highlighter-rouge">daemon</code>, or are running against a Windows client-based daemon, these commands are equivalent and result in <code class="language-plaintext highlighter-rouge">hyperv</code> isolation:</p> <div class="highlight"><pre class="highlight" data-language="">PS C:\> docker run -d microsoft/nanoserver powershell echo hyperv +PS C:\> docker run -d --isolation default microsoft/nanoserver powershell echo hyperv +PS C:\> docker run -d --isolation hyperv microsoft/nanoserver powershell echo hyperv +</pre></div> <h3 id="specify-hard-limits-on-memory-available-to-containers--m---memory">Specify hard limits on memory available to containers (-m, --memory)</h3> <p>These parameters always set an upper limit on the memory available to the container. On Linux, this is set on the cgroup and applications in a container can query it at <code class="language-plaintext highlighter-rouge">/sys/fs/cgroup/memory/memory.limit_in_bytes</code>.</p> <p>On Windows, this will affect containers differently depending on what type of isolation is used.</p> <ul> <li> <p>With <code class="language-plaintext highlighter-rouge">process</code> isolation, Windows will report the full memory of the host system, not the limit to applications running inside the container</p> <div class="highlight"><pre class="highlight" data-language=""> PS C:\> docker run -it -m 2GB --isolation=process microsoft/nanoserver powershell Get-ComputerInfo *memory* + + CsTotalPhysicalMemory : 17064509440 + CsPhyicallyInstalledMemory : 16777216 + OsTotalVisibleMemorySize : 16664560 + OsFreePhysicalMemory : 14646720 + OsTotalVirtualMemorySize : 19154928 + OsFreeVirtualMemory : 17197440 + OsInUseVirtualMemory : 1957488 + OsMaxProcessMemorySize : 137438953344 +</pre></div> </li> <li> <p>With <code class="language-plaintext highlighter-rouge">hyperv</code> isolation, Windows will create a utility VM that is big enough to hold the memory limit, plus the minimal OS needed to host the container. That size is reported as “Total Physical Memory.”</p> <div class="highlight"><pre class="highlight" data-language=""> PS C:\> docker run -it -m 2GB --isolation=hyperv microsoft/nanoserver powershell Get-ComputerInfo *memory* + + CsTotalPhysicalMemory : 2683355136 + CsPhyicallyInstalledMemory : + OsTotalVisibleMemorySize : 2620464 + OsFreePhysicalMemory : 2306552 + OsTotalVirtualMemorySize : 2620464 + OsFreeVirtualMemory : 2356692 + OsInUseVirtualMemory : 263772 + OsMaxProcessMemorySize : 137438953344 +</pre></div> </li> </ul> <h3 id="configure-namespaced-kernel-parameters-sysctls-at-runtime">Configure namespaced kernel parameters (sysctls) at runtime</h3> <p>The <code class="language-plaintext highlighter-rouge">--sysctl</code> sets namespaced kernel parameters (sysctls) in the container. For example, to turn on IP forwarding in the containers network namespace, run this command:</p> <div class="highlight"><pre class="highlight" data-language="">$ docker run --sysctl net.ipv4.ip_forward=1 someimage +</pre></div> <blockquote> <p><strong>Note</strong></p> <p>Not all sysctls are namespaced. Docker does not support changing sysctls inside of a container that also modify the host system. As the kernel evolves we expect to see more sysctls become namespaced.</p> </blockquote> <h4 id="currently-supported-sysctls">Currently supported sysctls</h4> <p>IPC Namespace:</p> <ul> <li> +<code class="language-plaintext highlighter-rouge">kernel.msgmax</code>, <code class="language-plaintext highlighter-rouge">kernel.msgmnb</code>, <code class="language-plaintext highlighter-rouge">kernel.msgmni</code>, <code class="language-plaintext highlighter-rouge">kernel.sem</code>, <code class="language-plaintext highlighter-rouge">kernel.shmall</code>, <code class="language-plaintext highlighter-rouge">kernel.shmmax</code>, <code class="language-plaintext highlighter-rouge">kernel.shmmni</code>, <code class="language-plaintext highlighter-rouge">kernel.shm_rmid_forced</code>.</li> <li>Sysctls beginning with <code class="language-plaintext highlighter-rouge">fs.mqueue.*</code> +</li> <li>If you use the <code class="language-plaintext highlighter-rouge">--ipc=host</code> option these sysctls are not allowed.</li> </ul> <p>Network Namespace:</p> <ul> <li>Sysctls beginning with <code class="language-plaintext highlighter-rouge">net.*</code> +</li> <li>If you use the <code class="language-plaintext highlighter-rouge">--network=host</code> option using these sysctls are not allowed.</li> </ul> <div class="_attribution"> + <p class="_attribution-p"> + © 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/run/" class="_attribution-link">https://docs.docker.com/engine/reference/commandline/run/</a> + </p> +</div> |
