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