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