github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/docs/reference/commandline/run.md (about) 1 --- 2 title: "run" 3 description: "The run command description and usage" 4 keywords: "run, command, container" 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 # run 17 18 ```markdown 19 Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 20 21 Run a command in a new container 22 23 Options: 24 --add-host value Add a custom host-to-IP mapping (host:ip) (default []) 25 -a, --attach value Attach to STDIN, STDOUT or STDERR (default []) 26 --blkio-weight value Block IO (relative weight), between 10 and 1000 27 --blkio-weight-device value Block IO weight (relative device weight) (default []) 28 --cap-add value Add Linux capabilities (default []) 29 --cap-drop value Drop Linux capabilities (default []) 30 --cgroup-parent string Optional parent cgroup for the container 31 --cidfile string Write the container ID to the file 32 --cpu-count int The number of CPUs available for execution by the container. 33 Windows daemon only. On Windows Server containers, this is 34 approximated as a percentage of total CPU usage. 35 --cpu-percent int Limit percentage of CPU available for execution 36 by the container. Windows daemon only. 37 The processor resource controls are mutually 38 exclusive, the order of precedence is CPUCount 39 first, then CPUShares, and CPUPercent last. 40 --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period 41 --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota 42 -c, --cpu-shares int CPU shares (relative weight) 43 --cpus NanoCPUs Number of CPUs (default 0.000) 44 --cpu-rt-period int Limit the CPU real-time period in microseconds 45 --cpu-rt-runtime int Limit the CPU real-time runtime in microseconds 46 --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) 47 --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) 48 -d, --detach Run container in background and print container ID 49 --detach-keys string Override the key sequence for detaching a container 50 --device value Add a host device to the container (default []) 51 --device-read-bps value Limit read rate (bytes per second) from a device (default []) 52 --device-read-iops value Limit read rate (IO per second) from a device (default []) 53 --device-write-bps value Limit write rate (bytes per second) to a device (default []) 54 --device-write-iops value Limit write rate (IO per second) to a device (default []) 55 --disable-content-trust Skip image verification (default true) 56 --dns value Set custom DNS servers (default []) 57 --dns-option value Set DNS options (default []) 58 --dns-search value Set custom DNS search domains (default []) 59 --entrypoint string Overwrite the default ENTRYPOINT of the image 60 -e, --env value Set environment variables (default []) 61 --env-file value Read in a file of environment variables (default []) 62 --expose value Expose a port or a range of ports (default []) 63 --group-add value Add additional groups to join (default []) 64 --health-cmd string Command to run to check health 65 --health-interval duration Time between running the check (ns|us|ms|s|m|h) (default 0s) 66 --health-retries int Consecutive failures needed to report unhealthy 67 --health-timeout duration Maximum time to allow one check to run (ns|us|ms|s|m|h) (default 0s) 68 --help Print usage 69 -h, --hostname string Container host name 70 -i, --interactive Keep STDIN open even if not attached 71 --io-maxbandwidth string Maximum IO bandwidth limit for the system drive (Windows only) 72 (Windows only). The format is `<number><unit>`. 73 Unit is optional and can be `b` (bytes per second), 74 `k` (kilobytes per second), `m` (megabytes per second), 75 or `g` (gigabytes per second). If you omit the unit, 76 the system uses bytes per second. 77 --io-maxbandwidth and --io-maxiops are mutually exclusive options. 78 --io-maxiops uint Maximum IOps limit for the system drive (Windows only) 79 --ip string IPv4 address (e.g., 172.30.100.104) 80 --ip6 string IPv6 address (e.g., 2001:db8::33) 81 --ipc string IPC namespace to use 82 --isolation string Container isolation technology 83 --kernel-memory string Kernel memory limit 84 -l, --label value Set meta data on a container (default []) 85 --label-file value Read in a line delimited file of labels (default []) 86 --link value Add link to another container (default []) 87 --link-local-ip value Container IPv4/IPv6 link-local addresses (default []) 88 --log-driver string Logging driver for the container 89 --log-opt value Log driver options (default []) 90 --mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33) 91 -m, --memory string Memory limit 92 --memory-reservation string Memory soft limit 93 --memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap 94 --memory-swappiness int Tune container memory swappiness (0 to 100) (default -1) 95 --name string Assign a name to the container 96 --network-alias value Add network-scoped alias for the container (default []) 97 --network string Connect a container to a network 98 'bridge': create a network stack on the default Docker bridge 99 'none': no networking 100 'container:<name|id>': reuse another container's network stack 101 'host': use the Docker host network stack 102 '<network-name>|<network-id>': connect to a user-defined network 103 --no-healthcheck Disable any container-specified HEALTHCHECK 104 --oom-kill-disable Disable OOM Killer 105 --oom-score-adj int Tune host's OOM preferences (-1000 to 1000) 106 --pid string PID namespace to use 107 --pids-limit int Tune container pids limit (set -1 for unlimited) 108 --privileged Give extended privileges to this container 109 -p, --publish value Publish a container's port(s) to the host (default []) 110 -P, --publish-all Publish all exposed ports to random ports 111 --read-only Mount the container's root filesystem as read only 112 --restart string Restart policy to apply when a container exits (default "no") 113 Possible values are : no, on-failure[:max-retry], always, unless-stopped 114 --rm Automatically remove the container when it exits 115 --runtime string Runtime to use for this container 116 --security-opt value Security Options (default []) 117 --shm-size string Size of /dev/shm, default value is 64MB. 118 The format is `<number><unit>`. `number` must be greater than `0`. 119 Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes), 120 or `g` (gigabytes). If you omit the unit, the system uses bytes. 121 --sig-proxy Proxy received signals to the process (default true) 122 --stop-signal string Signal to stop a container, SIGTERM by default (default "SIGTERM") 123 --stop-timeout=10 Timeout (in seconds) to stop a container 124 --storage-opt value Storage driver options for the container (default []) 125 --sysctl value Sysctl options (default map[]) 126 --tmpfs value Mount a tmpfs directory (default []) 127 -t, --tty Allocate a pseudo-TTY 128 --ulimit value Ulimit options (default []) 129 -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) 130 --userns string User namespace to use 131 'host': Use the Docker host user namespace 132 '': Use the Docker daemon user namespace specified by `--userns-remap` option. 133 --uts string UTS namespace to use 134 -v, --volume value Bind mount a volume (default []). The format 135 is `[host-src:]container-dest[:<options>]`. 136 The comma-delimited `options` are [rw|ro], 137 [z|Z], [[r]shared|[r]slave|[r]private], and 138 [nocopy]. The 'host-src' is an absolute path 139 or a name value. 140 --volume-driver string Optional volume driver for the container 141 --volumes-from value Mount volumes from the specified container(s) (default []) 142 -w, --workdir string Working directory inside the container 143 ``` 144 145 The `docker run` command first `creates` a writeable container layer over the 146 specified image, and then `starts` it using the specified command. That is, 147 `docker run` is equivalent to the API `/containers/create` then 148 `/containers/(id)/start`. A stopped container can be restarted with all its 149 previous changes intact using `docker start`. See `docker ps -a` to view a list 150 of all containers. 151 152 The `docker run` command can be used in combination with `docker commit` to 153 [*change the command that a container runs*](commit.md). There is additional detailed information about `docker run` in the [Docker run reference](../run.md). 154 155 For information on connecting a container to a network, see the ["*Docker network overview*"](https://docs.docker.com/engine/userguide/networking/). 156 157 ## Examples 158 159 ### Assign name and allocate pseudo-TTY (--name, -it) 160 161 $ docker run --name test -it debian 162 root@d6c0fe130dba:/# exit 13 163 $ echo $? 164 13 165 $ docker ps -a | grep test 166 d6c0fe130dba debian:7 "/bin/bash" 26 seconds ago Exited (13) 17 seconds ago test 167 168 This example runs a container named `test` using the `debian:latest` 169 image. The `-it` instructs Docker to allocate a pseudo-TTY connected to 170 the container's stdin; creating an interactive `bash` shell in the container. 171 In the example, the `bash` shell is quit by entering 172 `exit 13`. This exit code is passed on to the caller of 173 `docker run`, and is recorded in the `test` container's metadata. 174 175 ### Capture container ID (--cidfile) 176 177 $ docker run --cidfile /tmp/docker_test.cid ubuntu echo "test" 178 179 This will create a container and print `test` to the console. The `cidfile` 180 flag makes Docker attempt to create a new file and write the container ID to it. 181 If the file exists already, Docker will return an error. Docker will close this 182 file when `docker run` exits. 183 184 ### Full container capabilities (--privileged) 185 186 $ docker run -t -i --rm ubuntu bash 187 root@bc338942ef20:/# mount -t tmpfs none /mnt 188 mount: permission denied 189 190 This will *not* work, because by default, most potentially dangerous kernel 191 capabilities are dropped; including `cap_sys_admin` (which is required to mount 192 filesystems). However, the `--privileged` flag will allow it to run: 193 194 $ docker run -t -i --privileged ubuntu bash 195 root@50e3f57e16e6:/# mount -t tmpfs none /mnt 196 root@50e3f57e16e6:/# df -h 197 Filesystem Size Used Avail Use% Mounted on 198 none 1.9G 0 1.9G 0% /mnt 199 200 The `--privileged` flag gives *all* capabilities to the container, and it also 201 lifts all the limitations enforced by the `device` cgroup controller. In other 202 words, the container can then do almost everything that the host can do. This 203 flag exists to allow special use-cases, like running Docker within Docker. 204 205 ### Set working directory (-w) 206 207 $ docker run -w /path/to/dir/ -i -t ubuntu pwd 208 209 The `-w` lets the command being executed inside directory given, here 210 `/path/to/dir/`. If the path does not exist it is created inside the container. 211 212 ### Set storage driver options per container 213 214 $ docker run -it --storage-opt size=120G fedora /bin/bash 215 216 This (size) will allow to set the container rootfs size to 120G at creation time. 217 This option is only available for the `devicemapper`, `btrfs`, `overlay2`, 218 `windowsfilter` and `zfs` graph drivers. 219 For the `devicemapper`, `btrfs`, `windowsfilter` and `zfs` graph drivers, 220 user cannot pass a size less than the Default BaseFS Size. 221 For the `overlay2` storage driver, the size option is only available if the 222 backing fs is `xfs` and mounted with the `pquota` mount option. 223 Under these conditions, user can pass any size less then the backing fs size. 224 225 ### Mount tmpfs (--tmpfs) 226 227 $ docker run -d --tmpfs /run:rw,noexec,nosuid,size=65536k my_image 228 229 The `--tmpfs` flag mounts an empty tmpfs into the container with the `rw`, 230 `noexec`, `nosuid`, `size=65536k` options. 231 232 ### Mount volume (-v, --read-only) 233 234 $ docker run -v `pwd`:`pwd` -w `pwd` -i -t ubuntu pwd 235 236 The `-v` flag mounts the current working directory into the container. The `-w` 237 lets the command being executed inside the current working directory, by 238 changing into the directory to the value returned by `pwd`. So this 239 combination executes the command using the container, but inside the 240 current working directory. 241 242 $ docker run -v /doesnt/exist:/foo -w /foo -i -t ubuntu bash 243 244 When the host directory of a bind-mounted volume doesn't exist, Docker 245 will automatically create this directory on the host for you. In the 246 example above, Docker will create the `/doesnt/exist` 247 folder before starting your container. 248 249 $ docker run --read-only -v /icanwrite busybox touch /icanwrite/here 250 251 Volumes can be used in combination with `--read-only` to control where 252 a container writes files. The `--read-only` flag mounts the container's root 253 filesystem as read only prohibiting writes to locations other than the 254 specified volumes for the container. 255 256 $ docker run -t -i -v /var/run/docker.sock:/var/run/docker.sock -v /path/to/static-docker-binary:/usr/bin/docker busybox sh 257 258 By bind-mounting the docker unix socket and statically linked docker 259 binary (refer to [get the linux binary]( 260 https://docs.docker.com/engine/installation/binaries/#/get-the-linux-binary)), 261 you give the container the full access to create and manipulate the host's 262 Docker daemon. 263 264 On Windows, the paths must be specified using Windows-style semantics. 265 266 PS C:\> docker run -v c:\foo:c:\dest microsoft/nanoserver cmd /s /c type c:\dest\somefile.txt 267 Contents of file 268 269 PS C:\> docker run -v c:\foo:d: microsoft/nanoserver cmd /s /c type d:\somefile.txt 270 Contents of file 271 272 The following examples will fail when using Windows-based containers, as the 273 destination of a volume or bind-mount inside the container must be one of: 274 a non-existing or empty directory; or a drive other than C:. Further, the source 275 of a bind mount must be a local directory, not a file. 276 277 net use z: \\remotemachine\share 278 docker run -v z:\foo:c:\dest ... 279 docker run -v \\uncpath\to\directory:c:\dest ... 280 docker run -v c:\foo\somefile.txt:c:\dest ... 281 docker run -v c:\foo:c: ... 282 docker run -v c:\foo:c:\existing-directory-with-contents ... 283 284 For in-depth information about volumes, refer to [manage data in containers](https://docs.docker.com/engine/tutorials/dockervolumes/) 285 286 ### Publish or expose port (-p, --expose) 287 288 $ docker run -p 127.0.0.1:80:8080 ubuntu bash 289 290 This binds port `8080` of the container to port `80` on `127.0.0.1` of the host 291 machine. The [Docker User 292 Guide](https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/) 293 explains in detail how to manipulate ports in Docker. 294 295 $ docker run --expose 80 ubuntu bash 296 297 This exposes port `80` of the container without publishing the port to the host 298 system's interfaces. 299 300 ### Set environment variables (-e, --env, --env-file) 301 302 $ docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash 303 304 This sets simple (non-array) environmental variables in the container. For 305 illustration all three 306 flags are shown here. Where `-e`, `--env` take an environment variable and 307 value, or if no `=` is provided, then that variable's current value, set via 308 `export`, is passed through (i.e. `$MYVAR1` from the host is set to `$MYVAR1` 309 in the container). When no `=` is provided and that variable is not defined 310 in the client's environment then that variable will be removed from the 311 container's list of environment variables. All three flags, `-e`, `--env` and 312 `--env-file` can be repeated. 313 314 Regardless of the order of these three flags, the `--env-file` are processed 315 first, and then `-e`, `--env` flags. This way, the `-e` or `--env` will 316 override variables as needed. 317 318 $ cat ./env.list 319 TEST_FOO=BAR 320 $ docker run --env TEST_FOO="This is a test" --env-file ./env.list busybox env | grep TEST_FOO 321 TEST_FOO=This is a test 322 323 The `--env-file` flag takes a filename as an argument and expects each line 324 to be in the `VAR=VAL` format, mimicking the argument passed to `--env`. Comment 325 lines need only be prefixed with `#` 326 327 An example of a file passed with `--env-file` 328 329 $ cat ./env.list 330 TEST_FOO=BAR 331 332 # this is a comment 333 TEST_APP_DEST_HOST=10.10.0.127 334 TEST_APP_DEST_PORT=8888 335 _TEST_BAR=FOO 336 TEST_APP_42=magic 337 helloWorld=true 338 123qwe=bar 339 org.spring.config=something 340 341 # pass through this variable from the caller 342 TEST_PASSTHROUGH 343 $ TEST_PASSTHROUGH=howdy docker run --env-file ./env.list busybox env 344 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 345 HOSTNAME=5198e0745561 346 TEST_FOO=BAR 347 TEST_APP_DEST_HOST=10.10.0.127 348 TEST_APP_DEST_PORT=8888 349 _TEST_BAR=FOO 350 TEST_APP_42=magic 351 helloWorld=true 352 TEST_PASSTHROUGH=howdy 353 HOME=/root 354 123qwe=bar 355 org.spring.config=something 356 357 $ docker run --env-file ./env.list busybox env 358 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 359 HOSTNAME=5198e0745561 360 TEST_FOO=BAR 361 TEST_APP_DEST_HOST=10.10.0.127 362 TEST_APP_DEST_PORT=8888 363 _TEST_BAR=FOO 364 TEST_APP_42=magic 365 helloWorld=true 366 TEST_PASSTHROUGH= 367 HOME=/root 368 123qwe=bar 369 org.spring.config=something 370 371 ### Set metadata on container (-l, --label, --label-file) 372 373 A label is a `key=value` pair that applies metadata to a container. To label a container with two labels: 374 375 $ docker run -l my-label --label com.example.foo=bar ubuntu bash 376 377 The `my-label` key doesn't specify a value so the label defaults to an empty 378 string(`""`). To add multiple labels, repeat the label flag (`-l` or `--label`). 379 380 The `key=value` must be unique to avoid overwriting the label value. If you 381 specify labels with identical keys but different values, each subsequent value 382 overwrites the previous. Docker uses the last `key=value` you supply. 383 384 Use the `--label-file` flag to load multiple labels from a file. Delimit each 385 label in the file with an EOL mark. The example below loads labels from a 386 labels file in the current directory: 387 388 $ docker run --label-file ./labels ubuntu bash 389 390 The label-file format is similar to the format for loading environment 391 variables. (Unlike environment variables, labels are not visible to processes 392 running inside a container.) The following example illustrates a label-file 393 format: 394 395 com.example.label1="a label" 396 397 # this is a comment 398 com.example.label2=another\ label 399 com.example.label3 400 401 You can load multiple label-files by supplying multiple `--label-file` flags. 402 403 For additional information on working with labels, see [*Labels - custom 404 metadata in Docker*](https://docs.docker.com/engine/userguide/labels-custom-metadata/) in the Docker User 405 Guide. 406 407 ### Connect a container to a network (--network) 408 409 When you start a container use the `--network` flag to connect it to a network. 410 This adds the `busybox` container to the `my-net` network. 411 412 ```bash 413 $ docker run -itd --network=my-net busybox 414 ``` 415 416 You can also choose the IP addresses for the container with `--ip` and `--ip6` 417 flags when you start the container on a user-defined network. 418 419 ```bash 420 $ docker run -itd --network=my-net --ip=10.10.9.75 busybox 421 ``` 422 423 If you want to add a running container to a network use the `docker network connect` subcommand. 424 425 You can connect multiple containers to the same network. Once connected, the 426 containers can communicate easily need only another container's IP address 427 or name. For `overlay` networks or custom plugins that support multi-host 428 connectivity, containers connected to the same multi-host network but launched 429 from different Engines can also communicate in this way. 430 431 **Note**: Service discovery is unavailable on the default bridge network. 432 Containers can communicate via their IP addresses by default. To communicate 433 by name, they must be linked. 434 435 You can disconnect a container from a network using the `docker network 436 disconnect` command. 437 438 ### Mount volumes from container (--volumes-from) 439 440 $ docker run --volumes-from 777f7dc92da7 --volumes-from ba8c0c54f0f2:ro -i -t ubuntu pwd 441 442 The `--volumes-from` flag mounts all the defined volumes from the referenced 443 containers. Containers can be specified by repetitions of the `--volumes-from` 444 argument. The container ID may be optionally suffixed with `:ro` or `:rw` to 445 mount the volumes in read-only or read-write mode, respectively. By default, 446 the volumes are mounted in the same mode (read write or read only) as 447 the reference container. 448 449 Labeling systems like SELinux require that proper labels are placed on volume 450 content mounted into a container. Without a label, the security system might 451 prevent the processes running inside the container from using the content. By 452 default, Docker does not change the labels set by the OS. 453 454 To change the label in the container context, you can add either of two suffixes 455 `:z` or `:Z` to the volume mount. These suffixes tell Docker to relabel file 456 objects on the shared volumes. The `z` option tells Docker that two containers 457 share the volume content. As a result, Docker labels the content with a shared 458 content label. Shared volume labels allow all containers to read/write content. 459 The `Z` option tells Docker to label the content with a private unshared label. 460 Only the current container can use a private volume. 461 462 ### Attach to STDIN/STDOUT/STDERR (-a) 463 464 The `-a` flag tells `docker run` to bind to the container's `STDIN`, `STDOUT` 465 or `STDERR`. This makes it possible to manipulate the output and input as 466 needed. 467 468 $ echo "test" | docker run -i -a stdin ubuntu cat - 469 470 This pipes data into a container and prints the container's ID by attaching 471 only to the container's `STDIN`. 472 473 $ docker run -a stderr ubuntu echo test 474 475 This isn't going to print anything unless there's an error because we've 476 only attached to the `STDERR` of the container. The container's logs 477 still store what's been written to `STDERR` and `STDOUT`. 478 479 $ cat somefile | docker run -i -a stdin mybuilder dobuild 480 481 This is how piping a file into a container could be done for a build. 482 The container's ID will be printed after the build is done and the build 483 logs could be retrieved using `docker logs`. This is 484 useful if you need to pipe a file or something else into a container and 485 retrieve the container's ID once the container has finished running. 486 487 ### Add host device to container (--device) 488 489 $ docker run --device=/dev/sdc:/dev/xvdc --device=/dev/sdd --device=/dev/zero:/dev/nulo -i -t ubuntu ls -l /dev/{xvdc,sdd,nulo} 490 brw-rw---- 1 root disk 8, 2 Feb 9 16:05 /dev/xvdc 491 brw-rw---- 1 root disk 8, 3 Feb 9 16:05 /dev/sdd 492 crw-rw-rw- 1 root root 1, 5 Feb 9 16:05 /dev/nulo 493 494 It is often necessary to directly expose devices to a container. The `--device` 495 option enables that. For example, a specific block storage device or loop 496 device or audio device can be added to an otherwise unprivileged container 497 (without the `--privileged` flag) and have the application directly access it. 498 499 By default, the container will be able to `read`, `write` and `mknod` these devices. 500 This can be overridden using a third `:rwm` set of options to each `--device` 501 flag: 502 503 504 $ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc 505 506 Command (m for help): q 507 $ docker run --device=/dev/sda:/dev/xvdc:r --rm -it ubuntu fdisk /dev/xvdc 508 You will not be able to write the partition table. 509 510 Command (m for help): q 511 512 $ docker run --device=/dev/sda:/dev/xvdc:rw --rm -it ubuntu fdisk /dev/xvdc 513 514 Command (m for help): q 515 516 $ docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc 517 fdisk: unable to open /dev/xvdc: Operation not permitted 518 519 > **Note:** 520 > `--device` cannot be safely used with ephemeral devices. Block devices 521 > that may be removed should not be added to untrusted containers with 522 > `--device`. 523 524 ### Restart policies (--restart) 525 526 Use Docker's `--restart` to specify a container's *restart policy*. A restart 527 policy controls whether the Docker daemon restarts a container after exit. 528 Docker supports the following restart policies: 529 530 <table> 531 <thead> 532 <tr> 533 <th>Policy</th> 534 <th>Result</th> 535 </tr> 536 </thead> 537 <tbody> 538 <tr> 539 <td><strong>no</strong></td> 540 <td> 541 Do not automatically restart the container when it exits. This is the 542 default. 543 </td> 544 </tr> 545 <tr> 546 <td> 547 <span style="white-space: nowrap"> 548 <strong>on-failure</strong>[:max-retries] 549 </span> 550 </td> 551 <td> 552 Restart only if the container exits with a non-zero exit status. 553 Optionally, limit the number of restart retries the Docker 554 daemon attempts. 555 </td> 556 </tr> 557 <tr> 558 <td><strong>always</strong></td> 559 <td> 560 Always restart the container regardless of the exit status. 561 When you specify always, the Docker daemon will try to restart 562 the container indefinitely. The container will also always start 563 on daemon startup, regardless of the current state of the container. 564 </td> 565 </tr> 566 <tr> 567 <td><strong>unless-stopped</strong></td> 568 <td> 569 Always restart the container regardless of the exit status, but 570 do not start it on daemon startup if the container has been put 571 to a stopped state before. 572 </td> 573 </tr> 574 </tbody> 575 </table> 576 577 $ docker run --restart=always redis 578 579 This will run the `redis` container with a restart policy of **always** 580 so that if the container exits, Docker will restart it. 581 582 More detailed information on restart policies can be found in the 583 [Restart Policies (--restart)](../run.md#restart-policies-restart) 584 section of the Docker run reference page. 585 586 ### Add entries to container hosts file (--add-host) 587 588 You can add other hosts into a container's `/etc/hosts` file by using one or 589 more `--add-host` flags. This example adds a static address for a host named 590 `docker`: 591 592 $ docker run --add-host=docker:10.180.0.1 --rm -it debian 593 root@f38c87f2a42d:/# ping docker 594 PING docker (10.180.0.1): 48 data bytes 595 56 bytes from 10.180.0.1: icmp_seq=0 ttl=254 time=7.600 ms 596 56 bytes from 10.180.0.1: icmp_seq=1 ttl=254 time=30.705 ms 597 ^C--- docker ping statistics --- 598 2 packets transmitted, 2 packets received, 0% packet loss 599 round-trip min/avg/max/stddev = 7.600/19.152/30.705/11.553 ms 600 601 Sometimes you need to connect to the Docker host from within your 602 container. To enable this, pass the Docker host's IP address to 603 the container using the `--add-host` flag. To find the host's address, 604 use the `ip addr show` command. 605 606 The flags you pass to `ip addr show` depend on whether you are 607 using IPv4 or IPv6 networking in your containers. Use the following 608 flags for IPv4 address retrieval for a network device named `eth0`: 609 610 $ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1` 611 $ docker run --add-host=docker:${HOSTIP} --rm -it debian 612 613 For IPv6 use the `-6` flag instead of the `-4` flag. For other network 614 devices, replace `eth0` with the correct device name (for example `docker0` 615 for the bridge device). 616 617 ### Set ulimits in container (--ulimit) 618 619 Since setting `ulimit` settings in a container requires extra privileges not 620 available in the default container, you can set these using the `--ulimit` flag. 621 `--ulimit` is specified with a soft and hard limit as such: 622 `<type>=<soft limit>[:<hard limit>]`, for example: 623 624 $ docker run --ulimit nofile=1024:1024 --rm debian sh -c "ulimit -n" 625 1024 626 627 > **Note:** 628 > If you do not provide a `hard limit`, the `soft limit` will be used 629 > for both values. If no `ulimits` are set, they will be inherited from 630 > the default `ulimits` set on the daemon. `as` option is disabled now. 631 > In other words, the following script is not supported: 632 > `$ docker run -it --ulimit as=1024 fedora /bin/bash` 633 634 The values are sent to the appropriate `syscall` as they are set. 635 Docker doesn't perform any byte conversion. Take this into account when setting the values. 636 637 #### For `nproc` usage 638 639 Be careful setting `nproc` with the `ulimit` flag as `nproc` is designed by Linux to set the 640 maximum number of processes available to a user, not to a container. For example, start four 641 containers with `daemon` user: 642 643 docker run -d -u daemon --ulimit nproc=3 busybox top 644 docker run -d -u daemon --ulimit nproc=3 busybox top 645 docker run -d -u daemon --ulimit nproc=3 busybox top 646 docker run -d -u daemon --ulimit nproc=3 busybox top 647 648 The 4th container fails and reports "[8] System error: resource temporarily unavailable" error. 649 This fails because the caller set `nproc=3` resulting in the first three containers using up 650 the three processes quota set for the `daemon` user. 651 652 ### Stop container with signal (--stop-signal) 653 654 The `--stop-signal` flag sets the system call signal that will be sent to the container to exit. 655 This signal can be a valid unsigned number that matches a position in the kernel's syscall table, for instance 9, 656 or a signal name in the format SIGNAME, for instance SIGKILL. 657 658 ### Optional security options (--security-opt) 659 660 On Windows, this flag can be used to specify the `credentialspec` option. 661 The `credentialspec` must be in the format `file://spec.txt` or `registry://keyname`. 662 663 ### Stop container with timeout (--stop-timeout) 664 665 The `--stop-timeout` flag sets the timeout (in seconds) that a pre-defined (see `--stop-signal`) system call 666 signal that will be sent to the container to exit. After timeout elapses the container will be killed with SIGKILL. 667 668 ### Specify isolation technology for container (--isolation) 669 670 This option is useful in situations where you are running Docker containers on 671 Windows. The `--isolation <value>` option sets a container's isolation technology. 672 On Linux, the only supported is the `default` option which uses 673 Linux namespaces. These two commands are equivalent on Linux: 674 675 ```bash 676 $ docker run -d busybox top 677 $ docker run -d --isolation default busybox top 678 ``` 679 680 On Windows, `--isolation` can take one of these values: 681 682 683 | Value | Description | 684 |-----------|--------------------------------------------------------------------------------------------| 685 | `default` | Use the value specified by the Docker daemon's `--exec-opt` or system default (see below). | 686 | `process` | Shared-kernel namespace isolation (not supported on Windows client operating systems). | 687 | `hyperv` | Hyper-V hypervisor partition-based isolation. | 688 689 The default isolation on Windows server operating systems is `process`. The default (and only supported) 690 isolation on Windows client operating systems is `hyperv`. An attempt to start a container on a client 691 operating system with `--isolation process` will fail. 692 693 On Windows server, assuming the default configuration, these commands are equivalent 694 and result in `process` isolation: 695 696 ```PowerShell 697 PS C:\> docker run -d microsoft/nanoserver powershell echo process 698 PS C:\> docker run -d --isolation default microsoft/nanoserver powershell echo process 699 PS C:\> docker run -d --isolation process microsoft/nanoserver powershell echo process 700 ``` 701 702 If you have set the `--exec-opt isolation=hyperv` option on the Docker `daemon`, or 703 are running against a Windows client-based daemon, these commands are equivalent and 704 result in `hyperv` isolation: 705 706 ```PowerShell 707 PS C:\> docker run -d microsoft/nanoserver powershell echo hyperv 708 PS C:\> docker run -d --isolation default microsoft/nanoserver powershell echo hyperv 709 PS C:\> docker run -d --isolation hyperv microsoft/nanoserver powershell echo hyperv 710 ``` 711 712 ### Configure namespaced kernel parameters (sysctls) at runtime 713 714 The `--sysctl` sets namespaced kernel parameters (sysctls) in the 715 container. For example, to turn on IP forwarding in the containers 716 network namespace, run this command: 717 718 $ docker run --sysctl net.ipv4.ip_forward=1 someimage 719 720 721 > **Note**: Not all sysctls are namespaced. Docker does not support changing sysctls 722 > inside of a container that also modify the host system. As the kernel 723 > evolves we expect to see more sysctls become namespaced. 724 725 #### Currently supported sysctls 726 727 `IPC Namespace`: 728 729 kernel.msgmax, kernel.msgmnb, kernel.msgmni, kernel.sem, kernel.shmall, kernel.shmmax, kernel.shmmni, kernel.shm_rmid_forced 730 Sysctls beginning with fs.mqueue.* 731 732 If you use the `--ipc=host` option these sysctls will not be allowed. 733 734 `Network Namespace`: 735 Sysctls beginning with net.* 736 737 If you use the `--network=host` option using these sysctls will not be allowed.