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