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