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