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