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