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