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