github.com/sbward/docker@v1.4.2-0.20150114010528-c9dab702bed3/docs/man/docker-run.1.md (about)

     1  % DOCKER(1) Docker User Manuals
     2  % Docker Community
     3  % JUNE 2014
     4  # NAME
     5  docker-run - Run a command in a new container
     6  
     7  # SYNOPSIS
     8  **docker run**
     9  [**-a**|**--attach**[=*[]*]]
    10  [**--add-host**[=*[]*]]
    11  [**-c**|**--cpu-shares**[=*0*]]
    12  [**--cap-add**[=*[]*]]
    13  [**--cap-drop**[=*[]*]]
    14  [**--cidfile**[=*CIDFILE*]]
    15  [**--cpuset**[=*CPUSET*]]
    16  [**-d**|**--detach**[=*false*]]
    17  [**--device**[=*[]*]]
    18  [**--dns-search**[=*[]*]]
    19  [**--dns**[=*[]*]]
    20  [**-e**|**--env**[=*[]*]]
    21  [**--entrypoint**[=*ENTRYPOINT*]]
    22  [**--env-file**[=*[]*]]
    23  [**--expose**[=*[]*]]
    24  [**-h**|**--hostname**[=*HOSTNAME*]]
    25  [**--help**]
    26  [**-i**|**--interactive**[=*false*]]
    27  [**--ipc**[=*IPC*]]
    28  [**--link**[=*[]*]]
    29  [**--lxc-conf**[=*[]*]]
    30  [**-m**|**--memory**[=*MEMORY*]]
    31  [**--mac-address**[=*MAC-ADDRESS*]]
    32  [**--name**[=*NAME*]]
    33  [**--net**[=*"bridge"*]]
    34  [**-P**|**--publish-all**[=*false*]]
    35  [**-p**|**--publish**[=*[]*]]
    36  [**--pid**[=*[]*]]
    37  [**--privileged**[=*false*]]
    38  [**--restart**[=*RESTART*]]
    39  [**--rm**[=*false*]]
    40  [**--security-opt**[=*[]*]]
    41  [**--sig-proxy**[=*true*]]
    42  [**-t**|**--tty**[=*false*]]
    43  [**-u**|**--user**[=*USER*]]
    44  [**-v**|**--volume**[=*[]*]]
    45  [**--volumes-from**[=*[]*]]
    46  [**-w**|**--workdir**[=*WORKDIR*]]
    47  IMAGE [COMMAND] [ARG...]
    48  
    49  # DESCRIPTION
    50  
    51  Run a process in a new container. **docker run** starts a process with its own
    52  file system, its own networking, and its own isolated process tree. The IMAGE
    53  which starts the process may define defaults related to the process that will be
    54  run in the container, the networking to expose, and more, but **docker run**
    55  gives final control to the operator or administrator who starts the container
    56  from the image. For that reason **docker run** has more options than any other
    57  Docker command.
    58  
    59  If the IMAGE is not already loaded then **docker run** will pull the IMAGE, and
    60  all image dependencies, from the repository in the same way running **docker
    61  pull** IMAGE, before it starts the container from that image.
    62  
    63  # OPTIONS
    64  **-a**, **--attach**=[]
    65     Attach to STDIN, STDOUT or STDERR.
    66  
    67     In foreground mode (the default when **-d**
    68  is not specified), **docker run** can start the process in the container
    69  and attach the console to the process’s standard input, output, and standard
    70  error. It can even pretend to be a TTY (this is what most commandline
    71  executables expect) and pass along signals. The **-a** option can be set for
    72  each of stdin, stdout, and stderr.
    73  
    74  **--add-host**=[]
    75     Add a custom host-to-IP mapping (host:ip)
    76  
    77     Add a line to /etc/hosts. The format is hostname:ip.  The **--add-host**
    78  option can be set multiple times.
    79  
    80  **-c**, **--cpu-shares**=0
    81     CPU shares (relative weight)
    82  
    83     You can increase the priority of a container
    84  with the -c option. By default, all containers run at the same priority and get
    85  the same proportion of CPU cycles, but you can tell the kernel to give more
    86  shares of CPU time to one or more containers when you start them via **docker
    87  run**.
    88  
    89  **--cap-add**=[]
    90     Add Linux capabilities
    91  
    92  **--cap-drop**=[]
    93     Drop Linux capabilities
    94  
    95  **--cidfile**=""
    96     Write the container ID to the file
    97  
    98  **--cpuset**=""
    99     CPUs in which to allow execution (0-3, 0,1)
   100  
   101  **-d**, **--detach**=*true*|*false*
   102     Detached mode: run the container in the background and print the new container ID. The default is *false*.
   103  
   104     At any time you can run **docker ps** in
   105  the other shell to view a list of the running containers. You can reattach to a
   106  detached container with **docker attach**. If you choose to run a container in
   107  the detached mode, then you cannot use the **-rm** option.
   108  
   109     When attached in the tty mode, you can detach from a running container without
   110  stopping the process by pressing the keys CTRL-P CTRL-Q.
   111  
   112  **--device**=[]
   113     Add a host device to the container (e.g. --device=/dev/sdc:/dev/xvdc:rwm)
   114  
   115  **--dns-search**=[]
   116     Set custom DNS search domains (Use --dns-search=. if you don't wish to set the search domain)
   117  
   118  **--dns**=[]
   119     Set custom DNS servers
   120  
   121     This option can be used to override the DNS
   122  configuration passed to the container. Typically this is necessary when the
   123  host DNS configuration is invalid for the container (e.g., 127.0.0.1). When this
   124  is the case the **--dns** flags is necessary for every run.
   125  
   126  **-e**, **--env**=[]
   127     Set environment variables
   128  
   129     This option allows you to specify arbitrary
   130  environment variables that are available for the process that will be launched
   131  inside of the container.
   132  
   133  **--entrypoint**=""
   134     Overwrite the default ENTRYPOINT of the image
   135  
   136     This option allows you to overwrite the default entrypoint of the image that
   137  is set in the Dockerfile. The ENTRYPOINT of an image is similar to a COMMAND
   138  because it specifies what executable to run when the container starts, but it is
   139  (purposely) more difficult to override. The ENTRYPOINT gives a container its
   140  default nature or behavior, so that when you set an ENTRYPOINT you can run the
   141  container as if it were that binary, complete with default options, and you can
   142  pass in more options via the COMMAND. But, sometimes an operator may want to run
   143  something else inside the container, so you can override the default ENTRYPOINT
   144  at runtime by using a **--entrypoint** and a string to specify the new
   145  ENTRYPOINT.
   146  
   147  **--env-file**=[]
   148     Read in a line delimited file of environment variables
   149  
   150  **--expose**=[]
   151     Expose a port, or a range of ports (e.g. --expose=3300-3310), from the container without publishing it to your host
   152  
   153  **-h**, **--hostname**=""
   154     Container host name
   155  
   156     Sets the container host name that is available inside the container.
   157  
   158  **--help**
   159    Print usage statement
   160  
   161  **-i**, **--interactive**=*true*|*false*
   162     Keep STDIN open even if not attached. The default is *false*.
   163  
   164     When set to true, keep stdin open even if not attached. The default is false.
   165  
   166  **--ipc**=""
   167     Default is to create a private IPC namespace (POSIX SysV IPC) for the container
   168                                 'container:<name|id>': reuses another container shared memory, semaphores and message queues
   169                                 'host': use the host shared memory,semaphores and message queues inside the container.  Note: the host mode gives the container full access to local shared memory and is therefore considered insecure.
   170  
   171  **--link**=[]
   172     Add link to another container in the form of name:alias
   173  
   174     If the operator
   175  uses **--link** when starting the new client container, then the client
   176  container can access the exposed port via a private networking interface. Docker
   177  will set some environment variables in the client container to help indicate
   178  which interface and port to use.
   179  
   180  **--lxc-conf**=[]
   181     (lxc exec-driver only) Add custom lxc options --lxc-conf="lxc.cgroup.cpuset.cpus = 0,1"
   182  
   183  **-m**, **--memory**=""
   184     Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
   185  
   186     Allows you to constrain the memory available to a container. If the host
   187  supports swap memory, then the -m memory setting can be larger than physical
   188  RAM. If a limit of 0 is specified, the container's memory is not limited. The
   189  actual limit may be rounded up to a multiple of the operating system's page
   190  size, if it is not already. The memory limit should be formatted as follows:
   191  `<number><optional unit>`, where unit = b, k, m or g.
   192  
   193  **--mac-address**=""
   194     Container MAC address (e.g. 92:d0:c6:0a:29:33)
   195  
   196     Remember that the MAC address in an Ethernet network must be unique.
   197  The IPv6 link-local address will be based on the device's MAC address
   198  according to RFC4862.
   199  
   200  **--name**=""
   201     Assign a name to the container
   202  
   203     The operator can identify a container in three ways:
   204      UUID long identifier (“f78375b1c487e03c9438c729345e54db9d20cfa2ac1fc3494b6eb60872e74778”)
   205      UUID short identifier (“f78375b1c487”)
   206      Name (“jonah”)
   207  
   208     The UUID identifiers come from the Docker daemon, and if a name is not assigned
   209  to the container with **--name** then the daemon will also generate a random
   210  string name. The name is useful when defining links (see **--link**) (or any
   211  other place you need to identify a container). This works for both background
   212  and foreground Docker containers.
   213  
   214  **--net**="bridge"
   215     Set the Network mode for the container
   216                                 'bridge': creates a new network stack for the container on the docker bridge
   217                                 'none': no networking for this container
   218                                 'container:<name|id>': reuses another container network stack
   219                                 'host': use the host network stack inside the container.  Note: the host mode gives the container full access to local system services such as D-bus and is therefore considered insecure.
   220  
   221  **-P**, **--publish-all**=*true*|*false*
   222     Publish all exposed ports to random ports on the host interfaces. The default is *false*.
   223  
   224     When set to true publish all exposed ports to the host interfaces. The
   225  default is false. If the operator uses -P (or -p) then Docker will make the
   226  exposed port accessible on the host and the ports will be available to any
   227  client that can reach the host. When using -P, Docker will bind the exposed
   228  ports to a random port on the host between 49153 and 65535. To find the
   229  mapping between the host ports and the exposed ports, use **docker port**.
   230  
   231  **-p**, **--publish**=[]
   232     Publish a container's port, or range of ports, to the host.
   233                                 format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
   234                                 Both hostPort and containerPort can be specified as a range of ports. 
   235                                 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`)
   236                                 (use 'docker port' to see the actual mapping)
   237  
   238  **--pid**=host
   239     Set the PID mode for the container
   240       **host**: use the host's PID namespace inside the container.
   241       Note: the host mode gives the container full access to local PID and is therefore considered insecure.
   242  
   243  **--privileged**=*true*|*false*
   244     Give extended privileges to this container. The default is *false*.
   245  
   246     By default, Docker containers are
   247  “unprivileged” (=false) and cannot, for example, run a Docker daemon inside the
   248  Docker container. This is because by default a container is not allowed to
   249  access any devices. A “privileged” container is given access to all devices.
   250  
   251     When the operator executes **docker run --privileged**, Docker will enable access
   252  to all devices on the host as well as set some configuration in AppArmor to
   253  allow the container nearly all the same access to the host as processes running
   254  outside of a container on the host.
   255  
   256  **--restart**=""
   257     Restart policy to apply when a container exits (no, on-failure[:max-retry], always)
   258  
   259  **--rm**=*true*|*false*
   260     Automatically remove the container when it exits (incompatible with -d). The default is *false*.
   261  
   262  **--security-opt**=[]
   263     Security Options
   264  
   265     "label:user:USER"   : Set the label user for the container
   266      "label:role:ROLE"   : Set the label role for the container
   267      "label:type:TYPE"   : Set the label type for the container
   268      "label:level:LEVEL" : Set the label level for the container
   269      "label:disable"     : Turn off label confinement for the container
   270  
   271  **--sig-proxy**=*true*|*false*
   272     Proxy received signals to the process (non-TTY mode only). SIGCHLD, SIGSTOP, and SIGKILL are not proxied. The default is *true*.
   273  
   274  **-t**, **--tty**=*true*|*false*
   275     Allocate a pseudo-TTY. The default is *false*.
   276  
   277     When set to true Docker can allocate a pseudo-tty and attach to the standard
   278  input of any container. This can be used, for example, to run a throwaway
   279  interactive shell. The default is value is false.
   280  
   281  The **-t** option is incompatible with a redirection of the docker client
   282  standard input.
   283  
   284  **-u**, **--user**=""
   285     Username or UID
   286  
   287  **-v**, **--volume**=[]
   288     Bind mount a volume (e.g., from the host: -v /host:/container, from Docker: -v /container)
   289  
   290     The **-v** option can be used one or
   291  more times to add one or more mounts to a container. These mounts can then be
   292  used in other containers using the **--volumes-from** option.
   293  
   294     The volume may be optionally suffixed with :ro or :rw to mount the volumes in
   295  read-only or read-write mode, respectively. By default, the volumes are mounted
   296  read-write. See examples.
   297  
   298  **--volumes-from**=[]
   299     Mount volumes from the specified container(s)
   300  
   301     Will mount volumes from the specified container identified by container-id.
   302  Once a volume is mounted in a one container it can be shared with other
   303  containers using the **--volumes-from** option when running those other
   304  containers. The volumes can be shared even if the original container with the
   305  mount is not running.
   306  
   307     The container ID may be optionally suffixed with :ro or
   308  :rw to mount the volumes in read-only or read-write mode, respectively. By
   309  default, the volumes are mounted in the same mode (read write or read only) as
   310  the reference container.
   311  
   312  **-w**, **--workdir**=""
   313     Working directory inside the container
   314  
   315     The default working directory for
   316  running binaries within a container is the root directory (/). The developer can
   317  set a different default with the Dockerfile WORKDIR instruction. The operator
   318  can override the working directory by using the **-w** option.
   319  
   320  # EXAMPLES
   321  
   322  ## Exposing log messages from the container to the host's log
   323  
   324  If you want messages that are logged in your container to show up in the host's
   325  syslog/journal then you should bind mount the /dev/log directory as follows.
   326  
   327      # docker run -v /dev/log:/dev/log -i -t fedora /bin/bash
   328  
   329  From inside the container you can test this by sending a message to the log.
   330  
   331      (bash)# logger "Hello from my container"
   332  
   333  Then exit and check the journal.
   334  
   335      # exit
   336  
   337      # journalctl -b | grep Hello
   338  
   339  This should list the message sent to logger.
   340  
   341  ## Attaching to one or more from STDIN, STDOUT, STDERR
   342  
   343  If you do not specify -a then Docker will attach everything (stdin,stdout,stderr)
   344  . You can specify to which of the three standard streams (stdin, stdout, stderr)
   345  you’d like to connect instead, as in:
   346  
   347      # docker run -a stdin -a stdout -i -t fedora /bin/bash
   348  
   349  ## Sharing IPC between containers
   350  
   351  Using shm_server.c available here: http://www.cs.cf.ac.uk/Dave/C/node27.html
   352  
   353  Testing `--ipc=host` mode:
   354  
   355  Host shows a shared memory segment with 7 pids attached, happens to be from httpd:
   356  
   357  ```
   358   $ sudo ipcs -m
   359  
   360   ------ Shared Memory Segments --------
   361   key        shmid      owner      perms      bytes      nattch     status      
   362   0x01128e25 0          root       600        1000       7                       
   363  ```
   364  
   365  Now run a regular container, and it correctly does NOT see the shared memory segment from the host:
   366  
   367  ```
   368   $ sudo docker run -it shm ipcs -m
   369  
   370   ------ Shared Memory Segments --------	
   371   key        shmid      owner      perms      bytes      nattch     status      
   372  ```
   373  
   374  Run a container with the new `--ipc=host` option, and it now sees the shared memory segment from the host httpd:
   375  
   376   ```
   377   $ sudo docker run -it --ipc=host shm ipcs -m
   378  
   379   ------ Shared Memory Segments --------
   380   key        shmid      owner      perms      bytes      nattch     status      
   381   0x01128e25 0          root       600        1000       7                   
   382  ```
   383  Testing `--ipc=container:CONTAINERID` mode:
   384  
   385  Start a container with a program to create a shared memory segment:
   386  ```
   387   sudo docker run -it shm bash
   388   $ sudo shm/shm_server &
   389   $ sudo ipcs -m
   390  
   391   ------ Shared Memory Segments --------
   392   key        shmid      owner      perms      bytes      nattch     status      
   393   0x0000162e 0          root       666        27         1                       
   394  ```
   395  Create a 2nd container correctly shows no shared memory segment from 1st container:
   396  ```
   397   $ sudo docker run shm ipcs -m
   398  
   399   ------ Shared Memory Segments --------
   400   key        shmid      owner      perms      bytes      nattch     status      
   401  ```
   402  
   403  Create a 3rd container using the new --ipc=container:CONTAINERID option, now it shows the shared memory segment from the first:
   404  
   405  ```
   406   $ sudo docker run -it --ipc=container:ed735b2264ac shm ipcs -m
   407   $ sudo ipcs -m
   408  
   409   ------ Shared Memory Segments --------
   410   key        shmid      owner      perms      bytes      nattch     status      
   411   0x0000162e 0          root       666        27         1
   412  ```
   413  
   414  ## Linking Containers
   415  
   416  The link feature allows multiple containers to communicate with each other. For
   417  example, a container whose Dockerfile has exposed port 80 can be run and named
   418  as follows:
   419  
   420      # docker run --name=link-test -d -i -t fedora/httpd
   421  
   422  A second container, in this case called linker, can communicate with the httpd
   423  container, named link-test, by running with the **--link=<name>:<alias>**
   424  
   425      # docker run -t -i --link=link-test:lt --name=linker fedora /bin/bash
   426  
   427  Now the container linker is linked to container link-test with the alias lt.
   428  Running the **env** command in the linker container shows environment variables
   429   with the LT (alias) context (**LT_**)
   430  
   431      # env
   432      HOSTNAME=668231cb0978
   433      TERM=xterm
   434      LT_PORT_80_TCP=tcp://172.17.0.3:80
   435      LT_PORT_80_TCP_PORT=80
   436      LT_PORT_80_TCP_PROTO=tcp
   437      LT_PORT=tcp://172.17.0.3:80
   438      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
   439      PWD=/
   440      LT_NAME=/linker/lt
   441      SHLVL=1
   442      HOME=/
   443      LT_PORT_80_TCP_ADDR=172.17.0.3
   444      _=/usr/bin/env
   445  
   446  When linking two containers Docker will use the exposed ports of the container
   447  to create a secure tunnel for the parent to access.
   448  
   449  
   450  ## Mapping Ports for External Usage
   451  
   452  The exposed port of an application can be mapped to a host port using the **-p**
   453  flag. For example, a httpd port 80 can be mapped to the host port 8080 using the
   454  following:
   455  
   456      # docker run -p 8080:80 -d -i -t fedora/httpd
   457  
   458  ## Creating and Mounting a Data Volume Container
   459  
   460  Many applications require the sharing of persistent data across several
   461  containers. Docker allows you to create a Data Volume Container that other
   462  containers can mount from. For example, create a named container that contains
   463  directories /var/volume1 and /tmp/volume2. The image will need to contain these
   464  directories so a couple of RUN mkdir instructions might be required for you
   465  fedora-data image:
   466  
   467      # docker run --name=data -v /var/volume1 -v /tmp/volume2 -i -t fedora-data true
   468      # docker run --volumes-from=data --name=fedora-container1 -i -t fedora bash
   469  
   470  Multiple --volumes-from parameters will bring together multiple data volumes from
   471  multiple containers. And it's possible to mount the volumes that came from the
   472  DATA container in yet another container via the fedora-container1 intermediary
   473  container, allowing to abstract the actual data source from users of that data:
   474  
   475      # docker run --volumes-from=fedora-container1 --name=fedora-container2 -i -t fedora bash
   476  
   477  ## Mounting External Volumes
   478  
   479  To mount a host directory as a container volume, specify the absolute path to
   480  the directory and the absolute path for the container directory separated by a
   481  colon:
   482  
   483      # docker run -v /var/db:/data1 -i -t fedora bash
   484  
   485  When using SELinux, be aware that the host has no knowledge of container SELinux
   486  policy. Therefore, in the above example, if SELinux policy is enforced, the
   487  `/var/db` directory is not writable to the container. A "Permission Denied"
   488  message will occur and an avc: message in the host's syslog.
   489  
   490  
   491  To work around this, at time of writing this man page, the following command
   492  needs to be run in order for the proper SELinux policy type label to be attached
   493  to the host directory:
   494  
   495      # chcon -Rt svirt_sandbox_file_t /var/db
   496  
   497  
   498  Now, writing to the /data1 volume in the container will be allowed and the
   499  changes will also be reflected on the host in /var/db.
   500  
   501  ## Using alternative security labeling
   502  
   503  You can override the default labeling scheme for each container by specifying
   504  the `--security-opt` flag. For example, you can specify the MCS/MLS level, a
   505  requirement for MLS systems. Specifying the level in the following command
   506  allows you to share the same content between containers.
   507  
   508      # docker run --security-opt label:level:s0:c100,c200 -i -t fedora bash
   509  
   510  An MLS example might be:
   511  
   512      # docker run --security-opt label:level:TopSecret -i -t rhel7 bash
   513  
   514  To disable the security labeling for this container versus running with the
   515  `--permissive` flag, use the following command:
   516  
   517      # docker run --security-opt label:disable -i -t fedora bash
   518  
   519  If you want a tighter security policy on the processes within a container,
   520  you can specify an alternate type for the container. You could run a container
   521  that is only allowed to listen on Apache ports by executing the following
   522  command:
   523  
   524      # docker run --security-opt label:type:svirt_apache_t -i -t centos bash
   525  
   526  Note:
   527  
   528  You would have to write policy defining a `svirt_apache_t` type.
   529  
   530  # HISTORY
   531  April 2014, Originally compiled by William Henry (whenry at redhat dot com)
   532  based on docker.com source material and internal work.
   533  June 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>
   534  July 2014, updated by Sven Dowideit <SvenDowideit@home.org.au>