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