github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/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 # Docker run reference 6 7 **Docker runs processes in isolated containers**. When an operator 8 executes `docker run`, she starts a process with its own file system, 9 its own networking, and its own isolated process tree. The 10 [*Image*](/terms/image/#image) which starts the process may define 11 defaults related to the binary to run, the networking to expose, and 12 more, but `docker run` gives final control to the operator who starts 13 the container from the image. That's the main reason 14 [*run*](/reference/commandline/cli/#run) has more options than any 15 other `docker` command. 16 17 ## General form 18 19 The basic `docker run` command takes this form: 20 21 $ sudo docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...] 22 23 To learn how to interpret the types of `[OPTIONS]`, 24 see [*Option types*](/reference/commandline/cli/#option-types). 25 26 The list of `[OPTIONS]` breaks down into two groups: 27 28 1. Settings exclusive to operators, including: 29 * Detached or Foreground running, 30 * Container Identification, 31 * Network settings, and 32 * Runtime Constraints on CPU and Memory 33 * Privileges and LXC Configuration 34 2. Settings shared between operators and developers, where operators can 35 override defaults developers set in images at build time. 36 37 Together, the `docker run [OPTIONS]` give the operator complete control over runtime 38 behavior, allowing them to override all defaults set by 39 the developer during `docker build` and nearly all the defaults set by 40 the Docker runtime itself. 41 42 ## Operator exclusive options 43 44 Only the operator (the person executing `docker run`) can set the 45 following options. 46 47 - [Detached vs Foreground](#detached-vs-foreground) 48 - [Detached (-d)](#detached-d) 49 - [Foreground](#foreground) 50 - [Container Identification](#container-identification) 51 - [Name (--name)](#name-name) 52 - [PID Equivalent](#pid-equivalent) 53 - [IPC Settings](#ipc-settings) 54 - [Network Settings](#network-settings) 55 - [Clean Up (--rm)](#clean-up-rm) 56 - [Runtime Constraints on CPU and Memory](#runtime-constraints-on-cpu-and-memory) 57 - [Runtime Privilege, Linux Capabilities, and LXC Configuration](#runtime-privilege-linux-capabilities-and-lxc-configuration) 58 59 ## Detached vs foreground 60 61 When starting a Docker container, you must first decide if you want to 62 run the container in the background in a "detached" mode or in the 63 default foreground mode: 64 65 -d=false: Detached mode: Run container in the background, print new container id 66 67 ### Detached (-d) 68 69 In detached mode (`-d=true` or just `-d`), all I/O should be done 70 through network connections or shared volumes because the container is 71 no longer listening to the command line where you executed `docker run`. 72 You can reattach to a detached container with `docker` 73 [*attach*](/reference/commandline/cli/#attach). If you choose to run a 74 container in the detached mode, then you cannot use the `--rm` option. 75 76 ### Foreground 77 78 In foreground mode (the default when `-d` is not specified), `docker 79 run` can start the process in the container and attach the console to 80 the process's standard input, output, and standard error. It can even 81 pretend to be a TTY (this is what most command line executables expect) 82 and pass along signals. All of that is configurable: 83 84 -a=[] : Attach to `STDIN`, `STDOUT` and/or `STDERR` 85 -t=false : Allocate a pseudo-tty 86 --sig-proxy=true: Proxify all received signal to the process (non-TTY mode only) 87 -i=false : Keep STDIN open even if not attached 88 89 If you do not specify `-a` then Docker will [attach all standard 90 streams]( https://github.com/docker/docker/blob/ 91 75a7f4d90cde0295bcfb7213004abce8d4779b75/commands.go#L1797). You can 92 specify to which of the three standard streams (`STDIN`, `STDOUT`, 93 `STDERR`) you'd like to connect instead, as in: 94 95 $ sudo docker run -a stdin -a stdout -i -t ubuntu /bin/bash 96 97 For interactive processes (like a shell), you must use `-i -t` together in 98 order to allocate a tty for the container process. Specifying `-t` is however 99 forbidden when the client standard output is redirected or pipe, such as in: 100 `echo test | docker run -i busybox cat`. 101 102 ## Container identification 103 104 ### Name (--name) 105 106 The operator can identify a container in three ways: 107 108 - UUID long identifier 109 ("f78375b1c487e03c9438c729345e54db9d20cfa2ac1fc3494b6eb60872e74778") 110 - UUID short identifier ("f78375b1c487") 111 - Name ("evil_ptolemy") 112 113 The UUID identifiers come from the Docker daemon, and if you do not 114 assign a name to the container with `--name` then the daemon will also 115 generate a random string name too. The name can become a handy way to 116 add meaning to a container since you can use this name when defining 117 [*links*](/userguide/dockerlinks) (or any 118 other place you need to identify a container). This works for both 119 background and foreground Docker containers. 120 121 ### PID equivalent 122 123 Finally, to help with automation, you can have Docker write the 124 container ID out to a file of your choosing. This is similar to how some 125 programs might write out their process ID to a file (you've seen them as 126 PID files): 127 128 --cidfile="": Write the container ID to the file 129 130 ### Image[:tag] 131 132 While not strictly a means of identifying a container, you can specify a version of an 133 image you'd like to run the container with by adding `image[:tag]` to the command. For 134 example, `docker run ubuntu:14.04`. 135 136 ## IPC Settings 137 --ipc="" : Set the IPC mode for the container, 138 'container:<name|id>': reuses another container's IPC namespace 139 'host': use the host's IPC namespace inside the container 140 By default, all containers have the IPC namespace enabled 141 142 IPC (POSIX/SysV IPC) namespace provides separation of named shared memory segments, semaphores and message queues. 143 144 Shared memory segments are used to accelerate inter-process communication at 145 memory speed, rather than through pipes or through the network stack. Shared 146 memory is commonly used by databases and custom-built (typically C/OpenMPI, 147 C++/using boost libraries) high performance applications for scientific 148 computing and financial services industries. If these types of applications 149 are broken into multiple containers, you might need to share the IPC mechanisms 150 of the containers. 151 152 ## Network settings 153 154 --dns=[] : Set custom dns servers for the container 155 --net="bridge" : Set the Network mode for the container 156 'bridge': creates a new network stack for the container on the docker bridge 157 'none': no networking for this container 158 'container:<name|id>': reuses another container network stack 159 'host': use the host network stack inside the container 160 --add-host="" : Add a line to /etc/hosts (host:IP) 161 --mac-address="" : Sets the container's Ethernet device's MAC address 162 163 By default, all containers have networking enabled and they can make any 164 outgoing connections. The operator can completely disable networking 165 with `docker run --net none` which disables all incoming and outgoing 166 networking. In cases like this, you would perform I/O through files or 167 `STDIN` and `STDOUT` only. 168 169 Your container will use the same DNS servers as the host by default, but 170 you can override this with `--dns`. 171 172 By default a random MAC is generated. You can set the container's MAC address 173 explicitly by providing a MAC via the `--mac-address` parameter (format: 174 `12:34:56:78:9a:bc`). 175 176 Supported networking modes are: 177 178 * none - no networking in the container 179 * bridge - (default) connect the container to the bridge via veth interfaces 180 * host - use the host's network stack inside the container. Note: This gives the container full access to local system services such as D-bus and is therefore considered insecure. 181 * container - use another container's network stack 182 183 #### Mode: none 184 185 With the networking mode set to `none` a container will not have a 186 access to any external routes. The container will still have a 187 `loopback` interface enabled in the container but it does not have any 188 routes to external traffic. 189 190 #### Mode: bridge 191 192 With the networking mode set to `bridge` a container will use docker's 193 default networking setup. A bridge is setup on the host, commonly named 194 `docker0`, and a pair of `veth` interfaces will be created for the 195 container. One side of the `veth` pair will remain on the host attached 196 to the bridge while the other side of the pair will be placed inside the 197 container's namespaces in addition to the `loopback` interface. An IP 198 address will be allocated for containers on the bridge's network and 199 traffic will be routed though this bridge to the container. 200 201 #### Mode: host 202 203 With the networking mode set to `host` a container will share the host's 204 network stack and all interfaces from the host will be available to the 205 container. The container's hostname will match the hostname on the host 206 system. Publishing ports and linking to other containers will not work 207 when sharing the host's network stack. 208 209 #### Mode: container 210 211 With the networking mode set to `container` a container will share the 212 network stack of another container. The other container's name must be 213 provided in the format of `--net container:<name|id>`. 214 215 Example running a Redis container with Redis binding to `localhost` then 216 running the `redis-cli` command and connecting to the Redis server over the 217 `localhost` interface. 218 219 $ sudo docker run -d --name redis example/redis --bind 127.0.0.1 220 $ # use the redis container's network stack to access localhost 221 $ sudo docker run --rm -ti --net container:redis example/redis-cli -h 127.0.0.1 222 223 ### Managing /etc/hosts 224 225 Your container will have lines in `/etc/hosts` which define the hostname of the 226 container itself as well as `localhost` and a few other common things. The 227 `--add-host` flag can be used to add additional lines to `/etc/hosts`. 228 229 $ /docker run -ti --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts 230 172.17.0.22 09d03f76bf2c 231 fe00::0 ip6-localnet 232 ff00::0 ip6-mcastprefix 233 ff02::1 ip6-allnodes 234 ff02::2 ip6-allrouters 235 127.0.0.1 localhost 236 ::1 localhost ip6-localhost ip6-loopback 237 86.75.30.9 db-static 238 239 ## Clean up (--rm) 240 241 By default a container's file system persists even after the container 242 exits. This makes debugging a lot easier (since you can inspect the 243 final state) and you retain all your data by default. But if you are 244 running short-term **foreground** processes, these container file 245 systems can really pile up. If instead you'd like Docker to 246 **automatically clean up the container and remove the file system when 247 the container exits**, you can add the `--rm` flag: 248 249 --rm=false: Automatically remove the container when it exits (incompatible with -d) 250 251 ## Security configuration 252 --security-opt="label:user:USER" : Set the label user for the container 253 --security-opt="label:role:ROLE" : Set the label role for the container 254 --security-opt="label:type:TYPE" : Set the label type for the container 255 --security-opt="label:level:LEVEL" : Set the label level for the container 256 --security-opt="label:disable" : Turn off label confinement for the container 257 --security-opt="apparmor:PROFILE" : Set the apparmor profile to be applied 258 to the container 259 260 You can override the default labeling scheme for each container by specifying 261 the `--security-opt` flag. For example, you can specify the MCS/MLS level, a 262 requirement for MLS systems. Specifying the level in the following command 263 allows you to share the same content between containers. 264 265 # docker run --security-opt label:level:s0:c100,c200 -i -t fedora bash 266 267 An MLS example might be: 268 269 # docker run --security-opt label:level:TopSecret -i -t rhel7 bash 270 271 To disable the security labeling for this container versus running with the 272 `--permissive` flag, use the following command: 273 274 # docker run --security-opt label:disable -i -t fedora bash 275 276 If you want a tighter security policy on the processes within a container, 277 you can specify an alternate type for the container. You could run a container 278 that is only allowed to listen on Apache ports by executing the following 279 command: 280 281 # docker run --security-opt label:type:svirt_apache_t -i -t centos bash 282 283 Note: 284 285 You would have to write policy defining a `svirt_apache_t` type. 286 287 ## Runtime constraints on CPU and memory 288 289 The operator can also adjust the performance parameters of the 290 container: 291 292 -m="": Memory limit (format: <number><optional unit>, where unit = b, k, m or g) 293 -c=0 : CPU shares (relative weight) 294 295 The operator can constrain the memory available to a container easily 296 with `docker run -m`. If the host supports swap memory, then the `-m` 297 memory setting can be larger than physical RAM. 298 299 Similarly the operator can increase the priority of this container with 300 the `-c` option. By default, all containers run at the same priority and 301 get the same proportion of CPU cycles, but you can tell the kernel to 302 give more shares of CPU time to one or more containers when you start 303 them via Docker. 304 305 The flag `-c` or `--cpu-shares` with value 0 indicates that the running 306 container has access to all 1024 (default) CPU shares. However, this value 307 can be modified to run a container with a different priority or different 308 proportion of CPU cycles. 309 310 E.g., If we start three {C0, C1, C2} containers with default values 311 (`-c` OR `--cpu-shares` = 0) and one {C3} with (`-c` or `--cpu-shares`=512) 312 then C0, C1, and C2 would have access to 100% CPU shares (1024) and C3 would 313 only have access to 50% CPU shares (512). In the context of a time-sliced OS 314 with time quantum set as 100 milliseconds, containers C0, C1, and C2 will run 315 for full-time quantum, and container C3 will run for half-time quantum i.e 50 316 milliseconds. 317 318 ## Runtime privilege, Linux capabilities, and LXC configuration 319 320 --cap-add: Add Linux capabilities 321 --cap-drop: Drop Linux capabilities 322 --privileged=false: Give extended privileges to this container 323 --device=[]: Allows you to run devices inside the container without the --privileged flag. 324 --lxc-conf=[]: (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" 325 326 By default, Docker containers are "unprivileged" and cannot, for 327 example, run a Docker daemon inside a Docker container. This is because 328 by default a container is not allowed to access any devices, but a 329 "privileged" container is given access to all devices (see [lxc-template.go]( 330 https://github.com/docker/docker/blob/master/daemon/execdriver/lxc/lxc_template.go) 331 and documentation on [cgroups devices]( 332 https://www.kernel.org/doc/Documentation/cgroups/devices.txt)). 333 334 When the operator executes `docker run --privileged`, Docker will enable 335 to access to all devices on the host as well as set some configuration 336 in AppArmor or SELinux to allow the container nearly all the same access to the 337 host as processes running outside containers on the host. Additional 338 information about running with `--privileged` is available on the 339 [Docker Blog](http://blog.docker.com/2013/09/docker-can-now-run-within-docker/). 340 341 If you want to limit access to a specific device or devices you can use 342 the `--device` flag. It allows you to specify one or more devices that 343 will be accessible within the container. 344 345 $ sudo docker run --device=/dev/snd:/dev/snd ... 346 347 By default, the container will be able to `read`, `write`, and `mknod` these devices. 348 This can be overridden using a third `:rwm` set of options to each `--device` flag: 349 350 351 ``` 352 $ sudo docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc 353 354 Command (m for help): q 355 $ sudo docker run --device=/dev/sda:/dev/xvdc:r --rm -it ubuntu fdisk /dev/xvdc 356 You will not be able to write the partition table. 357 358 Command (m for help): q 359 360 $ sudo docker run --device=/dev/sda:/dev/xvdc:w --rm -it ubuntu fdisk /dev/xvdc 361 crash.... 362 363 $ sudo docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc 364 fdisk: unable to open /dev/xvdc: Operation not permitted 365 ``` 366 367 In addition to `--privileged`, the operator can have fine grain control over the 368 capabilities using `--cap-add` and `--cap-drop`. By default, Docker has a default 369 list of capabilities that are kept. Both flags support the value `all`, so if the 370 operator wants to have all capabilities but `MKNOD` they could use: 371 372 $ sudo docker run --cap-add=ALL --cap-drop=MKNOD ... 373 374 For interacting with the network stack, instead of using `--privileged` they 375 should use `--cap-add=NET_ADMIN` to modify the network interfaces. 376 377 $ docker run -t -i --rm ubuntu:14.04 ip link add dummy0 type dummy 378 RTNETLINK answers: Operation not permitted 379 $ docker run -t -i --rm --cap-add=NET_ADMIN ubuntu:14.04 ip link add dummy0 type dummy 380 381 To mount a FUSE based filesystem, you need to combine both `--cap-add` and 382 `--device`: 383 384 $ docker run --rm -it --cap-add SYS_ADMIN sshfs sshfs sven@10.10.10.20:/home/sven /mnt 385 fuse: failed to open /dev/fuse: Operation not permitted 386 $ docker run --rm -it --device /dev/fuse sshfs sshfs sven@10.10.10.20:/home/sven /mnt 387 fusermount: mount failed: Operation not permitted 388 $ docker run --rm -it --cap-add SYS_ADMIN --device /dev/fuse sshfs 389 # sshfs sven@10.10.10.20:/home/sven /mnt 390 The authenticity of host '10.10.10.20 (10.10.10.20)' can't be established. 391 ECDSA key fingerprint is 25:34:85:75:25:b0:17:46:05:19:04:93:b5:dd:5f:c6. 392 Are you sure you want to continue connecting (yes/no)? yes 393 sven@10.10.10.20's password: 394 root@30aa0cfaf1b5:/# ls -la /mnt/src/docker 395 total 1516 396 drwxrwxr-x 1 1000 1000 4096 Dec 4 06:08 . 397 drwxrwxr-x 1 1000 1000 4096 Dec 4 11:46 .. 398 -rw-rw-r-- 1 1000 1000 16 Oct 8 00:09 .dockerignore 399 -rwxrwxr-x 1 1000 1000 464 Oct 8 00:09 .drone.yml 400 drwxrwxr-x 1 1000 1000 4096 Dec 4 06:11 .git 401 -rw-rw-r-- 1 1000 1000 461 Dec 4 06:08 .gitignore 402 .... 403 404 405 If the Docker daemon was started using the `lxc` exec-driver 406 (`docker -d --exec-driver=lxc`) then the operator can also specify LXC options 407 using one or more `--lxc-conf` parameters. These can be new parameters or 408 override existing parameters from the [lxc-template.go]( 409 https://github.com/docker/docker/blob/master/daemon/execdriver/lxc/lxc_template.go). 410 Note that in the future, a given host's docker daemon may not use LXC, so this 411 is an implementation-specific configuration meant for operators already 412 familiar with using LXC directly. 413 414 > **Note:** 415 > If you use `--lxc-conf` to modify a container's configuration which is also 416 > managed by the Docker daemon, then the Docker daemon will not know about this 417 > modification, and you will need to manage any conflicts yourself. For example, 418 > you can use `--lxc-conf` to set a container's IP address, but this will not be 419 > reflected in the `/etc/hosts` file. 420 421 ## Overriding Dockerfile image defaults 422 423 When a developer builds an image from a [*Dockerfile*](/reference/builder) 424 or when she commits it, the developer can set a number of default parameters 425 that take effect when the image starts up as a container. 426 427 Four of the Dockerfile commands cannot be overridden at runtime: `FROM`, 428 `MAINTAINER`, `RUN`, and `ADD`. Everything else has a corresponding override 429 in `docker run`. We'll go through what the developer might have set in each 430 Dockerfile instruction and how the operator can override that setting. 431 432 - [CMD (Default Command or Options)](#cmd-default-command-or-options) 433 - [ENTRYPOINT (Default Command to Execute at Runtime)]( 434 #entrypoint-default-command-to-execute-at-runtime) 435 - [EXPOSE (Incoming Ports)](#expose-incoming-ports) 436 - [ENV (Environment Variables)](#env-environment-variables) 437 - [VOLUME (Shared Filesystems)](#volume-shared-filesystems) 438 - [USER](#user) 439 - [WORKDIR](#workdir) 440 441 ## CMD (default command or options) 442 443 Recall the optional `COMMAND` in the Docker 444 commandline: 445 446 $ sudo docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...] 447 448 This command is optional because the person who created the `IMAGE` may 449 have already provided a default `COMMAND` using the Dockerfile `CMD` 450 instruction. As the operator (the person running a container from the 451 image), you can override that `CMD` instruction just by specifying a new 452 `COMMAND`. 453 454 If the image also specifies an `ENTRYPOINT` then the `CMD` or `COMMAND` 455 get appended as arguments to the `ENTRYPOINT`. 456 457 ## ENTRYPOINT (default command to execute at runtime) 458 459 --entrypoint="": Overwrite the default entrypoint set by the image 460 461 The `ENTRYPOINT` of an image is similar to a `COMMAND` because it 462 specifies what executable to run when the container starts, but it is 463 (purposely) more difficult to override. The `ENTRYPOINT` gives a 464 container its default nature or behavior, so that when you set an 465 `ENTRYPOINT` you can run the container *as if it were that binary*, 466 complete with default options, and you can pass in more options via the 467 `COMMAND`. But, sometimes an operator may want to run something else 468 inside the container, so you can override the default `ENTRYPOINT` at 469 runtime by using a string to specify the new `ENTRYPOINT`. Here is an 470 example of how to run a shell in a container that has been set up to 471 automatically run something else (like `/usr/bin/redis-server`): 472 473 $ sudo docker run -i -t --entrypoint /bin/bash example/redis 474 475 or two examples of how to pass more parameters to that ENTRYPOINT: 476 477 $ sudo docker run -i -t --entrypoint /bin/bash example/redis -c ls -l 478 $ sudo docker run -i -t --entrypoint /usr/bin/redis-cli example/redis --help 479 480 ## EXPOSE (incoming ports) 481 482 The Dockerfile doesn't give much control over networking, only providing 483 the `EXPOSE` instruction to give a hint to the operator about what 484 incoming ports might provide services. The following options work with 485 or override the Dockerfile's exposed defaults: 486 487 --expose=[]: Expose a port or a range of ports from the container 488 without publishing it to your host 489 -P=false : Publish all exposed ports to the host interfaces 490 -p=[] : Publish a container᾿s port or a range of ports to the host 491 format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort 492 Both hostPort and containerPort can be specified as a range of ports. 493 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`) 494 (use 'docker port' to see the actual mapping) 495 --link="" : Add link to another container (name:alias) 496 497 As mentioned previously, `EXPOSE` (and `--expose`) makes ports available 498 **in** a container for incoming connections. The port number on the 499 inside of the container (where the service listens) does not need to be 500 the same number as the port exposed on the outside of the container 501 (where clients connect), so inside the container you might have an HTTP 502 service listening on port 80 (and so you `EXPOSE 80` in the Dockerfile), 503 but outside the container the port might be 42800. 504 505 To help a new client container reach the server container's internal 506 port operator `--expose`'d by the operator or `EXPOSE`'d by the 507 developer, the operator has three choices: start the server container 508 with `-P` or `-p,` or start the client container with `--link`. 509 510 If the operator uses `-P` or `-p` then Docker will make the exposed port 511 accessible on the host and the ports will be available to any client 512 that can reach the host. When using `-P`, Docker will bind the exposed 513 ports to a random port on the host between 49153 and 65535. To find the 514 mapping between the host ports and the exposed ports, use `docker port`. 515 516 If the operator uses `--link` when starting the new client container, 517 then the client container can access the exposed port via a private 518 networking interface. Docker will set some environment variables in the 519 client container to help indicate which interface and port to use. 520 521 ## ENV (environment variables) 522 523 When a new container is created, Docker will set the following environment 524 variables automatically: 525 526 <table width=100%> 527 <tr style="background-color:#C0C0C0"> 528 <td> <b>Variable</b> </td> 529 <td style="padding-left:10px"> <b>Value</b> </td> 530 </tr> 531 <tr> 532 <td> <code>HOME</code> </td> 533 <td style="padding-left:10px"> 534 Set based on the value of <code>USER</code> 535 </td> 536 </tr> 537 <tr style="background-color:#E8E8E8"> 538 <td valign=top> <code>HOSTNAME</code> </td> 539 <td style="padding-left:10px"> 540 The hostname associated with the container 541 </td> 542 </tr> 543 <tr> 544 <td valign=top> <code>PATH</code> </td> 545 <td style="padding-left:10px"> 546 Includes popular directories, such as :<br> 547 <code>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin</code> 548 </td> 549 <tr style="background-color:#E8E8E8"> 550 <td valign=top> <code>TERM</code> </td> 551 <td style="padding-left:10px"> 552 <code>xterm</code> if the container is allocated a psuedo-TTY 553 </td> 554 </tr> 555 </table> 556 557 The container may also include environment variables defined 558 as a result of the container being linked with another container. See 559 the [*Container Links*](/userguide/dockerlinks/#container-linking) 560 section for more details. 561 562 Additionally, the operator can **set any environment variable** in the 563 container by using one or more `-e` flags, even overriding those mentioned 564 above, or already defined by the developer with a Dockerfile `ENV`: 565 566 $ sudo docker run -e "deep=purple" --rm ubuntu /bin/bash -c export 567 declare -x HOME="/" 568 declare -x HOSTNAME="85bc26a0e200" 569 declare -x OLDPWD 570 declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" 571 declare -x PWD="/" 572 declare -x SHLVL="1" 573 declare -x container="lxc" 574 declare -x deep="purple" 575 576 Similarly the operator can set the **hostname** with `-h`. 577 578 `--link name:alias` also sets environment variables, using the *alias* string to 579 define environment variables within the container that give the IP and PORT 580 information for connecting to the service container. Let's imagine we have a 581 container running Redis: 582 583 # Start the service container, named redis-name 584 $ sudo docker run -d --name redis-name dockerfiles/redis 585 4241164edf6f5aca5b0e9e4c9eccd899b0b8080c64c0cd26efe02166c73208f3 586 587 # The redis-name container exposed port 6379 588 $ sudo docker ps 589 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 590 4241164edf6f $ dockerfiles/redis:latest /redis-stable/src/re 5 seconds ago Up 4 seconds 6379/tcp redis-name 591 592 # Note that there are no public ports exposed since we didn᾿t use -p or -P 593 $ sudo docker port 4241164edf6f 6379 594 2014/01/25 00:55:38 Error: No public port '6379' published for 4241164edf6f 595 596 Yet we can get information about the Redis container's exposed ports 597 with `--link`. Choose an alias that will form a 598 valid environment variable! 599 600 $ sudo docker run --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c export 601 declare -x HOME="/" 602 declare -x HOSTNAME="acda7f7b1cdc" 603 declare -x OLDPWD 604 declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" 605 declare -x PWD="/" 606 declare -x REDIS_ALIAS_NAME="/distracted_wright/redis" 607 declare -x REDIS_ALIAS_PORT="tcp://172.17.0.32:6379" 608 declare -x REDIS_ALIAS_PORT_6379_TCP="tcp://172.17.0.32:6379" 609 declare -x REDIS_ALIAS_PORT_6379_TCP_ADDR="172.17.0.32" 610 declare -x REDIS_ALIAS_PORT_6379_TCP_PORT="6379" 611 declare -x REDIS_ALIAS_PORT_6379_TCP_PROTO="tcp" 612 declare -x SHLVL="1" 613 declare -x container="lxc" 614 615 And we can use that information to connect from another container as a client: 616 617 $ sudo 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' 618 172.17.0.32:6379> 619 620 Docker will also map the private IP address to the alias of a linked 621 container by inserting an entry into `/etc/hosts`. You can use this 622 mechanism to communicate with a linked container by its alias: 623 624 $ sudo docker run -d --name servicename busybox sleep 30 625 $ sudo docker run -i -t --link servicename:servicealias busybox ping -c 1 servicealias 626 627 If you restart the source container (`servicename` in this case), the recipient 628 container's `/etc/hosts` entry will be automatically updated. 629 630 ## VOLUME (shared filesystems) 631 632 -v=[]: Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. 633 If "container-dir" is missing, then docker creates a new volume. 634 --volumes-from="": Mount all volumes from the given container(s) 635 636 The volumes commands are complex enough to have their own documentation 637 in section [*Managing data in 638 containers*](/userguide/dockervolumes). A developer can define 639 one or more `VOLUME`'s associated with an image, but only the operator 640 can give access from one container to another (or from a container to a 641 volume mounted on the host). 642 643 ## USER 644 645 The default user within a container is `root` (id = 0), but if the 646 developer created additional users, those are accessible too. The 647 developer can set a default user to run the first process with the 648 Dockerfile `USER` instruction, but the operator can override it: 649 650 -u="": Username or UID 651 652 > **Note:** if you pass numeric uid, it must be in range 0-2147483647. 653 654 ## WORKDIR 655 656 The default working directory for running binaries within a container is the 657 root directory (`/`), but the developer can set a different default with the 658 Dockerfile `WORKDIR` command. The operator can override this with: 659 660 -w="": Working directory inside the container