github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/docs/reference/run.md (about) 1 <!--[metadata]> 2 +++ 3 title = "Docker run reference" 4 description = "Configure containers at runtime" 5 keywords = ["docker, run, configure, runtime"] 6 [menu.main] 7 parent = "mn_reference" 8 +++ 9 <![end-metadata]--> 10 11 <!-- TODO (@thaJeztah) define more flexible table/td classes --> 12 <style> 13 table .no-wrap { 14 white-space: nowrap; 15 } 16 table code { 17 white-space: nowrap; 18 } 19 </style> 20 # Docker run reference 21 22 Docker runs processes in isolated containers. A container is a process 23 which runs on a host. The host may be local or remote. When an operator 24 executes `docker run`, the container process that runs is isolated in 25 that it has its own file system, its own networking, and its own 26 isolated process tree separate from the host. 27 28 This page details how to use the `docker run` command to define the 29 container's resources at runtime. 30 31 ## General form 32 33 The basic `docker run` command takes this form: 34 35 $ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] 36 37 The `docker run` command must specify an [*IMAGE*](glossary.md#image) 38 to derive the container from. An image developer can define image 39 defaults related to: 40 41 * detached or foreground running 42 * container identification 43 * network settings 44 * runtime constraints on CPU and memory 45 46 With the `docker run [OPTIONS]` an operator can add to or override the 47 image defaults set by a developer. And, additionally, operators can 48 override nearly all the defaults set by the Docker runtime itself. The 49 operator's ability to override image and Docker runtime defaults is why 50 [*run*](commandline/run.md) has more options than any 51 other `docker` command. 52 53 To learn how to interpret the types of `[OPTIONS]`, see [*Option 54 types*](commandline/cli.md#option-types). 55 56 > **Note**: Depending on your Docker system configuration, you may be 57 > required to preface the `docker run` command with `sudo`. To avoid 58 > having to use `sudo` with the `docker` command, your system 59 > administrator can create a Unix group called `docker` and add users to 60 > it. For more information about this configuration, refer to the Docker 61 > installation documentation for your operating system. 62 63 64 ## Operator exclusive options 65 66 Only the operator (the person executing `docker run`) can set the 67 following options. 68 69 - [Detached vs foreground](#detached-vs-foreground) 70 - [Detached (-d)](#detached-d) 71 - [Foreground](#foreground) 72 - [Container identification](#container-identification) 73 - [Name (--name)](#name-name) 74 - [PID equivalent](#pid-equivalent) 75 - [IPC settings (--ipc)](#ipc-settings-ipc) 76 - [Network settings](#network-settings) 77 - [Restart policies (--restart)](#restart-policies-restart) 78 - [Clean up (--rm)](#clean-up-rm) 79 - [Runtime constraints on resources](#runtime-constraints-on-resources) 80 - [Runtime privilege and Linux capabilities](#runtime-privilege-and-linux-capabilities) 81 82 ## Detached vs foreground 83 84 When starting a Docker container, you must first decide if you want to 85 run the container in the background in a "detached" mode or in the 86 default foreground mode: 87 88 -d=false: Detached mode: Run container in the background, print new container id 89 90 ### Detached (-d) 91 92 To start a container in detached mode, you use `-d=true` or just `-d` option. By 93 design, containers started in detached mode exit when the root process used to 94 run the container exits. A container in detached mode cannot be automatically 95 removed when it stops, this means you cannot use the `--rm` option with `-d` option. 96 97 Do not pass a `service x start` command to a detached container. For example, this 98 command attempts to start the `nginx` service. 99 100 $ docker run -d -p 80:80 my_image service nginx start 101 102 This succeeds in starting the `nginx` service inside the container. However, it 103 fails the detached container paradigm in that, the root process (`service nginx 104 start`) returns and the detached container stops as designed. As a result, the 105 `nginx` service is started but could not be used. Instead, to start a process 106 such as the `nginx` web server do the following: 107 108 $ docker run -d -p 80:80 my_image nginx -g 'daemon off;' 109 110 To do input/output with a detached container use network connections or shared 111 volumes. These are required because the container is no longer listening to the 112 command line where `docker run` was run. 113 114 To reattach to a detached container, use `docker` 115 [*attach*](commandline/attach.md) command. 116 117 ### Foreground 118 119 In foreground mode (the default when `-d` is not specified), `docker 120 run` can start the process in the container and attach the console to 121 the process's standard input, output, and standard error. It can even 122 pretend to be a TTY (this is what most command line executables expect) 123 and pass along signals. All of that is configurable: 124 125 -a=[] : Attach to `STDIN`, `STDOUT` and/or `STDERR` 126 -t=false : Allocate a pseudo-tty 127 --sig-proxy=true: Proxy all received signals to the process (non-TTY mode only) 128 -i=false : Keep STDIN open even if not attached 129 130 If you do not specify `-a` then Docker will [attach all standard 131 streams]( https://github.com/docker/docker/blob/75a7f4d90cde0295bcfb7213004abce8d4779b75/commands.go#L1797). 132 You can specify to which of the three standard streams (`STDIN`, `STDOUT`, 133 `STDERR`) you'd like to connect instead, as in: 134 135 $ docker run -a stdin -a stdout -i -t ubuntu /bin/bash 136 137 For interactive processes (like a shell), you must use `-i -t` together in 138 order to allocate a tty for the container process. `-i -t` is often written `-it` 139 as you'll see in later examples. Specifying `-t` is forbidden when the client 140 standard output is redirected or piped, such as in: 141 142 $ echo test | docker run -i busybox cat 143 144 >**Note**: A process running as PID 1 inside a container is treated 145 >specially by Linux: it ignores any signal with the default action. 146 >So, the process will not terminate on `SIGINT` or `SIGTERM` unless it is 147 >coded to do so. 148 149 ## Container identification 150 151 ### Name (--name) 152 153 The operator can identify a container in three ways: 154 155 | Identifier type | Example value | 156 | --------------------- | ------------------------------------------------------------------ | 157 | UUID long identifier | "f78375b1c487e03c9438c729345e54db9d20cfa2ac1fc3494b6eb60872e74778" | 158 | UUID short identifier | "f78375b1c487" | 159 | Name | "evil_ptolemy" | 160 161 The UUID identifiers come from the Docker daemon. If you do not assign a 162 container name with the `--name` option, then the daemon generates a random 163 string name for you. Defining a `name` can be a handy way to add meaning to a 164 container. If you specify a `name`, you can use it when referencing the 165 container within a Docker network. This works for both background and foreground 166 Docker containers. 167 168 > **Note**: Containers on the default bridge network must be linked to 169 > communicate by name. 170 171 ### PID equivalent 172 173 Finally, to help with automation, you can have Docker write the 174 container ID out to a file of your choosing. This is similar to how some 175 programs might write out their process ID to a file (you've seen them as 176 PID files): 177 178 --cidfile="": Write the container ID to the file 179 180 ### Image[:tag] 181 182 While not strictly a means of identifying a container, you can specify a version of an 183 image you'd like to run the container with by adding `image[:tag]` to the command. For 184 example, `docker run ubuntu:14.04`. 185 186 ### Image[@digest] 187 188 Images using the v2 or later image format have a content-addressable identifier 189 called a digest. As long as the input used to generate the image is unchanged, 190 the digest value is predictable and referenceable. 191 192 ## PID settings (--pid) 193 194 --pid="" : Set the PID (Process) Namespace mode for the container, 195 'host': use the host's PID namespace inside the container 196 197 By default, all containers have the PID namespace enabled. 198 199 PID namespace provides separation of processes. The PID Namespace removes the 200 view of the system processes, and allows process ids to be reused including 201 pid 1. 202 203 In certain cases you want your container to share the host's process namespace, 204 basically allowing processes within the container to see all of the processes 205 on the system. For example, you could build a container with debugging tools 206 like `strace` or `gdb`, but want to use these tools when debugging processes 207 within the container. 208 209 $ docker run --pid=host rhel7 strace -p 1234 210 211 This command would allow you to use `strace` inside the container on pid 1234 on 212 the host. 213 214 ## UTS settings (--uts) 215 216 --uts="" : Set the UTS namespace mode for the container, 217 'host': use the host's UTS namespace inside the container 218 219 The UTS namespace is for setting the hostname and the domain that is visible 220 to running processes in that namespace. By default, all containers, including 221 those with `--net=host`, have their own UTS namespace. The `host` setting will 222 result in the container using the same UTS namespace as the host. 223 224 You may wish to share the UTS namespace with the host if you would like the 225 hostname of the container to change as the hostname of the host changes. A 226 more advanced use case would be changing the host's hostname from a container. 227 228 > **Note**: `--uts="host"` gives the container full access to change the 229 > hostname of the host and is therefore considered insecure. 230 231 ## IPC settings (--ipc) 232 233 --ipc="" : Set the IPC mode for the container, 234 'container:<name|id>': reuses another container's IPC namespace 235 'host': use the host's IPC namespace inside the container 236 237 By default, all containers have the IPC namespace enabled. 238 239 IPC (POSIX/SysV IPC) namespace provides separation of named shared memory 240 segments, semaphores and message queues. 241 242 Shared memory segments are used to accelerate inter-process communication at 243 memory speed, rather than through pipes or through the network stack. Shared 244 memory is commonly used by databases and custom-built (typically C/OpenMPI, 245 C++/using boost libraries) high performance applications for scientific 246 computing and financial services industries. If these types of applications 247 are broken into multiple containers, you might need to share the IPC mechanisms 248 of the containers. 249 250 ## Network settings 251 252 --dns=[] : Set custom dns servers for the container 253 --net="bridge" : Connect a container to a network 254 'bridge': create a network stack on the default Docker bridge 255 'none': no networking 256 'container:<name|id>': reuse another container's network stack 257 'host': use the Docker host network stack 258 '<network-name>|<network-id>': connect to a user-defined network 259 --add-host="" : Add a line to /etc/hosts (host:IP) 260 --mac-address="" : Sets the container's Ethernet device's MAC address 261 262 By default, all containers have networking enabled and they can make any 263 outgoing connections. The operator can completely disable networking 264 with `docker run --net none` which disables all incoming and outgoing 265 networking. In cases like this, you would perform I/O through files or 266 `STDIN` and `STDOUT` only. 267 268 Publishing ports and linking to other containers only works with the the default (bridge). The linking feature is a legacy feature. You should always prefer using Docker network drivers over linking. 269 270 Your container will use the same DNS servers as the host by default, but 271 you can override this with `--dns`. 272 273 By default, the MAC address is generated using the IP address allocated to the 274 container. You can set the container's MAC address explicitly by providing a 275 MAC address via the `--mac-address` parameter (format:`12:34:56:78:9a:bc`). 276 277 Supported networks : 278 279 <table> 280 <thead> 281 <tr> 282 <th class="no-wrap">Network</th> 283 <th>Description</th> 284 </tr> 285 </thead> 286 <tbody> 287 <tr> 288 <td class="no-wrap"><strong>none</strong></td> 289 <td> 290 No networking in the container. 291 </td> 292 </tr> 293 <tr> 294 <td class="no-wrap"><strong>bridge</strong> (default)</td> 295 <td> 296 Connect the container to the bridge via veth interfaces. 297 </td> 298 </tr> 299 <tr> 300 <td class="no-wrap"><strong>host</strong></td> 301 <td> 302 Use the host's network stack inside the container. 303 </td> 304 </tr> 305 <tr> 306 <td class="no-wrap"><strong>container</strong>:<name|id></td> 307 <td> 308 Use the network stack of another container, specified via 309 its *name* or *id*. 310 </td> 311 </tr> 312 <tr> 313 <td class="no-wrap"><strong>NETWORK</strong></td> 314 <td> 315 Connects the container to a user created network (using `docker network create` command) 316 </td> 317 </tr> 318 </tbody> 319 </table> 320 321 #### Network: none 322 323 With the network is `none` a container will not have 324 access to any external routes. The container will still have a 325 `loopback` interface enabled in the container but it does not have any 326 routes to external traffic. 327 328 #### Network: bridge 329 330 With the network set to `bridge` a container will use docker's 331 default networking setup. A bridge is setup on the host, commonly named 332 `docker0`, and a pair of `veth` interfaces will be created for the 333 container. One side of the `veth` pair will remain on the host attached 334 to the bridge while the other side of the pair will be placed inside the 335 container's namespaces in addition to the `loopback` interface. An IP 336 address will be allocated for containers on the bridge's network and 337 traffic will be routed though this bridge to the container. 338 339 Containers can communicate via their IP addresses by default. To communicate by 340 name, they must be linked. 341 342 #### Network: host 343 344 With the network set to `host` a container will share the host's 345 network stack and all interfaces from the host will be available to the 346 container. The container's hostname will match the hostname on the host 347 system. Note that `--add-host` `--hostname` `--dns` `--dns-search` 348 `--dns-opt` and `--mac-address` are invalid in `host` netmode. 349 350 Compared to the default `bridge` mode, the `host` mode gives *significantly* 351 better networking performance since it uses the host's native networking stack 352 whereas the bridge has to go through one level of virtualization through the 353 docker daemon. It is recommended to run containers in this mode when their 354 networking performance is critical, for example, a production Load Balancer 355 or a High Performance Web Server. 356 357 > **Note**: `--net="host"` gives the container full access to local system 358 > services such as D-bus and is therefore considered insecure. 359 360 #### Network: container 361 362 With the network set to `container` a container will share the 363 network stack of another container. The other container's name must be 364 provided in the format of `--net container:<name|id>`. Note that `--add-host` 365 `--hostname` `--dns` `--dns-search` `--dns-opt` and `--mac-address` are 366 invalid in `container` netmode, and `--publish` `--publish-all` `--expose` are 367 also invalid in `container` netmode. 368 369 Example running a Redis container with Redis binding to `localhost` then 370 running the `redis-cli` command and connecting to the Redis server over the 371 `localhost` interface. 372 373 $ docker run -d --name redis example/redis --bind 127.0.0.1 374 $ # use the redis container's network stack to access localhost 375 $ docker run --rm -it --net container:redis example/redis-cli -h 127.0.0.1 376 377 #### User-defined network 378 379 You can create a network using a Docker network driver or an external network 380 driver plugin. You can connect multiple containers to the same network. Once 381 connected to a user-defined network, the containers can communicate easily using 382 only another container's IP address or name. 383 384 For `overlay` networks or custom plugins that support multi-host connectivity, 385 containers connected to the same multi-host network but launched from different 386 Engines can also communicate in this way. 387 388 The following example creates a network using the built-in `bridge` network 389 driver and running a container in the created network 390 391 ``` 392 $ docker network create -d overlay my-net 393 $ docker run --net=my-net -itd --name=container3 busybox 394 ``` 395 396 ### Managing /etc/hosts 397 398 Your container will have lines in `/etc/hosts` which define the hostname of the 399 container itself as well as `localhost` and a few other common things. The 400 `--add-host` flag can be used to add additional lines to `/etc/hosts`. 401 402 $ docker run -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts 403 172.17.0.22 09d03f76bf2c 404 fe00::0 ip6-localnet 405 ff00::0 ip6-mcastprefix 406 ff02::1 ip6-allnodes 407 ff02::2 ip6-allrouters 408 127.0.0.1 localhost 409 ::1 localhost ip6-localhost ip6-loopback 410 86.75.30.9 db-static 411 412 If a container is connected to the default bridge network and `linked` 413 with other containers, then the container's `/etc/hosts` file is updated 414 with the linked container's name. 415 416 If the container is connected to user-defined network, the container's 417 `/etc/hosts` file is updated with names of all other containers in that 418 user-defined network. 419 420 > **Note** Since Docker may live update the container’s `/etc/hosts` file, there 421 may be situations when processes inside the container can end up reading an 422 empty or incomplete `/etc/hosts` file. In most cases, retrying the read again 423 should fix the problem. 424 425 ## Restart policies (--restart) 426 427 Using the `--restart` flag on Docker run you can specify a restart policy for 428 how a container should or should not be restarted on exit. 429 430 When a restart policy is active on a container, it will be shown as either `Up` 431 or `Restarting` in [`docker ps`](commandline/ps.md). It can also be 432 useful to use [`docker events`](commandline/events.md) to see the 433 restart policy in effect. 434 435 Docker supports the following restart policies: 436 437 <table> 438 <thead> 439 <tr> 440 <th>Policy</th> 441 <th>Result</th> 442 </tr> 443 </thead> 444 <tbody> 445 <tr> 446 <td><strong>no</strong></td> 447 <td> 448 Do not automatically restart the container when it exits. This is the 449 default. 450 </td> 451 </tr> 452 <tr> 453 <td> 454 <span style="white-space: nowrap"> 455 <strong>on-failure</strong>[:max-retries] 456 </span> 457 </td> 458 <td> 459 Restart only if the container exits with a non-zero exit status. 460 Optionally, limit the number of restart retries the Docker 461 daemon attempts. 462 </td> 463 </tr> 464 <tr> 465 <td><strong>always</strong></td> 466 <td> 467 Always restart the container regardless of the exit status. 468 When you specify always, the Docker daemon will try to restart 469 the container indefinitely. The container will also always start 470 on daemon startup, regardless of the current state of the container. 471 </td> 472 </tr> 473 <tr> 474 <td><strong>unless-stopped</strong></td> 475 <td> 476 Always restart the container regardless of the exit status, but 477 do not start it on daemon startup if the container has been put 478 to a stopped state before. 479 </td> 480 </tr> 481 </tbody> 482 </table> 483 484 An ever increasing delay (double the previous delay, starting at 100 485 milliseconds) is added before each restart to prevent flooding the server. 486 This means the daemon will wait for 100 ms, then 200 ms, 400, 800, 1600, 487 and so on until either the `on-failure` limit is hit, or when you `docker stop` 488 or `docker rm -f` the container. 489 490 If a container is successfully restarted (the container is started and runs 491 for at least 10 seconds), the delay is reset to its default value of 100 ms. 492 493 You can specify the maximum amount of times Docker will try to restart the 494 container when using the **on-failure** policy. The default is that Docker 495 will try forever to restart the container. The number of (attempted) restarts 496 for a container can be obtained via [`docker inspect`](commandline/inspect.md). For example, to get the number of restarts 497 for container "my-container"; 498 499 $ docker inspect -f "{{ .RestartCount }}" my-container 500 # 2 501 502 Or, to get the last time the container was (re)started; 503 504 $ docker inspect -f "{{ .State.StartedAt }}" my-container 505 # 2015-03-04T23:47:07.691840179Z 506 507 You cannot set any restart policy in combination with 508 ["clean up (--rm)"](#clean-up-rm). Setting both `--restart` and `--rm` 509 results in an error. 510 511 ### Examples 512 513 $ docker run --restart=always redis 514 515 This will run the `redis` container with a restart policy of **always** 516 so that if the container exits, Docker will restart it. 517 518 $ docker run --restart=on-failure:10 redis 519 520 This will run the `redis` container with a restart policy of **on-failure** 521 and a maximum restart count of 10. If the `redis` container exits with a 522 non-zero exit status more than 10 times in a row Docker will abort trying to 523 restart the container. Providing a maximum restart limit is only valid for the 524 **on-failure** policy. 525 526 ## Exit Status 527 528 The exit code from `docker run` gives information about why the container 529 failed to run or why it exited. When `docker run` exits with a non-zero code, 530 the exit codes follow the `chroot` standard, see below: 531 532 **_125_** if the error is with Docker daemon **_itself_** 533 534 $ docker run --foo busybox; echo $? 535 # flag provided but not defined: --foo 536 See 'docker run --help'. 537 125 538 539 **_126_** if the **_contained command_** cannot be invoked 540 541 $ docker run busybox /etc; echo $? 542 # exec: "/etc": permission denied 543 docker: Error response from daemon: Contained command could not be invoked 544 126 545 546 **_127_** if the **_contained command_** cannot be found 547 548 $ docker run busybox foo; echo $? 549 # exec: "foo": executable file not found in $PATH 550 docker: Error response from daemon: Contained command not found or does not exist 551 127 552 553 **_Exit code_** of **_contained command_** otherwise 554 555 $ docker run busybox /bin/sh -c 'exit 3' 556 # 3 557 558 ## Clean up (--rm) 559 560 By default a container's file system persists even after the container 561 exits. This makes debugging a lot easier (since you can inspect the 562 final state) and you retain all your data by default. But if you are 563 running short-term **foreground** processes, these container file 564 systems can really pile up. If instead you'd like Docker to 565 **automatically clean up the container and remove the file system when 566 the container exits**, you can add the `--rm` flag: 567 568 --rm=false: Automatically remove the container when it exits (incompatible with -d) 569 570 > **Note**: When you set the `--rm` flag, Docker also removes the volumes 571 associated with the container when the container is removed. This is similar 572 to running `docker rm -v my-container`. 573 574 ## Security configuration 575 --security-opt="label:user:USER" : Set the label user for the container 576 --security-opt="label:role:ROLE" : Set the label role for the container 577 --security-opt="label:type:TYPE" : Set the label type for the container 578 --security-opt="label:level:LEVEL" : Set the label level for the container 579 --security-opt="label:disable" : Turn off label confinement for the container 580 --security-opt="apparmor:PROFILE" : Set the apparmor profile to be applied 581 to the container 582 583 You can override the default labeling scheme for each container by specifying 584 the `--security-opt` flag. For example, you can specify the MCS/MLS level, a 585 requirement for MLS systems. Specifying the level in the following command 586 allows you to share the same content between containers. 587 588 $ docker run --security-opt label:level:s0:c100,c200 -it fedora bash 589 590 An MLS example might be: 591 592 $ docker run --security-opt label:level:TopSecret -it rhel7 bash 593 594 To disable the security labeling for this container versus running with the 595 `--permissive` flag, use the following command: 596 597 $ docker run --security-opt label:disable -it fedora bash 598 599 If you want a tighter security policy on the processes within a container, 600 you can specify an alternate type for the container. You could run a container 601 that is only allowed to listen on Apache ports by executing the following 602 command: 603 604 $ docker run --security-opt label:type:svirt_apache_t -it centos bash 605 606 > **Note**: You would have to write policy defining a `svirt_apache_t` type. 607 608 ## Specifying custom cgroups 609 610 Using the `--cgroup-parent` flag, you can pass a specific cgroup to run a 611 container in. This allows you to create and manage cgroups on their own. You can 612 define custom resources for those cgroups and put containers under a common 613 parent group. 614 615 ## Runtime constraints on resources 616 617 The operator can also adjust the performance parameters of the 618 container: 619 620 | Option | Description | 621 | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | 622 | `-m`, `--memory=""` | Memory limit (format: `<number>[<unit>]`). Number is a positive integer. Unit can be one of `b`, `k`, `m`, or `g`. Minimum is 4M. | 623 | `--memory-swap=""` | Total memory limit (memory + swap, format: `<number>[<unit>]`). Number is a positive integer. Unit can be one of `b`, `k`, `m`, or `g`. | 624 | `--memory-reservation=""` | Memory soft limit (format: `<number>[<unit>]`). Number is a positive integer. Unit can be one of `b`, `k`, `m`, or `g`. | 625 | `--kernel-memory=""` | Kernel memory limit (format: `<number>[<unit>]`). Number is a positive integer. Unit can be one of `b`, `k`, `m`, or `g`. Minimum is 4M. | 626 | `-c`, `--cpu-shares=0` | CPU shares (relative weight) | 627 | `--cpu-period=0` | Limit the CPU CFS (Completely Fair Scheduler) period | 628 | `--cpuset-cpus=""` | CPUs in which to allow execution (0-3, 0,1) | 629 | `--cpuset-mems=""` | Memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems. | 630 | `--cpu-quota=0` | Limit the CPU CFS (Completely Fair Scheduler) quota | 631 | `--blkio-weight=0` | Block IO weight (relative weight) accepts a weight value between 10 and 1000. | 632 | `--blkio-weight-device=""` | Block IO weight (relative device weight, format: `DEVICE_NAME:WEIGHT`) | 633 | `--device-read-bps=""` | Limit read rate from a device (format: `<device-path>:<number>[<unit>]`). Number is a positive integer. Unit can be one of `kb`, `mb`, or `gb`. | 634 | `--device-write-bps=""` | Limit write rate to a device (format: `<device-path>:<number>[<unit>]`). Number is a positive integer. Unit can be one of `kb`, `mb`, or `gb`. | 635 | `--oom-kill-disable=false` | Whether to disable OOM Killer for the container or not. | 636 | `--memory-swappiness=""` | Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100. | 637 | `--shm-size=""` | Size of `/dev/shm`. The format is `<number><unit>`. `number` must be greater than `0`. Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). If you omit the unit, the system uses bytes. If you omit the size entirely, the system uses `64m`. | 638 639 ### User memory constraints 640 641 We have four ways to set user memory usage: 642 643 <table> 644 <thead> 645 <tr> 646 <th>Option</th> 647 <th>Result</th> 648 </tr> 649 </thead> 650 <tbody> 651 <tr> 652 <td class="no-wrap"> 653 <strong>memory=inf, memory-swap=inf</strong> (default) 654 </td> 655 <td> 656 There is no memory limit for the container. The container can use 657 as much memory as needed. 658 </td> 659 </tr> 660 <tr> 661 <td class="no-wrap"><strong>memory=L<inf, memory-swap=inf</strong></td> 662 <td> 663 (specify memory and set memory-swap as <code>-1</code>) The container is 664 not allowed to use more than L bytes of memory, but can use as much swap 665 as is needed (if the host supports swap memory). 666 </td> 667 </tr> 668 <tr> 669 <td class="no-wrap"><strong>memory=L<inf, memory-swap=2*L</strong></td> 670 <td> 671 (specify memory without memory-swap) The container is not allowed to 672 use more than L bytes of memory, swap *plus* memory usage is double 673 of that. 674 </td> 675 </tr> 676 <tr> 677 <td class="no-wrap"> 678 <strong>memory=L<inf, memory-swap=S<inf, L<=S</strong> 679 </td> 680 <td> 681 (specify both memory and memory-swap) The container is not allowed to 682 use more than L bytes of memory, swap *plus* memory usage is limited 683 by S. 684 </td> 685 </tr> 686 </tbody> 687 </table> 688 689 Examples: 690 691 $ docker run -it ubuntu:14.04 /bin/bash 692 693 We set nothing about memory, this means the processes in the container can use 694 as much memory and swap memory as they need. 695 696 $ docker run -it -m 300M --memory-swap -1 ubuntu:14.04 /bin/bash 697 698 We set memory limit and disabled swap memory limit, this means the processes in 699 the container can use 300M memory and as much swap memory as they need (if the 700 host supports swap memory). 701 702 $ docker run -it -m 300M ubuntu:14.04 /bin/bash 703 704 We set memory limit only, this means the processes in the container can use 705 300M memory and 300M swap memory, by default, the total virtual memory size 706 (--memory-swap) will be set as double of memory, in this case, memory + swap 707 would be 2*300M, so processes can use 300M swap memory as well. 708 709 $ docker run -it -m 300M --memory-swap 1G ubuntu:14.04 /bin/bash 710 711 We set both memory and swap memory, so the processes in the container can use 712 300M memory and 700M swap memory. 713 714 Memory reservation is a kind of memory soft limit that allows for greater 715 sharing of memory. Under normal circumstances, containers can use as much of 716 the memory as needed and are constrained only by the hard limits set with the 717 `-m`/`--memory` option. When memory reservation is set, Docker detects memory 718 contention or low memory and forces containers to restrict their consumption to 719 a reservation limit. 720 721 Always set the memory reservation value below the hard limit, otherwise the hard 722 limit takes precedence. A reservation of 0 is the same as setting no 723 reservation. By default (without reservation set), memory reservation is the 724 same as the hard memory limit. 725 726 Memory reservation is a soft-limit feature and does not guarantee the limit 727 won't be exceeded. Instead, the feature attempts to ensure that, when memory is 728 heavily contended for, memory is allocated based on the reservation hints/setup. 729 730 The following example limits the memory (`-m`) to 500M and sets the memory 731 reservation to 200M. 732 733 ```bash 734 $ docker run -it -m 500M --memory-reservation 200M ubuntu:14.04 /bin/bash 735 ``` 736 737 Under this configuration, when the container consumes memory more than 200M and 738 less than 500M, the next system memory reclaim attempts to shrink container 739 memory below 200M. 740 741 The following example set memory reservation to 1G without a hard memory limit. 742 743 ```bash 744 $ docker run -it --memory-reservation 1G ubuntu:14.04 /bin/bash 745 ``` 746 747 The container can use as much memory as it needs. The memory reservation setting 748 ensures the container doesn't consume too much memory for long time, because 749 every memory reclaim shrinks the container's consumption to the reservation. 750 751 By default, kernel kills processes in a container if an out-of-memory (OOM) 752 error occurs. To change this behaviour, use the `--oom-kill-disable` option. 753 Only disable the OOM killer on containers where you have also set the 754 `-m/--memory` option. If the `-m` flag is not set, this can result in the host 755 running out of memory and require killing the host's system processes to free 756 memory. 757 758 The following example limits the memory to 100M and disables the OOM killer for 759 this container: 760 761 $ docker run -it -m 100M --oom-kill-disable ubuntu:14.04 /bin/bash 762 763 The following example, illustrates a dangerous way to use the flag: 764 765 $ docker run -it --oom-kill-disable ubuntu:14.04 /bin/bash 766 767 The container has unlimited memory which can cause the host to run out memory 768 and require killing system processes to free memory. 769 770 ### Kernel memory constraints 771 772 Kernel memory is fundamentally different than user memory as kernel memory can't 773 be swapped out. The inability to swap makes it possible for the container to 774 block system services by consuming too much kernel memory. Kernel memory includes: 775 776 - stack pages 777 - slab pages 778 - sockets memory pressure 779 - tcp memory pressure 780 781 You can setup kernel memory limit to constrain these kinds of memory. For example, 782 every process consumes some stack pages. By limiting kernel memory, you can 783 prevent new processes from being created when the kernel memory usage is too high. 784 785 Kernel memory is never completely independent of user memory. Instead, you limit 786 kernel memory in the context of the user memory limit. Assume "U" is the user memory 787 limit and "K" the kernel limit. There are three possible ways to set limits: 788 789 <table> 790 <thead> 791 <tr> 792 <th>Option</th> 793 <th>Result</th> 794 </tr> 795 </thead> 796 <tbody> 797 <tr> 798 <td class="no-wrap"><strong>U != 0, K = inf</strong> (default)</td> 799 <td> 800 This is the standard memory limitation mechanism already present before using 801 kernel memory. Kernel memory is completely ignored. 802 </td> 803 </tr> 804 <tr> 805 <td class="no-wrap"><strong>U != 0, K < U</strong></td> 806 <td> 807 Kernel memory is a subset of the user memory. This setup is useful in 808 deployments where the total amount of memory per-cgroup is overcommitted. 809 Overcommitting kernel memory limits is definitely not recommended, since the 810 box can still run out of non-reclaimable memory. 811 In this case, the you can configure K so that the sum of all groups is 812 never greater than the total memory. Then, freely set U at the expense of 813 the system's service quality. 814 </td> 815 </tr> 816 <tr> 817 <td class="no-wrap"><strong>U != 0, K > U</strong></td> 818 <td> 819 Since kernel memory charges are also fed to the user counter and reclamation 820 is triggered for the container for both kinds of memory. This configuration 821 gives the admin a unified view of memory. It is also useful for people 822 who just want to track kernel memory usage. 823 </td> 824 </tr> 825 </tbody> 826 </table> 827 828 Examples: 829 830 $ docker run -it -m 500M --kernel-memory 50M ubuntu:14.04 /bin/bash 831 832 We set memory and kernel memory, so the processes in the container can use 833 500M memory in total, in this 500M memory, it can be 50M kernel memory tops. 834 835 $ docker run -it --kernel-memory 50M ubuntu:14.04 /bin/bash 836 837 We set kernel memory without **-m**, so the processes in the container can 838 use as much memory as they want, but they can only use 50M kernel memory. 839 840 ### Swappiness constraint 841 842 By default, a container's kernel can swap out a percentage of anonymous pages. 843 To set this percentage for a container, specify a `--memory-swappiness` value 844 between 0 and 100. A value of 0 turns off anonymous page swapping. A value of 845 100 sets all anonymous pages as swappable. By default, if you are not using 846 `--memory-swappiness`, memory swappiness value will be inherited from the parent. 847 848 For example, you can set: 849 850 $ docker run -it --memory-swappiness=0 ubuntu:14.04 /bin/bash 851 852 Setting the `--memory-swappiness` option is helpful when you want to retain the 853 container's working set and to avoid swapping performance penalties. 854 855 ### CPU share constraint 856 857 By default, all containers get the same proportion of CPU cycles. This proportion 858 can be modified by changing the container's CPU share weighting relative 859 to the weighting of all other running containers. 860 861 To modify the proportion from the default of 1024, use the `-c` or `--cpu-shares` 862 flag to set the weighting to 2 or higher. If 0 is set, the system will ignore the 863 value and use the default of 1024. 864 865 The proportion will only apply when CPU-intensive processes are running. 866 When tasks in one container are idle, other containers can use the 867 left-over CPU time. The actual amount of CPU time will vary depending on 868 the number of containers running on the system. 869 870 For example, consider three containers, one has a cpu-share of 1024 and 871 two others have a cpu-share setting of 512. When processes in all three 872 containers attempt to use 100% of CPU, the first container would receive 873 50% of the total CPU time. If you add a fourth container with a cpu-share 874 of 1024, the first container only gets 33% of the CPU. The remaining containers 875 receive 16.5%, 16.5% and 33% of the CPU. 876 877 On a multi-core system, the shares of CPU time are distributed over all CPU 878 cores. Even if a container is limited to less than 100% of CPU time, it can 879 use 100% of each individual CPU core. 880 881 For example, consider a system with more than three cores. If you start one 882 container `{C0}` with `-c=512` running one process, and another container 883 `{C1}` with `-c=1024` running two processes, this can result in the following 884 division of CPU shares: 885 886 PID container CPU CPU share 887 100 {C0} 0 100% of CPU0 888 101 {C1} 1 100% of CPU1 889 102 {C1} 2 100% of CPU2 890 891 ### CPU period constraint 892 893 The default CPU CFS (Completely Fair Scheduler) period is 100ms. We can use 894 `--cpu-period` to set the period of CPUs to limit the container's CPU usage. 895 And usually `--cpu-period` should work with `--cpu-quota`. 896 897 Examples: 898 899 $ docker run -it --cpu-period=50000 --cpu-quota=25000 ubuntu:14.04 /bin/bash 900 901 If there is 1 CPU, this means the container can get 50% CPU worth of run-time every 50ms. 902 903 For more information, see the [CFS documentation on bandwidth limiting](https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt). 904 905 ### Cpuset constraint 906 907 We can set cpus in which to allow execution for containers. 908 909 Examples: 910 911 $ docker run -it --cpuset-cpus="1,3" ubuntu:14.04 /bin/bash 912 913 This means processes in container can be executed on cpu 1 and cpu 3. 914 915 $ docker run -it --cpuset-cpus="0-2" ubuntu:14.04 /bin/bash 916 917 This means processes in container can be executed on cpu 0, cpu 1 and cpu 2. 918 919 We can set mems in which to allow execution for containers. Only effective 920 on NUMA systems. 921 922 Examples: 923 924 $ docker run -it --cpuset-mems="1,3" ubuntu:14.04 /bin/bash 925 926 This example restricts the processes in the container to only use memory from 927 memory nodes 1 and 3. 928 929 $ docker run -it --cpuset-mems="0-2" ubuntu:14.04 /bin/bash 930 931 This example restricts the processes in the container to only use memory from 932 memory nodes 0, 1 and 2. 933 934 ### CPU quota constraint 935 936 The `--cpu-quota` flag limits the container's CPU usage. The default 0 value 937 allows the container to take 100% of a CPU resource (1 CPU). The CFS (Completely Fair 938 Scheduler) handles resource allocation for executing processes and is default 939 Linux Scheduler used by the kernel. Set this value to 50000 to limit the container 940 to 50% of a CPU resource. For multiple CPUs, adjust the `--cpu-quota` as necessary. 941 For more information, see the [CFS documentation on bandwidth limiting](https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt). 942 943 ### Block IO bandwidth (Blkio) constraint 944 945 By default, all containers get the same proportion of block IO bandwidth 946 (blkio). This proportion is 500. To modify this proportion, change the 947 container's blkio weight relative to the weighting of all other running 948 containers using the `--blkio-weight` flag. 949 950 > **Note:** The blkio weight setting is only available for direct IO. Buffered IO 951 > is not currently supported. 952 953 The `--blkio-weight` flag can set the weighting to a value between 10 to 1000. 954 For example, the commands below create two containers with different blkio 955 weight: 956 957 $ docker run -it --name c1 --blkio-weight 300 ubuntu:14.04 /bin/bash 958 $ docker run -it --name c2 --blkio-weight 600 ubuntu:14.04 /bin/bash 959 960 If you do block IO in the two containers at the same time, by, for example: 961 962 $ time dd if=/mnt/zerofile of=test.out bs=1M count=1024 oflag=direct 963 964 You'll find that the proportion of time is the same as the proportion of blkio 965 weights of the two containers. 966 967 The `--blkio-weight-device="DEVICE_NAME:WEIGHT"` flag sets a specific device weight. 968 The `DEVICE_NAME:WEIGHT` is a string containing a colon-separated device name and weight. 969 For example, to set `/dev/sda` device weight to `200`: 970 971 $ docker run -it \ 972 --blkio-weight-device "/dev/sda:200" \ 973 ubuntu 974 975 If you specify both the `--blkio-weight` and `--blkio-weight-device`, Docker 976 uses the `--blkio-weight` as the default weight and uses `--blkio-weight-device` 977 to override this default with a new value on a specific device. 978 The following example uses a default weight of `300` and overrides this default 979 on `/dev/sda` setting that weight to `200`: 980 981 $ docker run -it \ 982 --blkio-weight 300 \ 983 --blkio-weight-device "/dev/sda:200" \ 984 ubuntu 985 986 The `--device-read-bps` flag limits the read rate from a device. For example, 987 this command creates a container and limits the read rate to `1mb` per second 988 from `/dev/sda`: 989 990 $ docker run -it --device-read-bps /dev/sda:1mb ubuntu 991 992 The `--device-write-bps` flag limits the write rate to a device. For example, 993 this command creates a container and limits the write rate to `1mb` per second 994 for `/dev/sda`: 995 996 $ docker run -it --device-write-bps /dev/sda:1mb ubuntu 997 998 Both flags take limits in the `<device-path>:<limit>[unit]` format. Both read 999 and write rates must be a positive integer. You can specify the rate in `kb` 1000 (kilobytes), `mb` (megabytes), or `gb` (gigabytes). 1001 1002 ## Additional groups 1003 --group-add: Add Linux capabilities 1004 1005 By default, the docker container process runs with the supplementary groups looked 1006 up for the specified user. If one wants to add more to that list of groups, then 1007 one can use this flag: 1008 1009 $ docker run -it --rm --group-add audio --group-add dbus --group-add 777 busybox id 1010 uid=0(root) gid=0(root) groups=10(wheel),29(audio),81(dbus),777 1011 1012 ## Runtime privilege and Linux capabilities 1013 1014 --cap-add: Add Linux capabilities 1015 --cap-drop: Drop Linux capabilities 1016 --privileged=false: Give extended privileges to this container 1017 --device=[]: Allows you to run devices inside the container without the --privileged flag. 1018 1019 By default, Docker containers are "unprivileged" and cannot, for 1020 example, run a Docker daemon inside a Docker container. This is because 1021 by default a container is not allowed to access any devices, but a 1022 "privileged" container is given access to all devices (see 1023 the documentation on [cgroups devices](https://www.kernel.org/doc/Documentation/cgroups/devices.txt)). 1024 1025 When the operator executes `docker run --privileged`, Docker will enable 1026 to access to all devices on the host as well as set some configuration 1027 in AppArmor or SELinux to allow the container nearly all the same access to the 1028 host as processes running outside containers on the host. Additional 1029 information about running with `--privileged` is available on the 1030 [Docker Blog](http://blog.docker.com/2013/09/docker-can-now-run-within-docker/). 1031 1032 If you want to limit access to a specific device or devices you can use 1033 the `--device` flag. It allows you to specify one or more devices that 1034 will be accessible within the container. 1035 1036 $ docker run --device=/dev/snd:/dev/snd ... 1037 1038 By default, the container will be able to `read`, `write`, and `mknod` these devices. 1039 This can be overridden using a third `:rwm` set of options to each `--device` flag: 1040 1041 $ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc 1042 1043 Command (m for help): q 1044 $ docker run --device=/dev/sda:/dev/xvdc:r --rm -it ubuntu fdisk /dev/xvdc 1045 You will not be able to write the partition table. 1046 1047 Command (m for help): q 1048 1049 $ docker run --device=/dev/sda:/dev/xvdc:w --rm -it ubuntu fdisk /dev/xvdc 1050 crash.... 1051 1052 $ docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc 1053 fdisk: unable to open /dev/xvdc: Operation not permitted 1054 1055 In addition to `--privileged`, the operator can have fine grain control over the 1056 capabilities using `--cap-add` and `--cap-drop`. By default, Docker has a default 1057 list of capabilities that are kept. The following table lists the Linux capability options which can be added or dropped. 1058 1059 | Capability Key | Capability Description | 1060 | ---------------- | ----------------------------------------------------------------------------------------------------------------------------- | 1061 | SETPCAP | Modify process capabilities. | 1062 | SYS_MODULE | Load and unload kernel modules. | 1063 | SYS_RAWIO | Perform I/O port operations (iopl(2) and ioperm(2)). | 1064 | SYS_PACCT | Use acct(2), switch process accounting on or off. | 1065 | SYS_ADMIN | Perform a range of system administration operations. | 1066 | SYS_NICE | Raise process nice value (nice(2), setpriority(2)) and change the nice value for arbitrary processes. | 1067 | SYS_RESOURCE | Override resource Limits. | 1068 | SYS_TIME | Set system clock (settimeofday(2), stime(2), adjtimex(2)); set real-time (hardware) clock. | 1069 | SYS_TTY_CONFIG | Use vhangup(2); employ various privileged ioctl(2) operations on virtual terminals. | 1070 | MKNOD | Create special files using mknod(2). | 1071 | AUDIT_WRITE | Write records to kernel auditing log. | 1072 | AUDIT_CONTROL | Enable and disable kernel auditing; change auditing filter rules; retrieve auditing status and filtering rules. | 1073 | MAC_OVERRIDE | Allow MAC configuration or state changes. Implemented for the Smack LSM. | 1074 | MAC_ADMIN | Override Mandatory Access Control (MAC). Implemented for the Smack Linux Security Module (LSM). | 1075 | NET_ADMIN | Perform various network-related operations. | 1076 | SYSLOG | Perform privileged syslog(2) operations. | 1077 | CHOWN | Make arbitrary changes to file UIDs and GIDs (see chown(2)). | 1078 | NET_RAW | Use RAW and PACKET sockets. | 1079 | DAC_OVERRIDE | Bypass file read, write, and execute permission checks. | 1080 | FOWNER | Bypass permission checks on operations that normally require the file system UID of the process to match the UID of the file. | 1081 | DAC_READ_SEARCH | Bypass file read permission checks and directory read and execute permission checks. | 1082 | FSETID | Don't clear set-user-ID and set-group-ID permission bits when a file is modified. | 1083 | KILL | Bypass permission checks for sending signals. | 1084 | SETGID | Make arbitrary manipulations of process GIDs and supplementary GID list. | 1085 | SETUID | Make arbitrary manipulations of process UIDs. | 1086 | LINUX_IMMUTABLE | Set the FS_APPEND_FL and FS_IMMUTABLE_FL i-node flags. | 1087 | NET_BIND_SERVICE | Bind a socket to internet domain privileged ports (port numbers less than 1024). | 1088 | NET_BROADCAST | Make socket broadcasts, and listen to multicasts. | 1089 | IPC_LOCK | Lock memory (mlock(2), mlockall(2), mmap(2), shmctl(2)). | 1090 | IPC_OWNER | Bypass permission checks for operations on System V IPC objects. | 1091 | SYS_CHROOT | Use chroot(2), change root directory. | 1092 | SYS_PTRACE | Trace arbitrary processes using ptrace(2). | 1093 | SYS_BOOT | Use reboot(2) and kexec_load(2), reboot and load a new kernel for later execution. | 1094 | LEASE | Establish leases on arbitrary files (see fcntl(2)). | 1095 | SETFCAP | Set file capabilities. | 1096 | WAKE_ALARM | Trigger something that will wake up the system. | 1097 | BLOCK_SUSPEND | Employ features that can block system suspend. 1098 1099 Further reference information is available on the [capabilities(7) - Linux man page](http://linux.die.net/man/7/capabilities) 1100 1101 Both flags support the value `ALL`, so if the 1102 operator wants to have all capabilities but `MKNOD` they could use: 1103 1104 $ docker run --cap-add=ALL --cap-drop=MKNOD ... 1105 1106 For interacting with the network stack, instead of using `--privileged` they 1107 should use `--cap-add=NET_ADMIN` to modify the network interfaces. 1108 1109 $ docker run -it --rm ubuntu:14.04 ip link add dummy0 type dummy 1110 RTNETLINK answers: Operation not permitted 1111 $ docker run -it --rm --cap-add=NET_ADMIN ubuntu:14.04 ip link add dummy0 type dummy 1112 1113 To mount a FUSE based filesystem, you need to combine both `--cap-add` and 1114 `--device`: 1115 1116 $ docker run --rm -it --cap-add SYS_ADMIN sshfs sshfs sven@10.10.10.20:/home/sven /mnt 1117 fuse: failed to open /dev/fuse: Operation not permitted 1118 $ docker run --rm -it --device /dev/fuse sshfs sshfs sven@10.10.10.20:/home/sven /mnt 1119 fusermount: mount failed: Operation not permitted 1120 $ docker run --rm -it --cap-add SYS_ADMIN --device /dev/fuse sshfs 1121 # sshfs sven@10.10.10.20:/home/sven /mnt 1122 The authenticity of host '10.10.10.20 (10.10.10.20)' can't be established. 1123 ECDSA key fingerprint is 25:34:85:75:25:b0:17:46:05:19:04:93:b5:dd:5f:c6. 1124 Are you sure you want to continue connecting (yes/no)? yes 1125 sven@10.10.10.20's password: 1126 root@30aa0cfaf1b5:/# ls -la /mnt/src/docker 1127 total 1516 1128 drwxrwxr-x 1 1000 1000 4096 Dec 4 06:08 . 1129 drwxrwxr-x 1 1000 1000 4096 Dec 4 11:46 .. 1130 -rw-rw-r-- 1 1000 1000 16 Oct 8 00:09 .dockerignore 1131 -rwxrwxr-x 1 1000 1000 464 Oct 8 00:09 .drone.yml 1132 drwxrwxr-x 1 1000 1000 4096 Dec 4 06:11 .git 1133 -rw-rw-r-- 1 1000 1000 461 Dec 4 06:08 .gitignore 1134 .... 1135 1136 1137 ## Logging drivers (--log-driver) 1138 1139 The container can have a different logging driver than the Docker daemon. Use 1140 the `--log-driver=VALUE` with the `docker run` command to configure the 1141 container's logging driver. The following options are supported: 1142 1143 | Driver | Description | 1144 | ----------- | ----------------------------------------------------------------------------------------------------------------------------- | 1145 | `none` | Disables any logging for the container. `docker logs` won't be available with this driver. | 1146 | `json-file` | Default logging driver for Docker. Writes JSON messages to file. No logging options are supported for this driver. | 1147 | `syslog` | Syslog logging driver for Docker. Writes log messages to syslog. | 1148 | `journald` | Journald logging driver for Docker. Writes log messages to `journald`. | 1149 | `gelf` | Graylog Extended Log Format (GELF) logging driver for Docker. Writes log messages to a GELF endpoint likeGraylog or Logstash. | 1150 | `fluentd` | Fluentd logging driver for Docker. Writes log messages to `fluentd` (forward input). | 1151 | `awslogs` | Amazon CloudWatch Logs logging driver for Docker. Writes log messages to Amazon CloudWatch Logs | 1152 | `splunk` | Splunk logging driver for Docker. Writes log messages to `splunk` using Event Http Collector. | 1153 1154 The `docker logs` command is available only for the `json-file` and `journald` 1155 logging drivers. For detailed information on working with logging drivers, see 1156 [Configure a logging driver](logging/overview.md). 1157 1158 1159 ## Overriding Dockerfile image defaults 1160 1161 When a developer builds an image from a [*Dockerfile*](builder.md) 1162 or when she commits it, the developer can set a number of default parameters 1163 that take effect when the image starts up as a container. 1164 1165 Four of the Dockerfile commands cannot be overridden at runtime: `FROM`, 1166 `MAINTAINER`, `RUN`, and `ADD`. Everything else has a corresponding override 1167 in `docker run`. We'll go through what the developer might have set in each 1168 Dockerfile instruction and how the operator can override that setting. 1169 1170 - [CMD (Default Command or Options)](#cmd-default-command-or-options) 1171 - [ENTRYPOINT (Default Command to Execute at Runtime)]( 1172 #entrypoint-default-command-to-execute-at-runtime) 1173 - [EXPOSE (Incoming Ports)](#expose-incoming-ports) 1174 - [ENV (Environment Variables)](#env-environment-variables) 1175 - [VOLUME (Shared Filesystems)](#volume-shared-filesystems) 1176 - [USER](#user) 1177 - [WORKDIR](#workdir) 1178 1179 ### CMD (default command or options) 1180 1181 Recall the optional `COMMAND` in the Docker 1182 commandline: 1183 1184 $ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] 1185 1186 This command is optional because the person who created the `IMAGE` may 1187 have already provided a default `COMMAND` using the Dockerfile `CMD` 1188 instruction. As the operator (the person running a container from the 1189 image), you can override that `CMD` instruction just by specifying a new 1190 `COMMAND`. 1191 1192 If the image also specifies an `ENTRYPOINT` then the `CMD` or `COMMAND` 1193 get appended as arguments to the `ENTRYPOINT`. 1194 1195 ### ENTRYPOINT (default command to execute at runtime) 1196 1197 --entrypoint="": Overwrite the default entrypoint set by the image 1198 1199 The `ENTRYPOINT` of an image is similar to a `COMMAND` because it 1200 specifies what executable to run when the container starts, but it is 1201 (purposely) more difficult to override. The `ENTRYPOINT` gives a 1202 container its default nature or behavior, so that when you set an 1203 `ENTRYPOINT` you can run the container *as if it were that binary*, 1204 complete with default options, and you can pass in more options via the 1205 `COMMAND`. But, sometimes an operator may want to run something else 1206 inside the container, so you can override the default `ENTRYPOINT` at 1207 runtime by using a string to specify the new `ENTRYPOINT`. Here is an 1208 example of how to run a shell in a container that has been set up to 1209 automatically run something else (like `/usr/bin/redis-server`): 1210 1211 $ docker run -it --entrypoint /bin/bash example/redis 1212 1213 or two examples of how to pass more parameters to that ENTRYPOINT: 1214 1215 $ docker run -it --entrypoint /bin/bash example/redis -c ls -l 1216 $ docker run -it --entrypoint /usr/bin/redis-cli example/redis --help 1217 1218 ### EXPOSE (incoming ports) 1219 1220 The following `run` command options work with container networking: 1221 1222 --expose=[]: Expose a port or a range of ports inside the container. 1223 These are additional to those exposed by the `EXPOSE` instruction 1224 -P=false : Publish all exposed ports to the host interfaces 1225 -p=[] : Publish a container᾿s port or a range of ports to the host 1226 format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort 1227 Both hostPort and containerPort can be specified as a 1228 range of ports. When specifying ranges for both, the 1229 number of container ports in the range must match the 1230 number of host ports in the range, for example: 1231 -p 1234-1236:1234-1236/tcp 1232 1233 When specifying a range for hostPort only, the 1234 containerPort must not be a range. In this case the 1235 container port is published somewhere within the 1236 specified hostPort range. (e.g., `-p 1234-1236:1234/tcp`) 1237 1238 (use 'docker port' to see the actual mapping) 1239 1240 --link="" : Add link to another container (<name or id>:alias or <name or id>) 1241 1242 With the exception of the `EXPOSE` directive, an image developer hasn't 1243 got much control over networking. The `EXPOSE` instruction defines the 1244 initial incoming ports that provide services. These ports are available 1245 to processes inside the container. An operator can use the `--expose` 1246 option to add to the exposed ports. 1247 1248 To expose a container's internal port, an operator can start the 1249 container with the `-P` or `-p` flag. The exposed port is accessible on 1250 the host and the ports are available to any client that can reach the 1251 host. 1252 1253 The `-P` option publishes all the ports to the host interfaces. Docker 1254 binds each exposed port to a random port on the host. The range of 1255 ports are within an *ephemeral port range* defined by 1256 `/proc/sys/net/ipv4/ip_local_port_range`. Use the `-p` flag to 1257 explicitly map a single port or range of ports. 1258 1259 The port number inside the container (where the service listens) does 1260 not need to match the port number exposed on the outside of the 1261 container (where clients connect). For example, inside the container an 1262 HTTP service is listening on port 80 (and so the image developer 1263 specifies `EXPOSE 80` in the Dockerfile). At runtime, the port might be 1264 bound to 42800 on the host. To find the mapping between the host ports 1265 and the exposed ports, use `docker port`. 1266 1267 If the operator uses `--link` when starting a new client container, then the 1268 client container can access the exposed port via a private networking interface. 1269 Linking is a legacy feature that is only supported on the default bridge 1270 network. You should prefer the Docker networks feature instead. For more 1271 information on this feature, see the [*Docker network 1272 overview*""](../userguide/networking/index.md)). 1273 1274 ### ENV (environment variables) 1275 1276 When a new container is created, Docker will set the following environment 1277 variables automatically: 1278 1279 <table> 1280 <tr> 1281 <th>Variable</th> 1282 <th>Value</th> 1283 </tr> 1284 <tr> 1285 <td><code>HOME</code></td> 1286 <td> 1287 Set based on the value of <code>USER</code> 1288 </td> 1289 </tr> 1290 <tr> 1291 <td><code>HOSTNAME</code></td> 1292 <td> 1293 The hostname associated with the container 1294 </td> 1295 </tr> 1296 <tr> 1297 <td><code>PATH</code></td> 1298 <td> 1299 Includes popular directories, such as :<br> 1300 <code>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin</code> 1301 </td> 1302 <tr> 1303 <td><code>TERM</code></td> 1304 <td><code>xterm</code> if the container is allocated a pseudo-TTY</td> 1305 </tr> 1306 </table> 1307 1308 Additionally, the operator can **set any environment variable** in the 1309 container by using one or more `-e` flags, even overriding those mentioned 1310 above, or already defined by the developer with a Dockerfile `ENV`: 1311 1312 $ docker run -e "deep=purple" --rm ubuntu /bin/bash -c export 1313 declare -x HOME="/" 1314 declare -x HOSTNAME="85bc26a0e200" 1315 declare -x OLDPWD 1316 declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" 1317 declare -x PWD="/" 1318 declare -x SHLVL="1" 1319 declare -x deep="purple" 1320 1321 Similarly the operator can set the **hostname** with `-h`. 1322 1323 ### TMPFS (mount tmpfs filesystems) 1324 1325 --tmpfs=[]: Create a tmpfs mount with: container-dir[:<options>], where the options are identical to the Linux `mount -t tmpfs -o` command. 1326 1327 Underlying content from the "container-dir" is copied into tmpfs. 1328 1329 $ docker run -d --tmpfs /run:rw,noexec,nosuid,size=65536k my_image 1330 1331 ### VOLUME (shared filesystems) 1332 1333 -v, --volume=[host-src:]container-dest[:<options>]: Bind mount a volume. 1334 The comma-delimited `options` are [rw|ro], [z|Z], or 1335 [[r]shared|[r]slave|[r]private]. The 'host-src' is an absolute path or a 1336 name value. 1337 1338 If neither 'rw' or 'ro' is specified then the volume is mounted in 1339 read-write mode. 1340 1341 --volumes-from="": Mount all volumes from the given container(s) 1342 1343 > **Note**: 1344 > The auto-creation of the host path has been [*deprecated*](../misc/deprecated.md#auto-creating-missing-host-paths-for-bind-mounts). 1345 1346 The volumes commands are complex enough to have their own documentation 1347 in section [*Managing data in 1348 containers*](../userguide/dockervolumes.md). A developer can define 1349 one or more `VOLUME`'s associated with an image, but only the operator 1350 can give access from one container to another (or from a container to a 1351 volume mounted on the host). 1352 1353 The `container-dest` must always be an absolute path such as `/src/docs`. 1354 The `host-src` can either be an absolute path or a `name` value. If you 1355 supply an absolute path for the `host-dir`, Docker bind-mounts to the path 1356 you specify. If you supply a `name`, Docker creates a named volume by that `name`. 1357 1358 A `name` value must start with start with an alphanumeric character, 1359 followed by `a-z0-9`, `_` (underscore), `.` (period) or `-` (hyphen). 1360 An absolute path starts with a `/` (forward slash). 1361 1362 For example, you can specify either `/foo` or `foo` for a `host-src` value. 1363 If you supply the `/foo` value, Docker creates a bind-mount. If you supply 1364 the `foo` specification, Docker creates a named volume. 1365 1366 ### USER 1367 1368 `root` (id = 0) is the default user within a container. The image developer can 1369 create additional users. Those users are accessible by name. When passing a numeric 1370 ID, the user does not have to exist in the container. 1371 1372 The developer can set a default user to run the first process with the 1373 Dockerfile `USER` instruction. When starting a container, the operator can override 1374 the `USER` instruction by passing the `-u` option. 1375 1376 -u="": Username or UID 1377 1378 > **Note:** if you pass a numeric uid, it must be in the range of 0-2147483647. 1379 1380 ### WORKDIR 1381 1382 The default working directory for running binaries within a container is the 1383 root directory (`/`), but the developer can set a different default with the 1384 Dockerfile `WORKDIR` command. The operator can override this with: 1385 1386 -w="": Working directory inside the container