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