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