github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/docs/reference/commandline/run.md (about) 1 <!--[metadata]> 2 +++ 3 title = "run" 4 description = "The run command description and usage" 5 keywords = ["run, command, container"] 6 [menu.main] 7 parent = "smn_cli" 8 weight=1 9 +++ 10 <![end-metadata]--> 11 12 # run 13 14 Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 15 16 Run a command in a new container 17 18 -a, --attach=[] Attach to STDIN, STDOUT or STDERR 19 --add-host=[] Add a custom host-to-IP mapping (host:ip) 20 --blkio-weight=0 Block IO weight (relative weight) 21 -c, --cpu-shares=0 CPU shares (relative weight) 22 --cap-add=[] Add Linux capabilities 23 --cap-drop=[] Drop Linux capabilities 24 --cgroup-parent="" Optional parent cgroup for the container 25 --cidfile="" Write the container ID to the file 26 --cpu-period=0 Limit CPU CFS (Completely Fair Scheduler) period 27 --cpu-quota=0 Limit CPU CFS (Completely Fair Scheduler) quota 28 --cpuset-cpus="" CPUs in which to allow execution (0-3, 0,1) 29 --cpuset-mems="" Memory nodes (MEMs) in which to allow execution (0-3, 0,1) 30 -d, --detach=false Run container in background and print container ID 31 --device=[] Add a host device to the container 32 --dns=[] Set custom DNS servers 33 --dns-opt=[] Set custom DNS options 34 --dns-search=[] Set custom DNS search domains 35 -e, --env=[] Set environment variables 36 --entrypoint="" Overwrite the default ENTRYPOINT of the image 37 --env-file=[] Read in a file of environment variables 38 --expose=[] Expose a port or a range of ports 39 --group-add=[] Add additional groups to run as 40 -h, --hostname="" Container host name 41 --help=false Print usage 42 -i, --interactive=false Keep STDIN open even if not attached 43 --ipc="" IPC namespace to use 44 --kernel-memory="" Kernel memory limit 45 -l, --label=[] Set metadata on the container (e.g., --label=com.example.key=value) 46 --label-file=[] Read in a file of labels (EOL delimited) 47 --link=[] Add link to another container 48 --log-driver="" Logging driver for container 49 --log-opt=[] Log driver specific options 50 --lxc-conf=[] Add custom lxc options 51 -m, --memory="" Memory limit 52 --mac-address="" Container MAC address (e.g. 92:d0:c6:0a:29:33) 53 --memory-swap="" Total memory (memory + swap), '-1' to disable swap 54 --memory-swappiness="" Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100. 55 --name="" Assign a name to the container 56 --net="bridge" Set the Network mode for the container 57 --oom-kill-disable=false Whether to disable OOM Killer for the container or not 58 -P, --publish-all=false Publish all exposed ports to random ports 59 -p, --publish=[] Publish a container's port(s) to the host 60 --pid="" PID namespace to use 61 --privileged=false Give extended privileges to this container 62 --read-only=false Mount the container's root filesystem as read only 63 --restart="no" Restart policy (no, on-failure[:max-retry], always, unless-stopped) 64 --rm=false Automatically remove the container when it exits 65 --security-opt=[] Security Options 66 --stop-signal="SIGTERM" Signal to stop a container 67 --sig-proxy=true Proxy received signals to the process 68 -t, --tty=false Allocate a pseudo-TTY 69 -u, --user="" Username or UID (format: <name|uid>[:<group|gid>]) 70 --ulimit=[] Ulimit options 71 --disable-content-trust=true Skip image verification 72 --uts="" UTS namespace to use 73 -v, --volume=[] Bind mount a volume 74 --volumes-from=[] Mount volumes from the specified container(s) 75 -w, --workdir="" Working directory inside the container 76 77 The `docker run` command first `creates` a writeable container layer over the 78 specified image, and then `starts` it using the specified command. That is, 79 `docker run` is equivalent to the API `/containers/create` then 80 `/containers/(id)/start`. A stopped container can be restarted with all its 81 previous changes intact using `docker start`. See `docker ps -a` to view a list 82 of all containers. 83 84 There is detailed information about `docker run` in the [Docker run reference]( 85 /reference/run/). 86 87 The `docker run` command can be used in combination with `docker commit` to 88 [*change the command that a container runs*](/reference/commandline/commit). 89 90 See the [Docker User Guide](/userguide/dockerlinks/) for more detailed 91 information about the `--expose`, `-p`, `-P` and `--link` parameters, 92 and linking containers. 93 94 ## Examples 95 96 $ docker run --name test -it debian 97 root@d6c0fe130dba:/# exit 13 98 $ echo $? 99 13 100 $ docker ps -a | grep test 101 d6c0fe130dba debian:7 "/bin/bash" 26 seconds ago Exited (13) 17 seconds ago test 102 103 This example runs a container named `test` using the `debian:latest` 104 image. The `-it` instructs Docker to allocate a pseudo-TTY connected to 105 the container's stdin; creating an interactive `bash` shell in the container. 106 In the example, the `bash` shell is quit by entering 107 `exit 13`. This exit code is passed on to the caller of 108 `docker run`, and is recorded in the `test` container's metadata. 109 110 $ docker run --cidfile /tmp/docker_test.cid ubuntu echo "test" 111 112 This will create a container and print `test` to the console. The `cidfile` 113 flag makes Docker attempt to create a new file and write the container ID to it. 114 If the file exists already, Docker will return an error. Docker will close this 115 file when `docker run` exits. 116 117 $ docker run -t -i --rm ubuntu bash 118 root@bc338942ef20:/# mount -t tmpfs none /mnt 119 mount: permission denied 120 121 This will *not* work, because by default, most potentially dangerous kernel 122 capabilities are dropped; including `cap_sys_admin` (which is required to mount 123 filesystems). However, the `--privileged` flag will allow it to run: 124 125 $ docker run --privileged ubuntu bash 126 root@50e3f57e16e6:/# mount -t tmpfs none /mnt 127 root@50e3f57e16e6:/# df -h 128 Filesystem Size Used Avail Use% Mounted on 129 none 1.9G 0 1.9G 0% /mnt 130 131 The `--privileged` flag gives *all* capabilities to the container, and it also 132 lifts all the limitations enforced by the `device` cgroup controller. In other 133 words, the container can then do almost everything that the host can do. This 134 flag exists to allow special use-cases, like running Docker within Docker. 135 136 $ docker run -w /path/to/dir/ -i -t ubuntu pwd 137 138 The `-w` lets the command being executed inside directory given, here 139 `/path/to/dir/`. If the path does not exists it is created inside the container. 140 141 $ docker run -v `pwd`:`pwd` -w `pwd` -i -t ubuntu pwd 142 143 The `-v` flag mounts the current working directory into the container. The `-w` 144 lets the command being executed inside the current working directory, by 145 changing into the directory to the value returned by `pwd`. So this 146 combination executes the command using the container, but inside the 147 current working directory. 148 149 $ docker run -v /doesnt/exist:/foo -w /foo -i -t ubuntu bash 150 151 When the host directory of a bind-mounted volume doesn't exist, Docker 152 will automatically create this directory on the host for you. In the 153 example above, Docker will create the `/doesnt/exist` 154 folder before starting your container. 155 156 $ docker run --read-only -v /icanwrite busybox touch /icanwrite here 157 158 Volumes can be used in combination with `--read-only` to control where 159 a container writes files. The `--read-only` flag mounts the container's root 160 filesystem as read only prohibiting writes to locations other than the 161 specified volumes for the container. 162 163 $ docker run -t -i -v /var/run/docker.sock:/var/run/docker.sock -v ./static-docker:/usr/bin/docker busybox sh 164 165 By bind-mounting the docker unix socket and statically linked docker 166 binary (such as that provided by [https://get.docker.com]( 167 https://get.docker.com)), you give the container the full access to create and 168 manipulate the host's Docker daemon. 169 170 $ docker run -p 127.0.0.1:80:8080 ubuntu bash 171 172 This binds port `8080` of the container to port `80` on `127.0.0.1` of 173 the host machine. The [Docker User Guide](/userguide/dockerlinks/) 174 explains in detail how to manipulate ports in Docker. 175 176 $ docker run --expose 80 ubuntu bash 177 178 This exposes port `80` of the container for use within a link without 179 publishing the port to the host system's interfaces. The [Docker User 180 Guide](/userguide/dockerlinks) explains in detail how to manipulate 181 ports in Docker. 182 183 $ docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash 184 185 This sets environmental variables in the container. For illustration all three 186 flags are shown here. Where `-e`, `--env` take an environment variable and 187 value, or if no `=` is provided, then that variable's current value is passed 188 through (i.e. `$MYVAR1` from the host is set to `$MYVAR1` in the container). 189 When no `=` is provided and that variable is not defined in the client's 190 environment then that variable will be removed from the container's list of 191 environment variables. 192 All three flags, `-e`, `--env` and `--env-file` can be repeated. 193 194 Regardless of the order of these three flags, the `--env-file` are processed 195 first, and then `-e`, `--env` flags. This way, the `-e` or `--env` will 196 override variables as needed. 197 198 $ cat ./env.list 199 TEST_FOO=BAR 200 $ docker run --env TEST_FOO="This is a test" --env-file ./env.list busybox env | grep TEST_FOO 201 TEST_FOO=This is a test 202 203 The `--env-file` flag takes a filename as an argument and expects each line 204 to be in the `VAR=VAL` format, mimicking the argument passed to `--env`. Comment 205 lines need only be prefixed with `#` 206 207 An example of a file passed with `--env-file` 208 209 $ cat ./env.list 210 TEST_FOO=BAR 211 212 # this is a comment 213 TEST_APP_DEST_HOST=10.10.0.127 214 TEST_APP_DEST_PORT=8888 215 _TEST_BAR=FOO 216 TEST_APP_42=magic 217 helloWorld=true 218 # 123qwe=bar <- is not valid 219 220 # pass through this variable from the caller 221 TEST_PASSTHROUGH 222 $ TEST_PASSTHROUGH=howdy docker run --env-file ./env.list busybox env 223 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 224 HOSTNAME=5198e0745561 225 TEST_FOO=BAR 226 TEST_APP_DEST_HOST=10.10.0.127 227 TEST_APP_DEST_PORT=8888 228 _TEST_BAR=FOO 229 TEST_APP_42=magic 230 helloWorld=true 231 TEST_PASSTHROUGH=howdy 232 HOME=/root 233 234 $ docker run --env-file ./env.list busybox env 235 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 236 HOSTNAME=5198e0745561 237 TEST_FOO=BAR 238 TEST_APP_DEST_HOST=10.10.0.127 239 TEST_APP_DEST_PORT=8888 240 _TEST_BAR=FOO 241 TEST_APP_42=magic 242 helloWorld=true 243 TEST_PASSTHROUGH= 244 HOME=/root 245 246 > **Note**: Environment variables names must consist solely of letters, numbers, 247 > and underscores - and cannot start with a number. 248 249 A label is a a `key=value` pair that applies metadata to a container. To label a container with two labels: 250 251 $ docker run -l my-label --label com.example.foo=bar ubuntu bash 252 253 The `my-label` key doesn't specify a value so the label defaults to an empty 254 string(`""`). To add multiple labels, repeat the label flag (`-l` or `--label`). 255 256 The `key=value` must be unique to avoid overwriting the label value. If you 257 specify labels with identical keys but different values, each subsequent value 258 overwrites the previous. Docker uses the last `key=value` you supply. 259 260 Use the `--label-file` flag to load multiple labels from a file. Delimit each 261 label in the file with an EOL mark. The example below loads labels from a 262 labels file in the current directory: 263 264 $ docker run --label-file ./labels ubuntu bash 265 266 The label-file format is similar to the format for loading environment 267 variables. (Unlike environment variables, labels are not visible to processes 268 running inside a container.) The following example illustrates a label-file 269 format: 270 271 com.example.label1="a label" 272 273 # this is a comment 274 com.example.label2=another\ label 275 com.example.label3 276 277 You can load multiple label-files by supplying multiple `--label-file` flags. 278 279 For additional information on working with labels, see [*Labels - custom 280 metadata in Docker*](/userguide/labels-custom-metadata/) in the Docker User 281 Guide. 282 283 $ docker run --link /redis:redis --name console ubuntu bash 284 285 The `--link` flag will link the container named `/redis` into the newly 286 created container with the alias `redis`. The new container can access the 287 network and environment of the `redis` container via environment variables. 288 The `--link` flag will also just accept the form `<name or id>` in which case 289 the alias will match the name. For instance, you could have written the previous 290 example as: 291 292 $ docker run --link redis --name console ubuntu bash 293 294 The `--name` flag will assign the name `console` to the newly created 295 container. 296 297 $ docker run --volumes-from 777f7dc92da7 --volumes-from ba8c0c54f0f2:ro -i -t ubuntu pwd 298 299 The `--volumes-from` flag mounts all the defined volumes from the referenced 300 containers. Containers can be specified by repetitions of the `--volumes-from` 301 argument. The container ID may be optionally suffixed with `:ro` or `:rw` to 302 mount the volumes in read-only or read-write mode, respectively. By default, 303 the volumes are mounted in the same mode (read write or read only) as 304 the reference container. 305 306 Labeling systems like SELinux require that proper labels are placed on volume 307 content mounted into a container. Without a label, the security system might 308 prevent the processes running inside the container from using the content. By 309 default, Docker does not change the labels set by the OS. 310 311 To change the label in the container context, you can add either of two suffixes 312 `:z` or `:Z` to the volume mount. These suffixes tell Docker to relabel file 313 objects on the shared volumes. The `z` option tells Docker that two containers 314 share the volume content. As a result, Docker labels the content with a shared 315 content label. Shared volume labels allow all containers to read/write content. 316 The `Z` option tells Docker to label the content with a private unshared label. 317 Only the current container can use a private volume. 318 319 The `-a` flag tells `docker run` to bind to the container's `STDIN`, `STDOUT` 320 or `STDERR`. This makes it possible to manipulate the output and input as 321 needed. 322 323 $ echo "test" | docker run -i -a stdin ubuntu cat - 324 325 This pipes data into a container and prints the container's ID by attaching 326 only to the container's `STDIN`. 327 328 $ docker run -a stderr ubuntu echo test 329 330 This isn't going to print anything unless there's an error because we've 331 only attached to the `STDERR` of the container. The container's logs 332 still store what's been written to `STDERR` and `STDOUT`. 333 334 $ cat somefile | docker run -i -a stdin mybuilder dobuild 335 336 This is how piping a file into a container could be done for a build. 337 The container's ID will be printed after the build is done and the build 338 logs could be retrieved using `docker logs`. This is 339 useful if you need to pipe a file or something else into a container and 340 retrieve the container's ID once the container has finished running. 341 342 $ docker run --device=/dev/sdc:/dev/xvdc --device=/dev/sdd --device=/dev/zero:/dev/nulo -i -t ubuntu ls -l /dev/{xvdc,sdd,nulo} 343 brw-rw---- 1 root disk 8, 2 Feb 9 16:05 /dev/xvdc 344 brw-rw---- 1 root disk 8, 3 Feb 9 16:05 /dev/sdd 345 crw-rw-rw- 1 root root 1, 5 Feb 9 16:05 /dev/nulo 346 347 It is often necessary to directly expose devices to a container. The `--device` 348 option enables that. For example, a specific block storage device or loop 349 device or audio device can be added to an otherwise unprivileged container 350 (without the `--privileged` flag) and have the application directly access it. 351 352 By default, the container will be able to `read`, `write` and `mknod` these devices. 353 This can be overridden using a third `:rwm` set of options to each `--device` 354 flag: 355 356 357 $ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc 358 359 Command (m for help): q 360 $ docker run --device=/dev/sda:/dev/xvdc:ro --rm -it ubuntu fdisk /dev/xvdc 361 You will not be able to write the partition table. 362 363 Command (m for help): q 364 365 $ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc 366 367 Command (m for help): q 368 369 $ docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc 370 fdisk: unable to open /dev/xvdc: Operation not permitted 371 372 > **Note:** 373 > `--device` cannot be safely used with ephemeral devices. Block devices 374 > that may be removed should not be added to untrusted containers with 375 > `--device`. 376 377 **A complete example:** 378 379 $ docker run -d --name static static-web-files sh 380 $ docker run -d --expose=8098 --name riak riakserver 381 $ docker run -d -m 100m -e DEVELOPMENT=1 -e BRANCH=example-code -v $(pwd):/app/bin:ro --name app appserver 382 $ docker run -d -p 1443:443 --dns=10.0.0.1 --dns-search=dev.org -v /var/log/httpd --volumes-from static --link riak --link app -h www.sven.dev.org --name web webserver 383 $ docker run -t -i --rm --volumes-from web -w /var/log/httpd busybox tail -f access.log 384 385 This example shows five containers that might be set up to test a web 386 application change: 387 388 1. Start a pre-prepared volume image `static-web-files` (in the background) 389 that has CSS, image and static HTML in it, (with a `VOLUME` instruction in 390 the Dockerfile to allow the web server to use those files); 391 2. Start a pre-prepared `riakserver` image, give the container name `riak` and 392 expose port `8098` to any containers that link to it; 393 3. Start the `appserver` image, restricting its memory usage to 100MB, setting 394 two environment variables `DEVELOPMENT` and `BRANCH` and bind-mounting the 395 current directory (`$(pwd)`) in the container in read-only mode as `/app/bin`; 396 4. Start the `webserver`, mapping port `443` in the container to port `1443` on 397 the Docker server, setting the DNS server to `10.0.0.1` and DNS search 398 domain to `dev.org`, creating a volume to put the log files into (so we can 399 access it from another container), then importing the files from the volume 400 exposed by the `static` container, and linking to all exposed ports from 401 `riak` and `app`. Lastly, we set the hostname to `web.sven.dev.org` so its 402 consistent with the pre-generated SSL certificate; 403 5. Finally, we create a container that runs `tail -f access.log` using the logs 404 volume from the `web` container, setting the workdir to `/var/log/httpd`. The 405 `--rm` option means that when the container exits, the container's layer is 406 removed. 407 408 ## Restart policies 409 410 Use Docker's `--restart` to specify a container's *restart policy*. A restart 411 policy controls whether the Docker daemon restarts a container after exit. 412 Docker supports the following restart policies: 413 414 <table> 415 <thead> 416 <tr> 417 <th>Policy</th> 418 <th>Result</th> 419 </tr> 420 </thead> 421 <tbody> 422 <tr> 423 <td><strong>no</strong></td> 424 <td> 425 Do not automatically restart the container when it exits. This is the 426 default. 427 </td> 428 </tr> 429 <tr> 430 <td> 431 <span style="white-space: nowrap"> 432 <strong>on-failure</strong>[:max-retries] 433 </span> 434 </td> 435 <td> 436 Restart only if the container exits with a non-zero exit status. 437 Optionally, limit the number of restart retries the Docker 438 daemon attempts. 439 </td> 440 </tr> 441 <tr> 442 <td><strong>always</strong></td> 443 <td> 444 Always restart the container regardless of the exit status. 445 When you specify always, the Docker daemon will try to restart 446 the container indefinitely. The container will also always start 447 on daemon startup, regardless of the current state of the container. 448 </td> 449 </tr> 450 <tr> 451 <td><strong>unless-stopped</strong></td> 452 <td> 453 Always restart the container regardless of the exit status, but 454 do not start it on daemon startup if the container has been put 455 to a stopped state before. 456 </td> 457 </tr> 458 </tbody> 459 </table> 460 461 $ docker run --restart=always redis 462 463 This will run the `redis` container with a restart policy of **always** 464 so that if the container exits, Docker will restart it. 465 466 More detailed information on restart policies can be found in the 467 [Restart Policies (--restart)](/reference/run/#restart-policies-restart) 468 section of the Docker run reference page. 469 470 ## Adding entries to a container hosts file 471 472 You can add other hosts into a container's `/etc/hosts` file by using one or 473 more `--add-host` flags. This example adds a static address for a host named 474 `docker`: 475 476 $ docker run --add-host=docker:10.180.0.1 --rm -it debian 477 $$ ping docker 478 PING docker (10.180.0.1): 48 data bytes 479 56 bytes from 10.180.0.1: icmp_seq=0 ttl=254 time=7.600 ms 480 56 bytes from 10.180.0.1: icmp_seq=1 ttl=254 time=30.705 ms 481 ^C--- docker ping statistics --- 482 2 packets transmitted, 2 packets received, 0% packet loss 483 round-trip min/avg/max/stddev = 7.600/19.152/30.705/11.553 ms 484 485 Sometimes you need to connect to the Docker host from within your 486 container. To enable this, pass the Docker host's IP address to 487 the container using the `--add-host` flag. To find the host's address, 488 use the `ip addr show` command. 489 490 The flags you pass to `ip addr show` depend on whether you are 491 using IPv4 or IPv6 networking in your containers. Use the following 492 flags for IPv4 address retrieval for a network device named `eth0`: 493 494 $ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1` 495 $ docker run --add-host=docker:${HOSTIP} --rm -it debian 496 497 For IPv6 use the `-6` flag instead of the `-4` flag. For other network 498 devices, replace `eth0` with the correct device name (for example `docker0` 499 for the bridge device). 500 501 ### Setting ulimits in a container 502 503 Since setting `ulimit` settings in a container requires extra privileges not 504 available in the default container, you can set these using the `--ulimit` flag. 505 `--ulimit` is specified with a soft and hard limit as such: 506 `<type>=<soft limit>[:<hard limit>]`, for example: 507 508 $ docker run --ulimit nofile=1024:1024 --rm debian ulimit -n 509 1024 510 511 > **Note:** 512 > If you do not provide a `hard limit`, the `soft limit` will be used 513 > for both values. If no `ulimits` are set, they will be inherited from 514 > the default `ulimits` set on the daemon. `as` option is disabled now. 515 > In other words, the following script is not supported: 516 > `$ docker run -it --ulimit as=1024 fedora /bin/bash` 517 518 The values are sent to the appropriate `syscall` as they are set. 519 Docker doesn't perform any byte conversion. Take this into account when setting the values. 520 521 #### For `nproc` usage: 522 523 Be careful setting `nproc` with the `ulimit` flag as `nproc` is designed by Linux to set the 524 maximum number of processes available to a user, not to a container. For example, start four 525 containers with `daemon` user: 526 527 528 docker run -d -u daemon --ulimit nproc=3 busybox top 529 docker run -d -u daemon --ulimit nproc=3 busybox top 530 docker run -d -u daemon --ulimit nproc=3 busybox top 531 docker run -d -u daemon --ulimit nproc=3 busybox top 532 533 The 4th container fails and reports "[8] System error: resource temporarily unavailable" error. 534 This fails because the caller set `nproc=3` resulting in the first three containers using up 535 the three processes quota set for the `daemon` user. 536 537 ### Stopping a container with a specific signal 538 539 The `--stop-signal` flag sets the system call signal that will be sent to the container to exit. 540 This signal can be a valid unsigned number that matches a position in the kernel's syscall table, for instance 9, 541 or a signal name in the format SIGNAME, for instance SIGKILL.