github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/docs/reference/run.md (about) 1 <!--[metadata]> 2 +++ 3 title = "Docker run reference" 4 description = "Configure containers at runtime" 5 keywords = ["docker, run, configure, runtime"] 6 [menu.main] 7 parent = "engine_ref" 8 weight=-80 9 +++ 10 <![end-metadata]--> 11 12 <!-- TODO (@thaJeztah) define more flexible table/td classes --> 13 <style> 14 table .no-wrap { 15 white-space: nowrap; 16 } 17 table code { 18 white-space: nowrap; 19 } 20 </style> 21 # Docker run reference 22 23 Docker runs processes in isolated containers. A container is a process 24 which runs on a host. The host may be local or remote. When an operator 25 executes `docker run`, the container process that runs is isolated in 26 that it has its own file system, its own networking, and its own 27 isolated process tree separate from the host. 28 29 This page details how to use the `docker run` command to define the 30 container's resources at runtime. 31 32 ## General form 33 34 The basic `docker run` command takes this form: 35 36 $ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] 37 38 The `docker run` command must specify an [*IMAGE*](glossary.md#image) 39 to derive the container from. An image developer can define image 40 defaults related to: 41 42 * detached or foreground running 43 * container identification 44 * network settings 45 * runtime constraints on CPU and memory 46 47 With the `docker run [OPTIONS]` an operator can add to or override the 48 image defaults set by a developer. And, additionally, operators can 49 override nearly all the defaults set by the Docker runtime itself. The 50 operator's ability to override image and Docker runtime defaults is why 51 [*run*](commandline/run.md) has more options than any 52 other `docker` command. 53 54 To learn how to interpret the types of `[OPTIONS]`, see [*Option 55 types*](commandline/cli.md#option-types). 56 57 > **Note**: Depending on your Docker system configuration, you may be 58 > required to preface the `docker run` command with `sudo`. To avoid 59 > having to use `sudo` with the `docker` command, your system 60 > administrator can create a Unix group called `docker` and add users to 61 > it. For more information about this configuration, refer to the Docker 62 > installation documentation for your operating system. 63 64 65 ## Operator exclusive options 66 67 Only the operator (the person executing `docker run`) can set the 68 following options. 69 70 - [Detached vs foreground](#detached-vs-foreground) 71 - [Detached (-d)](#detached-d) 72 - [Foreground](#foreground) 73 - [Container identification](#container-identification) 74 - [Name (--name)](#name-name) 75 - [PID equivalent](#pid-equivalent) 76 - [IPC settings (--ipc)](#ipc-settings-ipc) 77 - [Network settings](#network-settings) 78 - [Restart policies (--restart)](#restart-policies-restart) 79 - [Clean up (--rm)](#clean-up-rm) 80 - [Runtime constraints on resources](#runtime-constraints-on-resources) 81 - [Runtime privilege and Linux capabilities](#runtime-privilege-and-linux-capabilities) 82 83 ## Detached vs foreground 84 85 When starting a Docker container, you must first decide if you want to 86 run the container in the background in a "detached" mode or in the 87 default foreground mode: 88 89 -d=false: Detached mode: Run container in the background, print new container id 90 91 ### Detached (-d) 92 93 To start a container in detached mode, you use `-d=true` or just `-d` option. By 94 design, containers started in detached mode exit when the root process used to 95 run the container exits. A container in detached mode cannot be automatically 96 removed when it stops, this means you cannot use the `--rm` option with `-d` option. 97 98 Do not pass a `service x start` command to a detached container. For example, this 99 command attempts to start the `nginx` service. 100 101 $ docker run -d -p 80:80 my_image service nginx start 102 103 This succeeds in starting the `nginx` service inside the container. However, it 104 fails the detached container paradigm in that, the root process (`service nginx 105 start`) returns and the detached container stops as designed. As a result, the 106 `nginx` service is started but could not be used. Instead, to start a process 107 such as the `nginx` web server do the following: 108 109 $ docker run -d -p 80:80 my_image nginx -g 'daemon off;' 110 111 To do input/output with a detached container use network connections or shared 112 volumes. These are required because the container is no longer listening to the 113 command line where `docker run` was run. 114 115 To reattach to a detached container, use `docker` 116 [*attach*](commandline/attach.md) command. 117 118 ### Foreground 119 120 In foreground mode (the default when `-d` is not specified), `docker 121 run` can start the process in the container and attach the console to 122 the process's standard input, output, and standard error. It can even 123 pretend to be a TTY (this is what most command line executables expect) 124 and pass along signals. All of that is configurable: 125 126 -a=[] : Attach to `STDIN`, `STDOUT` and/or `STDERR` 127 -t : Allocate a pseudo-tty 128 --sig-proxy=true: Proxy all received signals to the process (non-TTY mode only) 129 -i : Keep STDIN open even if not attached 130 131 If you do not specify `-a` then Docker will [attach all standard 132 streams]( https://github.com/docker/docker/blob/75a7f4d90cde0295bcfb7213004abce8d4779b75/commands.go#L1797). 133 You can specify to which of the three standard streams (`STDIN`, `STDOUT`, 134 `STDERR`) you'd like to connect instead, as in: 135 136 $ docker run -a stdin -a stdout -i -t ubuntu /bin/bash 137 138 For interactive processes (like a shell), you must use `-i -t` together in 139 order to allocate a tty for the container process. `-i -t` is often written `-it` 140 as you'll see in later examples. Specifying `-t` is forbidden when the client 141 standard output is redirected or piped, such as in: 142 143 $ echo test | docker run -i busybox cat 144 145 >**Note**: A process running as PID 1 inside a container is treated 146 >specially by Linux: it ignores any signal with the default action. 147 >So, the process will not terminate on `SIGINT` or `SIGTERM` unless it is 148 >coded to do so. 149 150 ## Container identification 151 152 ### Name (--name) 153 154 The operator can identify a container in three ways: 155 156 | Identifier type | Example value | 157 | --------------------- | ------------------------------------------------------------------ | 158 | UUID long identifier | "f78375b1c487e03c9438c729345e54db9d20cfa2ac1fc3494b6eb60872e74778" | 159 | UUID short identifier | "f78375b1c487" | 160 | Name | "evil_ptolemy" | 161 162 The UUID identifiers come from the Docker daemon. If you do not assign a 163 container name with the `--name` option, then the daemon generates a random 164 string name for you. Defining a `name` can be a handy way to add meaning to a 165 container. If you specify a `name`, you can use it when referencing the 166 container within a Docker network. This works for both background and foreground 167 Docker containers. 168 169 > **Note**: Containers on the default bridge network must be linked to 170 > communicate by name. 171 172 ### PID equivalent 173 174 Finally, to help with automation, you can have Docker write the 175 container ID out to a file of your choosing. This is similar to how some 176 programs might write out their process ID to a file (you've seen them as 177 PID files): 178 179 --cidfile="": Write the container ID to the file 180 181 ### Image[:tag] 182 183 While not strictly a means of identifying a container, you can specify a version of an 184 image you'd like to run the container with by adding `image[:tag]` to the command. For 185 example, `docker run ubuntu:14.04`. 186 187 ### Image[@digest] 188 189 Images using the v2 or later image format have a content-addressable identifier 190 called a digest. As long as the input used to generate the image is unchanged, 191 the digest value is predictable and referenceable. 192 193 ## PID settings (--pid) 194 195 --pid="" : Set the PID (Process) Namespace mode for the container, 196 'host': use the host's PID namespace inside the container 197 198 By default, all containers have the PID namespace enabled. 199 200 PID namespace provides separation of processes. The PID Namespace removes the 201 view of the system processes, and allows process ids to be reused including 202 pid 1. 203 204 In certain cases you want your container to share the host's process namespace, 205 basically allowing processes within the container to see all of the processes 206 on the system. For example, you could build a container with debugging tools 207 like `strace` or `gdb`, but want to use these tools when debugging processes 208 within the container. 209 210 ### Example: run htop inside a container 211 212 Create this Dockerfile: 213 214 ``` 215 FROM alpine:latest 216 RUN apk add --update htop && rm -rf /var/cache/apk/* 217 CMD ["htop"] 218 ``` 219 220 Build the Dockerfile and tag the image as `myhtop`: 221 222 ```bash 223 $ docker build -t myhtop . 224 ``` 225 226 Use the following command to run `htop` inside a container: 227 228 ``` 229 $ docker run -it --rm --pid=host myhtop 230 ``` 231 232 ## UTS settings (--uts) 233 234 --uts="" : Set the UTS namespace mode for the container, 235 'host': use the host's UTS namespace inside the container 236 237 The UTS namespace is for setting the hostname and the domain that is visible 238 to running processes in that namespace. By default, all containers, including 239 those with `--net=host`, have their own UTS namespace. The `host` setting will 240 result in the container using the same UTS namespace as the host. Note that 241 `--hostname` is invalid in `host` UTS mode. 242 243 You may wish to share the UTS namespace with the host if you would like the 244 hostname of the container to change as the hostname of the host changes. A 245 more advanced use case would be changing the host's hostname from a container. 246 247 ## IPC settings (--ipc) 248 249 --ipc="" : Set the IPC mode for the container, 250 'container:<name|id>': reuses another container's IPC namespace 251 'host': use the host's IPC namespace inside the container 252 253 By default, all containers have the IPC namespace enabled. 254 255 IPC (POSIX/SysV IPC) namespace provides separation of named shared memory 256 segments, semaphores and message queues. 257 258 Shared memory segments are used to accelerate inter-process communication at 259 memory speed, rather than through pipes or through the network stack. Shared 260 memory is commonly used by databases and custom-built (typically C/OpenMPI, 261 C++/using boost libraries) high performance applications for scientific 262 computing and financial services industries. If these types of applications 263 are broken into multiple containers, you might need to share the IPC mechanisms 264 of the containers. 265 266 ## Network settings 267 268 --dns=[] : Set custom dns servers for the container 269 --net="bridge" : Connect a container to a network 270 'bridge': create a network stack on the default Docker bridge 271 'none': no networking 272 'container:<name|id>': reuse another container's network stack 273 'host': use the Docker host network stack 274 '<network-name>|<network-id>': connect to a user-defined network 275 --net-alias=[] : Add network-scoped alias for the container 276 --add-host="" : Add a line to /etc/hosts (host:IP) 277 --mac-address="" : Sets the container's Ethernet device's MAC address 278 --ip="" : Sets the container's Ethernet device's IPv4 address 279 --ip6="" : Sets the container's Ethernet device's IPv6 address 280 281 By default, all containers have networking enabled and they can make any 282 outgoing connections. The operator can completely disable networking 283 with `docker run --net none` which disables all incoming and outgoing 284 networking. In cases like this, you would perform I/O through files or 285 `STDIN` and `STDOUT` only. 286 287 Publishing ports and linking to other containers only works with the default (bridge). The linking feature is a legacy feature. You should always prefer using Docker network drivers over linking. 288 289 Your container will use the same DNS servers as the host by default, but 290 you can override this with `--dns`. 291 292 By default, the MAC address is generated using the IP address allocated to the 293 container. You can set the container's MAC address explicitly by providing a 294 MAC address via the `--mac-address` parameter (format:`12:34:56:78:9a:bc`). 295 296 Supported networks : 297 298 <table> 299 <thead> 300 <tr> 301 <th class="no-wrap">Network</th> 302 <th>Description</th> 303 </tr> 304 </thead> 305 <tbody> 306 <tr> 307 <td class="no-wrap"><strong>none</strong></td> 308 <td> 309 No networking in the container. 310 </td> 311 </tr> 312 <tr> 313 <td class="no-wrap"><strong>bridge</strong> (default)</td> 314 <td> 315 Connect the container to the bridge via veth interfaces. 316 </td> 317 </tr> 318 <tr> 319 <td class="no-wrap"><strong>host</strong></td> 320 <td> 321 Use the host's network stack inside the container. 322 </td> 323 </tr> 324 <tr> 325 <td class="no-wrap"><strong>container</strong>:<name|id></td> 326 <td> 327 Use the network stack of another container, specified via 328 its *name* or *id*. 329 </td> 330 </tr> 331 <tr> 332 <td class="no-wrap"><strong>NETWORK</strong></td> 333 <td> 334 Connects the container to a user created network (using `docker network create` command) 335 </td> 336 </tr> 337 </tbody> 338 </table> 339 340 #### Network: none 341 342 With the network is `none` a container will not have 343 access to any external routes. The container will still have a 344 `loopback` interface enabled in the container but it does not have any 345 routes to external traffic. 346 347 #### Network: bridge 348 349 With the network set to `bridge` a container will use docker's 350 default networking setup. A bridge is setup on the host, commonly named 351 `docker0`, and a pair of `veth` interfaces will be created for the 352 container. One side of the `veth` pair will remain on the host attached 353 to the bridge while the other side of the pair will be placed inside the 354 container's namespaces in addition to the `loopback` interface. An IP 355 address will be allocated for containers on the bridge's network and 356 traffic will be routed though this bridge to the container. 357 358 Containers can communicate via their IP addresses by default. To communicate by 359 name, they must be linked. 360 361 #### Network: host 362 363 With the network set to `host` a container will share the host's 364 network stack and all interfaces from the host will be available to the 365 container. The container's hostname will match the hostname on the host 366 system. Note that `--add-host` `--dns` `--dns-search` 367 `--dns-opt` and `--mac-address` are invalid in `host` netmode. Even in `host` 368 network mode a container has its own UTS namespace by default. As such 369 `--hostname` is allowed in `host` network mode and will only change the 370 hostname inside the container. 371 372 Compared to the default `bridge` mode, the `host` mode gives *significantly* 373 better networking performance since it uses the host's native networking stack 374 whereas the bridge has to go through one level of virtualization through the 375 docker daemon. It is recommended to run containers in this mode when their 376 networking performance is critical, for example, a production Load Balancer 377 or a High Performance Web Server. 378 379 > **Note**: `--net="host"` gives the container full access to local system 380 > services such as D-bus and is therefore considered insecure. 381 382 #### Network: container 383 384 With the network set to `container` a container will share the 385 network stack of another container. The other container's name must be 386 provided in the format of `--net container:<name|id>`. Note that `--add-host` 387 `--hostname` `--dns` `--dns-search` `--dns-opt` and `--mac-address` are 388 invalid in `container` netmode, and `--publish` `--publish-all` `--expose` are 389 also invalid in `container` netmode. 390 391 Example running a Redis container with Redis binding to `localhost` then 392 running the `redis-cli` command and connecting to the Redis server over the 393 `localhost` interface. 394 395 $ docker run -d --name redis example/redis --bind 127.0.0.1 396 $ # use the redis container's network stack to access localhost 397 $ docker run --rm -it --net container:redis example/redis-cli -h 127.0.0.1 398 399 #### User-defined network 400 401 You can create a network using a Docker network driver or an external network 402 driver plugin. You can connect multiple containers to the same network. Once 403 connected to a user-defined network, the containers can communicate easily using 404 only another container's IP address or name. 405 406 For `overlay` networks or custom plugins that support multi-host connectivity, 407 containers connected to the same multi-host network but launched from different 408 Engines can also communicate in this way. 409 410 The following example creates a network using the built-in `bridge` network 411 driver and running a container in the created network 412 413 ``` 414 $ docker network create -d bridge my-net 415 $ docker run --net=my-net -itd --name=container3 busybox 416 ``` 417 418 ### Managing /etc/hosts 419 420 Your container will have lines in `/etc/hosts` which define the hostname of the 421 container itself as well as `localhost` and a few other common things. The 422 `--add-host` flag can be used to add additional lines to `/etc/hosts`. 423 424 $ docker run -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts 425 172.17.0.22 09d03f76bf2c 426 fe00::0 ip6-localnet 427 ff00::0 ip6-mcastprefix 428 ff02::1 ip6-allnodes 429 ff02::2 ip6-allrouters 430 127.0.0.1 localhost 431 ::1 localhost ip6-localhost ip6-loopback 432 86.75.30.9 db-static 433 434 If a container is connected to the default bridge network and `linked` 435 with other containers, then the container's `/etc/hosts` file is updated 436 with the linked container's name. 437 438 If the container is connected to user-defined network, the container's 439 `/etc/hosts` file is updated with names of all other containers in that 440 user-defined network. 441 442 > **Note** Since Docker may live update the container’s `/etc/hosts` file, there 443 may be situations when processes inside the container can end up reading an 444 empty or incomplete `/etc/hosts` file. In most cases, retrying the read again 445 should fix the problem. 446 447 ## Restart policies (--restart) 448 449 Using the `--restart` flag on Docker run you can specify a restart policy for 450 how a container should or should not be restarted on exit. 451 452 When a restart policy is active on a container, it will be shown as either `Up` 453 or `Restarting` in [`docker ps`](commandline/ps.md). It can also be 454 useful to use [`docker events`](commandline/events.md) to see the 455 restart policy in effect. 456 457 Docker supports the following restart policies: 458 459 <table> 460 <thead> 461 <tr> 462 <th>Policy</th> 463 <th>Result</th> 464 </tr> 465 </thead> 466 <tbody> 467 <tr> 468 <td><strong>no</strong></td> 469 <td> 470 Do not automatically restart the container when it exits. This is the 471 default. 472 </td> 473 </tr> 474 <tr> 475 <td> 476 <span style="white-space: nowrap"> 477 <strong>on-failure</strong>[:max-retries] 478 </span> 479 </td> 480 <td> 481 Restart only if the container exits with a non-zero exit status. 482 Optionally, limit the number of restart retries the Docker 483 daemon attempts. 484 </td> 485 </tr> 486 <tr> 487 <td><strong>always</strong></td> 488 <td> 489 Always restart the container regardless of the exit status. 490 When you specify always, the Docker daemon will try to restart 491 the container indefinitely. The container will also always start 492 on daemon startup, regardless of the current state of the container. 493 </td> 494 </tr> 495 <tr> 496 <td><strong>unless-stopped</strong></td> 497 <td> 498 Always restart the container regardless of the exit status, but 499 do not start it on daemon startup if the container has been put 500 to a stopped state before. 501 </td> 502 </tr> 503 </tbody> 504 </table> 505 506 An ever increasing delay (double the previous delay, starting at 100 507 milliseconds) is added before each restart to prevent flooding the server. 508 This means the daemon will wait for 100 ms, then 200 ms, 400, 800, 1600, 509 and so on until either the `on-failure` limit is hit, or when you `docker stop` 510 or `docker rm -f` the container. 511 512 If a container is successfully restarted (the container is started and runs 513 for at least 10 seconds), the delay is reset to its default value of 100 ms. 514 515 You can specify the maximum amount of times Docker will try to restart the 516 container when using the **on-failure** policy. The default is that Docker 517 will try forever to restart the container. The number of (attempted) restarts 518 for a container can be obtained via [`docker inspect`](commandline/inspect.md). For example, to get the number of restarts 519 for container "my-container"; 520 521 $ docker inspect -f "{{ .RestartCount }}" my-container 522 # 2 523 524 Or, to get the last time the container was (re)started; 525 526 $ docker inspect -f "{{ .State.StartedAt }}" my-container 527 # 2015-03-04T23:47:07.691840179Z 528 529 530 Combining `--restart` (restart policy) with the `--rm` (clean up) flag results 531 in an error. On container restart, attached clients are disconnected. See the 532 examples on using the [`--rm` (clean up)](#clean-up-rm) flag later in this page. 533 534 ### Examples 535 536 $ docker run --restart=always redis 537 538 This will run the `redis` container with a restart policy of **always** 539 so that if the container exits, Docker will restart it. 540 541 $ docker run --restart=on-failure:10 redis 542 543 This will run the `redis` container with a restart policy of **on-failure** 544 and a maximum restart count of 10. If the `redis` container exits with a 545 non-zero exit status more than 10 times in a row Docker will abort trying to 546 restart the container. Providing a maximum restart limit is only valid for the 547 **on-failure** policy. 548 549 ## Exit Status 550 551 The exit code from `docker run` gives information about why the container 552 failed to run or why it exited. When `docker run` exits with a non-zero code, 553 the exit codes follow the `chroot` standard, see below: 554 555 **_125_** if the error is with Docker daemon **_itself_** 556 557 $ docker run --foo busybox; echo $? 558 # flag provided but not defined: --foo 559 See 'docker run --help'. 560 125 561 562 **_126_** if the **_contained command_** cannot be invoked 563 564 $ docker run busybox /etc; echo $? 565 # exec: "/etc": permission denied 566 docker: Error response from daemon: Contained command could not be invoked 567 126 568 569 **_127_** if the **_contained command_** cannot be found 570 571 $ docker run busybox foo; echo $? 572 # exec: "foo": executable file not found in $PATH 573 docker: Error response from daemon: Contained command not found or does not exist 574 127 575 576 **_Exit code_** of **_contained command_** otherwise 577 578 $ docker run busybox /bin/sh -c 'exit 3' 579 # 3 580 581 ## Clean up (--rm) 582 583 By default a container's file system persists even after the container 584 exits. This makes debugging a lot easier (since you can inspect the 585 final state) and you retain all your data by default. But if you are 586 running short-term **foreground** processes, these container file 587 systems can really pile up. If instead you'd like Docker to 588 **automatically clean up the container and remove the file system when 589 the container exits**, you can add the `--rm` flag: 590 591 --rm=false: Automatically remove the container when it exits (incompatible with -d) 592 593 > **Note**: When you set the `--rm` flag, Docker also removes the volumes 594 associated with the container when the container is removed. This is similar 595 to running `docker rm -v my-container`. Only volumes that are specified without a 596 name are removed. For example, with 597 `docker run --rm -v /foo -v awesome:/bar busybox top`, the volume for `/foo` will be removed, 598 but the volume for `/bar` will not. Volumes inheritted via `--volumes-from` will be removed 599 with the same logic -- if the original volume was specified with a name it will **not** be removed. 600 601 ## Security configuration 602 --security-opt="label=user:USER" : Set the label user for the container 603 --security-opt="label=role:ROLE" : Set the label role for the container 604 --security-opt="label=type:TYPE" : Set the label type for the container 605 --security-opt="label=level:LEVEL" : Set the label level for the container 606 --security-opt="label=disable" : Turn off label confinement for the container 607 --security-opt="apparmor=PROFILE" : Set the apparmor profile to be applied 608 to the container 609 --security-opt="no-new-privileges" : Disable container processes from gaining 610 new privileges 611 --security-opt="seccomp=unconfined": Turn off seccomp confinement for the container 612 --security-opt="seccomp=profile.json: White listed syscalls seccomp Json file to be used as a seccomp filter 613 614 615 You can override the default labeling scheme for each container by specifying 616 the `--security-opt` flag. For example, you can specify the MCS/MLS level, a 617 requirement for MLS systems. Specifying the level in the following command 618 allows you to share the same content between containers. 619 620 $ docker run --security-opt label=level:s0:c100,c200 -it fedora bash 621 622 An MLS example might be: 623 624 $ docker run --security-opt label=level:TopSecret -it rhel7 bash 625 626 To disable the security labeling for this container versus running with the 627 `--permissive` flag, use the following command: 628 629 $ docker run --security-opt label=disable -it fedora bash 630 631 If you want a tighter security policy on the processes within a container, 632 you can specify an alternate type for the container. You could run a container 633 that is only allowed to listen on Apache ports by executing the following 634 command: 635 636 $ docker run --security-opt label=type:svirt_apache_t -it centos bash 637 638 > **Note**: You would have to write policy defining a `svirt_apache_t` type. 639 640 If you want to prevent your container processes from gaining additional 641 privileges, you can execute the following command: 642 643 $ docker run --security-opt no-new-privileges -it centos bash 644 645 For more details, see [kernel documentation](https://www.kernel.org/doc/Documentation/prctl/no_new_privs.txt). 646 647 ## Specifying custom cgroups 648 649 Using the `--cgroup-parent` flag, you can pass a specific cgroup to run a 650 container in. This allows you to create and manage cgroups on their own. You can 651 define custom resources for those cgroups and put containers under a common 652 parent group. 653 654 ## Runtime constraints on resources 655 656 The operator can also adjust the performance parameters of the 657 container: 658 659 | Option | Description | 660 | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | 661 | `-m`, `--memory=""` | Memory limit (format: `<number>[<unit>]`). Number is a positive integer. Unit can be one of `b`, `k`, `m`, or `g`. Minimum is 4M. | 662 | `--memory-swap=""` | Total memory limit (memory + swap, format: `<number>[<unit>]`). Number is a positive integer. Unit can be one of `b`, `k`, `m`, or `g`. | 663 | `--memory-reservation=""` | Memory soft limit (format: `<number>[<unit>]`). Number is a positive integer. Unit can be one of `b`, `k`, `m`, or `g`. | 664 | `--kernel-memory=""` | Kernel memory limit (format: `<number>[<unit>]`). Number is a positive integer. Unit can be one of `b`, `k`, `m`, or `g`. Minimum is 4M. | 665 | `-c`, `--cpu-shares=0` | CPU shares (relative weight) | 666 | `--cpu-period=0` | Limit the CPU CFS (Completely Fair Scheduler) period | 667 | `--cpuset-cpus=""` | CPUs in which to allow execution (0-3, 0,1) | 668 | `--cpuset-mems=""` | Memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems. | 669 | `--cpu-quota=0` | Limit the CPU CFS (Completely Fair Scheduler) quota | 670 | `--blkio-weight=0` | Block IO weight (relative weight) accepts a weight value between 10 and 1000. | 671 | `--blkio-weight-device=""` | Block IO weight (relative device weight, format: `DEVICE_NAME:WEIGHT`) | 672 | `--device-read-bps=""` | Limit read rate from a device (format: `<device-path>:<number>[<unit>]`). Number is a positive integer. Unit can be one of `kb`, `mb`, or `gb`. | 673 | `--device-write-bps=""` | Limit write rate to a device (format: `<device-path>:<number>[<unit>]`). Number is a positive integer. Unit can be one of `kb`, `mb`, or `gb`. | 674 | `--device-read-iops="" ` | Limit read rate (IO per second) from a device (format: `<device-path>:<number>`). Number is a positive integer. | 675 | `--device-write-iops="" ` | Limit write rate (IO per second) to a device (format: `<device-path>:<number>`). Number is a positive integer. | 676 | `--oom-kill-disable=false` | Whether to disable OOM Killer for the container or not. | 677 | `--memory-swappiness=""` | Tune a container's memory swappiness behavior. Accepts an integer between 0 and 100. | 678 | `--shm-size=""` | Size of `/dev/shm`. The format is `<number><unit>`. `number` must be greater than `0`. Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). If you omit the unit, the system uses bytes. If you omit the size entirely, the system uses `64m`. | 679 680 ### User memory constraints 681 682 We have four ways to set user memory usage: 683 684 <table> 685 <thead> 686 <tr> 687 <th>Option</th> 688 <th>Result</th> 689 </tr> 690 </thead> 691 <tbody> 692 <tr> 693 <td class="no-wrap"> 694 <strong>memory=inf, memory-swap=inf</strong> (default) 695 </td> 696 <td> 697 There is no memory limit for the container. The container can use 698 as much memory as needed. 699 </td> 700 </tr> 701 <tr> 702 <td class="no-wrap"><strong>memory=L<inf, memory-swap=inf</strong></td> 703 <td> 704 (specify memory and set memory-swap as <code>-1</code>) The container is 705 not allowed to use more than L bytes of memory, but can use as much swap 706 as is needed (if the host supports swap memory). 707 </td> 708 </tr> 709 <tr> 710 <td class="no-wrap"><strong>memory=L<inf, memory-swap=2*L</strong></td> 711 <td> 712 (specify memory without memory-swap) The container is not allowed to 713 use more than L bytes of memory, swap *plus* memory usage is double 714 of that. 715 </td> 716 </tr> 717 <tr> 718 <td class="no-wrap"> 719 <strong>memory=L<inf, memory-swap=S<inf, L<=S</strong> 720 </td> 721 <td> 722 (specify both memory and memory-swap) The container is not allowed to 723 use more than L bytes of memory, swap *plus* memory usage is limited 724 by S. 725 </td> 726 </tr> 727 </tbody> 728 </table> 729 730 Examples: 731 732 $ docker run -it ubuntu:14.04 /bin/bash 733 734 We set nothing about memory, this means the processes in the container can use 735 as much memory and swap memory as they need. 736 737 $ docker run -it -m 300M --memory-swap -1 ubuntu:14.04 /bin/bash 738 739 We set memory limit and disabled swap memory limit, this means the processes in 740 the container can use 300M memory and as much swap memory as they need (if the 741 host supports swap memory). 742 743 $ docker run -it -m 300M ubuntu:14.04 /bin/bash 744 745 We set memory limit only, this means the processes in the container can use 746 300M memory and 300M swap memory, by default, the total virtual memory size 747 (--memory-swap) will be set as double of memory, in this case, memory + swap 748 would be 2*300M, so processes can use 300M swap memory as well. 749 750 $ docker run -it -m 300M --memory-swap 1G ubuntu:14.04 /bin/bash 751 752 We set both memory and swap memory, so the processes in the container can use 753 300M memory and 700M swap memory. 754 755 Memory reservation is a kind of memory soft limit that allows for greater 756 sharing of memory. Under normal circumstances, containers can use as much of 757 the memory as needed and are constrained only by the hard limits set with the 758 `-m`/`--memory` option. When memory reservation is set, Docker detects memory 759 contention or low memory and forces containers to restrict their consumption to 760 a reservation limit. 761 762 Always set the memory reservation value below the hard limit, otherwise the hard 763 limit takes precedence. A reservation of 0 is the same as setting no 764 reservation. By default (without reservation set), memory reservation is the 765 same as the hard memory limit. 766 767 Memory reservation is a soft-limit feature and does not guarantee the limit 768 won't be exceeded. Instead, the feature attempts to ensure that, when memory is 769 heavily contended for, memory is allocated based on the reservation hints/setup. 770 771 The following example limits the memory (`-m`) to 500M and sets the memory 772 reservation to 200M. 773 774 ```bash 775 $ docker run -it -m 500M --memory-reservation 200M ubuntu:14.04 /bin/bash 776 ``` 777 778 Under this configuration, when the container consumes memory more than 200M and 779 less than 500M, the next system memory reclaim attempts to shrink container 780 memory below 200M. 781 782 The following example set memory reservation to 1G without a hard memory limit. 783 784 ```bash 785 $ docker run -it --memory-reservation 1G ubuntu:14.04 /bin/bash 786 ``` 787 788 The container can use as much memory as it needs. The memory reservation setting 789 ensures the container doesn't consume too much memory for long time, because 790 every memory reclaim shrinks the container's consumption to the reservation. 791 792 By default, kernel kills processes in a container if an out-of-memory (OOM) 793 error occurs. To change this behaviour, use the `--oom-kill-disable` option. 794 Only disable the OOM killer on containers where you have also set the 795 `-m/--memory` option. If the `-m` flag is not set, this can result in the host 796 running out of memory and require killing the host's system processes to free 797 memory. 798 799 The following example limits the memory to 100M and disables the OOM killer for 800 this container: 801 802 $ docker run -it -m 100M --oom-kill-disable ubuntu:14.04 /bin/bash 803 804 The following example, illustrates a dangerous way to use the flag: 805 806 $ docker run -it --oom-kill-disable ubuntu:14.04 /bin/bash 807 808 The container has unlimited memory which can cause the host to run out memory 809 and require killing system processes to free memory. 810 811 ### Kernel memory constraints 812 813 Kernel memory is fundamentally different than user memory as kernel memory can't 814 be swapped out. The inability to swap makes it possible for the container to 815 block system services by consuming too much kernel memory. Kernel memory includes: 816 817 - stack pages 818 - slab pages 819 - sockets memory pressure 820 - tcp memory pressure 821 822 You can setup kernel memory limit to constrain these kinds of memory. For example, 823 every process consumes some stack pages. By limiting kernel memory, you can 824 prevent new processes from being created when the kernel memory usage is too high. 825 826 Kernel memory is never completely independent of user memory. Instead, you limit 827 kernel memory in the context of the user memory limit. Assume "U" is the user memory 828 limit and "K" the kernel limit. There are three possible ways to set limits: 829 830 <table> 831 <thead> 832 <tr> 833 <th>Option</th> 834 <th>Result</th> 835 </tr> 836 </thead> 837 <tbody> 838 <tr> 839 <td class="no-wrap"><strong>U != 0, K = inf</strong> (default)</td> 840 <td> 841 This is the standard memory limitation mechanism already present before using 842 kernel memory. Kernel memory is completely ignored. 843 </td> 844 </tr> 845 <tr> 846 <td class="no-wrap"><strong>U != 0, K < U</strong></td> 847 <td> 848 Kernel memory is a subset of the user memory. This setup is useful in 849 deployments where the total amount of memory per-cgroup is overcommitted. 850 Overcommitting kernel memory limits is definitely not recommended, since the 851 box can still run out of non-reclaimable memory. 852 In this case, the you can configure K so that the sum of all groups is 853 never greater than the total memory. Then, freely set U at the expense of 854 the system's service quality. 855 </td> 856 </tr> 857 <tr> 858 <td class="no-wrap"><strong>U != 0, K > U</strong></td> 859 <td> 860 Since kernel memory charges are also fed to the user counter and reclamation 861 is triggered for the container for both kinds of memory. This configuration 862 gives the admin a unified view of memory. It is also useful for people 863 who just want to track kernel memory usage. 864 </td> 865 </tr> 866 </tbody> 867 </table> 868 869 Examples: 870 871 $ docker run -it -m 500M --kernel-memory 50M ubuntu:14.04 /bin/bash 872 873 We set memory and kernel memory, so the processes in the container can use 874 500M memory in total, in this 500M memory, it can be 50M kernel memory tops. 875 876 $ docker run -it --kernel-memory 50M ubuntu:14.04 /bin/bash 877 878 We set kernel memory without **-m**, so the processes in the container can 879 use as much memory as they want, but they can only use 50M kernel memory. 880 881 ### Swappiness constraint 882 883 By default, a container's kernel can swap out a percentage of anonymous pages. 884 To set this percentage for a container, specify a `--memory-swappiness` value 885 between 0 and 100. A value of 0 turns off anonymous page swapping. A value of 886 100 sets all anonymous pages as swappable. By default, if you are not using 887 `--memory-swappiness`, memory swappiness value will be inherited from the parent. 888 889 For example, you can set: 890 891 $ docker run -it --memory-swappiness=0 ubuntu:14.04 /bin/bash 892 893 Setting the `--memory-swappiness` option is helpful when you want to retain the 894 container's working set and to avoid swapping performance penalties. 895 896 ### CPU share constraint 897 898 By default, all containers get the same proportion of CPU cycles. This proportion 899 can be modified by changing the container's CPU share weighting relative 900 to the weighting of all other running containers. 901 902 To modify the proportion from the default of 1024, use the `-c` or `--cpu-shares` 903 flag to set the weighting to 2 or higher. If 0 is set, the system will ignore the 904 value and use the default of 1024. 905 906 The proportion will only apply when CPU-intensive processes are running. 907 When tasks in one container are idle, other containers can use the 908 left-over CPU time. The actual amount of CPU time will vary depending on 909 the number of containers running on the system. 910 911 For example, consider three containers, one has a cpu-share of 1024 and 912 two others have a cpu-share setting of 512. When processes in all three 913 containers attempt to use 100% of CPU, the first container would receive 914 50% of the total CPU time. If you add a fourth container with a cpu-share 915 of 1024, the first container only gets 33% of the CPU. The remaining containers 916 receive 16.5%, 16.5% and 33% of the CPU. 917 918 On a multi-core system, the shares of CPU time are distributed over all CPU 919 cores. Even if a container is limited to less than 100% of CPU time, it can 920 use 100% of each individual CPU core. 921 922 For example, consider a system with more than three cores. If you start one 923 container `{C0}` with `-c=512` running one process, and another container 924 `{C1}` with `-c=1024` running two processes, this can result in the following 925 division of CPU shares: 926 927 PID container CPU CPU share 928 100 {C0} 0 100% of CPU0 929 101 {C1} 1 100% of CPU1 930 102 {C1} 2 100% of CPU2 931 932 ### CPU period constraint 933 934 The default CPU CFS (Completely Fair Scheduler) period is 100ms. We can use 935 `--cpu-period` to set the period of CPUs to limit the container's CPU usage. 936 And usually `--cpu-period` should work with `--cpu-quota`. 937 938 Examples: 939 940 $ docker run -it --cpu-period=50000 --cpu-quota=25000 ubuntu:14.04 /bin/bash 941 942 If there is 1 CPU, this means the container can get 50% CPU worth of run-time every 50ms. 943 944 For more information, see the [CFS documentation on bandwidth limiting](https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt). 945 946 ### Cpuset constraint 947 948 We can set cpus in which to allow execution for containers. 949 950 Examples: 951 952 $ docker run -it --cpuset-cpus="1,3" ubuntu:14.04 /bin/bash 953 954 This means processes in container can be executed on cpu 1 and cpu 3. 955 956 $ docker run -it --cpuset-cpus="0-2" ubuntu:14.04 /bin/bash 957 958 This means processes in container can be executed on cpu 0, cpu 1 and cpu 2. 959 960 We can set mems in which to allow execution for containers. Only effective 961 on NUMA systems. 962 963 Examples: 964 965 $ docker run -it --cpuset-mems="1,3" ubuntu:14.04 /bin/bash 966 967 This example restricts the processes in the container to only use memory from 968 memory nodes 1 and 3. 969 970 $ docker run -it --cpuset-mems="0-2" ubuntu:14.04 /bin/bash 971 972 This example restricts the processes in the container to only use memory from 973 memory nodes 0, 1 and 2. 974 975 ### CPU quota constraint 976 977 The `--cpu-quota` flag limits the container's CPU usage. The default 0 value 978 allows the container to take 100% of a CPU resource (1 CPU). The CFS (Completely Fair 979 Scheduler) handles resource allocation for executing processes and is default 980 Linux Scheduler used by the kernel. Set this value to 50000 to limit the container 981 to 50% of a CPU resource. For multiple CPUs, adjust the `--cpu-quota` as necessary. 982 For more information, see the [CFS documentation on bandwidth limiting](https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt). 983 984 ### Block IO bandwidth (Blkio) constraint 985 986 By default, all containers get the same proportion of block IO bandwidth 987 (blkio). This proportion is 500. To modify this proportion, change the 988 container's blkio weight relative to the weighting of all other running 989 containers using the `--blkio-weight` flag. 990 991 > **Note:** The blkio weight setting is only available for direct IO. Buffered IO 992 > is not currently supported. 993 994 The `--blkio-weight` flag can set the weighting to a value between 10 to 1000. 995 For example, the commands below create two containers with different blkio 996 weight: 997 998 $ docker run -it --name c1 --blkio-weight 300 ubuntu:14.04 /bin/bash 999 $ docker run -it --name c2 --blkio-weight 600 ubuntu:14.04 /bin/bash 1000 1001 If you do block IO in the two containers at the same time, by, for example: 1002 1003 $ time dd if=/mnt/zerofile of=test.out bs=1M count=1024 oflag=direct 1004 1005 You'll find that the proportion of time is the same as the proportion of blkio 1006 weights of the two containers. 1007 1008 The `--blkio-weight-device="DEVICE_NAME:WEIGHT"` flag sets a specific device weight. 1009 The `DEVICE_NAME:WEIGHT` is a string containing a colon-separated device name and weight. 1010 For example, to set `/dev/sda` device weight to `200`: 1011 1012 $ docker run -it \ 1013 --blkio-weight-device "/dev/sda:200" \ 1014 ubuntu 1015 1016 If you specify both the `--blkio-weight` and `--blkio-weight-device`, Docker 1017 uses the `--blkio-weight` as the default weight and uses `--blkio-weight-device` 1018 to override this default with a new value on a specific device. 1019 The following example uses a default weight of `300` and overrides this default 1020 on `/dev/sda` setting that weight to `200`: 1021 1022 $ docker run -it \ 1023 --blkio-weight 300 \ 1024 --blkio-weight-device "/dev/sda:200" \ 1025 ubuntu 1026 1027 The `--device-read-bps` flag limits the read rate (bytes per second) from a device. 1028 For example, this command creates a container and limits the read rate to `1mb` 1029 per second from `/dev/sda`: 1030 1031 $ docker run -it --device-read-bps /dev/sda:1mb ubuntu 1032 1033 The `--device-write-bps` flag limits the write rate (bytes per second)to a device. 1034 For example, this command creates a container and limits the write rate to `1mb` 1035 per second for `/dev/sda`: 1036 1037 $ docker run -it --device-write-bps /dev/sda:1mb ubuntu 1038 1039 Both flags take limits in the `<device-path>:<limit>[unit]` format. Both read 1040 and write rates must be a positive integer. You can specify the rate in `kb` 1041 (kilobytes), `mb` (megabytes), or `gb` (gigabytes). 1042 1043 The `--device-read-iops` flag limits read rate (IO per second) from a device. 1044 For example, this command creates a container and limits the read rate to 1045 `1000` IO per second from `/dev/sda`: 1046 1047 $ docker run -ti --device-read-iops /dev/sda:1000 ubuntu 1048 1049 The `--device-write-iops` flag limits write rate (IO per second) to a device. 1050 For example, this command creates a container and limits the write rate to 1051 `1000` IO per second to `/dev/sda`: 1052 1053 $ docker run -ti --device-write-iops /dev/sda:1000 ubuntu 1054 1055 Both flags take limits in the `<device-path>:<limit>` format. Both read and 1056 write rates must be a positive integer. 1057 1058 ## Additional groups 1059 --group-add: Add additional groups to run as 1060 1061 By default, the docker container process runs with the supplementary groups looked 1062 up for the specified user. If one wants to add more to that list of groups, then 1063 one can use this flag: 1064 1065 $ docker run --rm --group-add audio --group-add nogroup --group-add 777 busybox id 1066 uid=0(root) gid=0(root) groups=10(wheel),29(audio),99(nogroup),777 1067 1068 ## Runtime privilege and Linux capabilities 1069 1070 --cap-add: Add Linux capabilities 1071 --cap-drop: Drop Linux capabilities 1072 --privileged=false: Give extended privileges to this container 1073 --device=[]: Allows you to run devices inside the container without the --privileged flag. 1074 1075 > **Note:** 1076 > With Docker 1.10 and greater, the default seccomp profile will also block 1077 > syscalls, regardless of `--cap-add` passed to the container. We recommend in 1078 > these cases to create your own custom seccomp profile based off our 1079 > [default](https://github.com/docker/docker/blob/master/profiles/seccomp/default.json). 1080 > Or if you don't want to run with the default seccomp profile, you can pass 1081 > `--security-opt=seccomp=unconfined` on run. 1082 1083 By default, Docker containers are "unprivileged" and cannot, for 1084 example, run a Docker daemon inside a Docker container. This is because 1085 by default a container is not allowed to access any devices, but a 1086 "privileged" container is given access to all devices (see 1087 the documentation on [cgroups devices](https://www.kernel.org/doc/Documentation/cgroups/devices.txt)). 1088 1089 When the operator executes `docker run --privileged`, Docker will enable 1090 to access to all devices on the host as well as set some configuration 1091 in AppArmor or SELinux to allow the container nearly all the same access to the 1092 host as processes running outside containers on the host. Additional 1093 information about running with `--privileged` is available on the 1094 [Docker Blog](http://blog.docker.com/2013/09/docker-can-now-run-within-docker/). 1095 1096 If you want to limit access to a specific device or devices you can use 1097 the `--device` flag. It allows you to specify one or more devices that 1098 will be accessible within the container. 1099 1100 $ docker run --device=/dev/snd:/dev/snd ... 1101 1102 By default, the container will be able to `read`, `write`, and `mknod` these devices. 1103 This can be overridden using a third `:rwm` set of options to each `--device` flag: 1104 1105 $ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc 1106 1107 Command (m for help): q 1108 $ docker run --device=/dev/sda:/dev/xvdc:r --rm -it ubuntu fdisk /dev/xvdc 1109 You will not be able to write the partition table. 1110 1111 Command (m for help): q 1112 1113 $ docker run --device=/dev/sda:/dev/xvdc:w --rm -it ubuntu fdisk /dev/xvdc 1114 crash.... 1115 1116 $ docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc 1117 fdisk: unable to open /dev/xvdc: Operation not permitted 1118 1119 In addition to `--privileged`, the operator can have fine grain control over the 1120 capabilities using `--cap-add` and `--cap-drop`. By default, Docker has a default 1121 list of capabilities that are kept. The following table lists the Linux capability options which can be added or dropped. 1122 1123 | Capability Key | Capability Description | 1124 | ---------------- | ----------------------------------------------------------------------------------------------------------------------------- | 1125 | SETPCAP | Modify process capabilities. | 1126 | SYS_MODULE | Load and unload kernel modules. | 1127 | SYS_RAWIO | Perform I/O port operations (iopl(2) and ioperm(2)). | 1128 | SYS_PACCT | Use acct(2), switch process accounting on or off. | 1129 | SYS_ADMIN | Perform a range of system administration operations. | 1130 | SYS_NICE | Raise process nice value (nice(2), setpriority(2)) and change the nice value for arbitrary processes. | 1131 | SYS_RESOURCE | Override resource Limits. | 1132 | SYS_TIME | Set system clock (settimeofday(2), stime(2), adjtimex(2)); set real-time (hardware) clock. | 1133 | SYS_TTY_CONFIG | Use vhangup(2); employ various privileged ioctl(2) operations on virtual terminals. | 1134 | MKNOD | Create special files using mknod(2). | 1135 | AUDIT_WRITE | Write records to kernel auditing log. | 1136 | AUDIT_CONTROL | Enable and disable kernel auditing; change auditing filter rules; retrieve auditing status and filtering rules. | 1137 | MAC_OVERRIDE | Allow MAC configuration or state changes. Implemented for the Smack LSM. | 1138 | MAC_ADMIN | Override Mandatory Access Control (MAC). Implemented for the Smack Linux Security Module (LSM). | 1139 | NET_ADMIN | Perform various network-related operations. | 1140 | SYSLOG | Perform privileged syslog(2) operations. | 1141 | CHOWN | Make arbitrary changes to file UIDs and GIDs (see chown(2)). | 1142 | NET_RAW | Use RAW and PACKET sockets. | 1143 | DAC_OVERRIDE | Bypass file read, write, and execute permission checks. | 1144 | FOWNER | Bypass permission checks on operations that normally require the file system UID of the process to match the UID of the file. | 1145 | DAC_READ_SEARCH | Bypass file read permission checks and directory read and execute permission checks. | 1146 | FSETID | Don't clear set-user-ID and set-group-ID permission bits when a file is modified. | 1147 | KILL | Bypass permission checks for sending signals. | 1148 | SETGID | Make arbitrary manipulations of process GIDs and supplementary GID list. | 1149 | SETUID | Make arbitrary manipulations of process UIDs. | 1150 | LINUX_IMMUTABLE | Set the FS_APPEND_FL and FS_IMMUTABLE_FL i-node flags. | 1151 | NET_BIND_SERVICE | Bind a socket to internet domain privileged ports (port numbers less than 1024). | 1152 | NET_BROADCAST | Make socket broadcasts, and listen to multicasts. | 1153 | IPC_LOCK | Lock memory (mlock(2), mlockall(2), mmap(2), shmctl(2)). | 1154 | IPC_OWNER | Bypass permission checks for operations on System V IPC objects. | 1155 | SYS_CHROOT | Use chroot(2), change root directory. | 1156 | SYS_PTRACE | Trace arbitrary processes using ptrace(2). | 1157 | SYS_BOOT | Use reboot(2) and kexec_load(2), reboot and load a new kernel for later execution. | 1158 | LEASE | Establish leases on arbitrary files (see fcntl(2)). | 1159 | SETFCAP | Set file capabilities. | 1160 | WAKE_ALARM | Trigger something that will wake up the system. | 1161 | BLOCK_SUSPEND | Employ features that can block system suspend. 1162 1163 Further reference information is available on the [capabilities(7) - Linux man page](http://linux.die.net/man/7/capabilities) 1164 1165 Both flags support the value `ALL`, so if the 1166 operator wants to have all capabilities but `MKNOD` they could use: 1167 1168 $ docker run --cap-add=ALL --cap-drop=MKNOD ... 1169 1170 For interacting with the network stack, instead of using `--privileged` they 1171 should use `--cap-add=NET_ADMIN` to modify the network interfaces. 1172 1173 $ docker run -it --rm ubuntu:14.04 ip link add dummy0 type dummy 1174 RTNETLINK answers: Operation not permitted 1175 $ docker run -it --rm --cap-add=NET_ADMIN ubuntu:14.04 ip link add dummy0 type dummy 1176 1177 To mount a FUSE based filesystem, you need to combine both `--cap-add` and 1178 `--device`: 1179 1180 $ docker run --rm -it --cap-add SYS_ADMIN sshfs sshfs sven@10.10.10.20:/home/sven /mnt 1181 fuse: failed to open /dev/fuse: Operation not permitted 1182 $ docker run --rm -it --device /dev/fuse sshfs sshfs sven@10.10.10.20:/home/sven /mnt 1183 fusermount: mount failed: Operation not permitted 1184 $ docker run --rm -it --cap-add SYS_ADMIN --device /dev/fuse sshfs 1185 # sshfs sven@10.10.10.20:/home/sven /mnt 1186 The authenticity of host '10.10.10.20 (10.10.10.20)' can't be established. 1187 ECDSA key fingerprint is 25:34:85:75:25:b0:17:46:05:19:04:93:b5:dd:5f:c6. 1188 Are you sure you want to continue connecting (yes/no)? yes 1189 sven@10.10.10.20's password: 1190 root@30aa0cfaf1b5:/# ls -la /mnt/src/docker 1191 total 1516 1192 drwxrwxr-x 1 1000 1000 4096 Dec 4 06:08 . 1193 drwxrwxr-x 1 1000 1000 4096 Dec 4 11:46 .. 1194 -rw-rw-r-- 1 1000 1000 16 Oct 8 00:09 .dockerignore 1195 -rwxrwxr-x 1 1000 1000 464 Oct 8 00:09 .drone.yml 1196 drwxrwxr-x 1 1000 1000 4096 Dec 4 06:11 .git 1197 -rw-rw-r-- 1 1000 1000 461 Dec 4 06:08 .gitignore 1198 .... 1199 1200 1201 ## Logging drivers (--log-driver) 1202 1203 The container can have a different logging driver than the Docker daemon. Use 1204 the `--log-driver=VALUE` with the `docker run` command to configure the 1205 container's logging driver. The following options are supported: 1206 1207 | Driver | Description | 1208 | ----------- | ----------------------------------------------------------------------------------------------------------------------------- | 1209 | `none` | Disables any logging for the container. `docker logs` won't be available with this driver. | 1210 | `json-file` | Default logging driver for Docker. Writes JSON messages to file. No logging options are supported for this driver. | 1211 | `syslog` | Syslog logging driver for Docker. Writes log messages to syslog. | 1212 | `journald` | Journald logging driver for Docker. Writes log messages to `journald`. | 1213 | `gelf` | Graylog Extended Log Format (GELF) logging driver for Docker. Writes log messages to a GELF endpoint likeGraylog or Logstash. | 1214 | `fluentd` | Fluentd logging driver for Docker. Writes log messages to `fluentd` (forward input). | 1215 | `awslogs` | Amazon CloudWatch Logs logging driver for Docker. Writes log messages to Amazon CloudWatch Logs | 1216 | `splunk` | Splunk logging driver for Docker. Writes log messages to `splunk` using Event Http Collector. | 1217 1218 The `docker logs` command is available only for the `json-file` and `journald` 1219 logging drivers. For detailed information on working with logging drivers, see 1220 [Configure a logging driver](../admin/logging/overview.md). 1221 1222 1223 ## Overriding Dockerfile image defaults 1224 1225 When a developer builds an image from a [*Dockerfile*](builder.md) 1226 or when she commits it, the developer can set a number of default parameters 1227 that take effect when the image starts up as a container. 1228 1229 Four of the Dockerfile commands cannot be overridden at runtime: `FROM`, 1230 `MAINTAINER`, `RUN`, and `ADD`. Everything else has a corresponding override 1231 in `docker run`. We'll go through what the developer might have set in each 1232 Dockerfile instruction and how the operator can override that setting. 1233 1234 - [CMD (Default Command or Options)](#cmd-default-command-or-options) 1235 - [ENTRYPOINT (Default Command to Execute at Runtime)]( 1236 #entrypoint-default-command-to-execute-at-runtime) 1237 - [EXPOSE (Incoming Ports)](#expose-incoming-ports) 1238 - [ENV (Environment Variables)](#env-environment-variables) 1239 - [VOLUME (Shared Filesystems)](#volume-shared-filesystems) 1240 - [USER](#user) 1241 - [WORKDIR](#workdir) 1242 1243 ### CMD (default command or options) 1244 1245 Recall the optional `COMMAND` in the Docker 1246 commandline: 1247 1248 $ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] 1249 1250 This command is optional because the person who created the `IMAGE` may 1251 have already provided a default `COMMAND` using the Dockerfile `CMD` 1252 instruction. As the operator (the person running a container from the 1253 image), you can override that `CMD` instruction just by specifying a new 1254 `COMMAND`. 1255 1256 If the image also specifies an `ENTRYPOINT` then the `CMD` or `COMMAND` 1257 get appended as arguments to the `ENTRYPOINT`. 1258 1259 ### ENTRYPOINT (default command to execute at runtime) 1260 1261 --entrypoint="": Overwrite the default entrypoint set by the image 1262 1263 The `ENTRYPOINT` of an image is similar to a `COMMAND` because it 1264 specifies what executable to run when the container starts, but it is 1265 (purposely) more difficult to override. The `ENTRYPOINT` gives a 1266 container its default nature or behavior, so that when you set an 1267 `ENTRYPOINT` you can run the container *as if it were that binary*, 1268 complete with default options, and you can pass in more options via the 1269 `COMMAND`. But, sometimes an operator may want to run something else 1270 inside the container, so you can override the default `ENTRYPOINT` at 1271 runtime by using a string to specify the new `ENTRYPOINT`. Here is an 1272 example of how to run a shell in a container that has been set up to 1273 automatically run something else (like `/usr/bin/redis-server`): 1274 1275 $ docker run -it --entrypoint /bin/bash example/redis 1276 1277 or two examples of how to pass more parameters to that ENTRYPOINT: 1278 1279 $ docker run -it --entrypoint /bin/bash example/redis -c ls -l 1280 $ docker run -it --entrypoint /usr/bin/redis-cli example/redis --help 1281 1282 ### EXPOSE (incoming ports) 1283 1284 The following `run` command options work with container networking: 1285 1286 --expose=[]: Expose a port or a range of ports inside the container. 1287 These are additional to those exposed by the `EXPOSE` instruction 1288 -P : Publish all exposed ports to the host interfaces 1289 -p=[] : Publish a container᾿s port or a range of ports to the host 1290 format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort 1291 Both hostPort and containerPort can be specified as a 1292 range of ports. When specifying ranges for both, the 1293 number of container ports in the range must match the 1294 number of host ports in the range, for example: 1295 -p 1234-1236:1234-1236/tcp 1296 1297 When specifying a range for hostPort only, the 1298 containerPort must not be a range. In this case the 1299 container port is published somewhere within the 1300 specified hostPort range. (e.g., `-p 1234-1236:1234/tcp`) 1301 1302 (use 'docker port' to see the actual mapping) 1303 1304 --link="" : Add link to another container (<name or id>:alias or <name or id>) 1305 1306 With the exception of the `EXPOSE` directive, an image developer hasn't 1307 got much control over networking. The `EXPOSE` instruction defines the 1308 initial incoming ports that provide services. These ports are available 1309 to processes inside the container. An operator can use the `--expose` 1310 option to add to the exposed ports. 1311 1312 To expose a container's internal port, an operator can start the 1313 container with the `-P` or `-p` flag. The exposed port is accessible on 1314 the host and the ports are available to any client that can reach the 1315 host. 1316 1317 The `-P` option publishes all the ports to the host interfaces. Docker 1318 binds each exposed port to a random port on the host. The range of 1319 ports are within an *ephemeral port range* defined by 1320 `/proc/sys/net/ipv4/ip_local_port_range`. Use the `-p` flag to 1321 explicitly map a single port or range of ports. 1322 1323 The port number inside the container (where the service listens) does 1324 not need to match the port number exposed on the outside of the 1325 container (where clients connect). For example, inside the container an 1326 HTTP service is listening on port 80 (and so the image developer 1327 specifies `EXPOSE 80` in the Dockerfile). At runtime, the port might be 1328 bound to 42800 on the host. To find the mapping between the host ports 1329 and the exposed ports, use `docker port`. 1330 1331 If the operator uses `--link` when starting a new client container in the 1332 default bridge network, then the client container can access the exposed 1333 port via a private networking interface. 1334 If `--link` is used when starting a container in a user-defined network as 1335 described in [*Docker network overview*""](../userguide/networking/index.md)), 1336 it will provide a named alias for the container being linked to. 1337 1338 ### ENV (environment variables) 1339 1340 When a new container is created, Docker will set the following environment 1341 variables automatically: 1342 1343 <table> 1344 <tr> 1345 <th>Variable</th> 1346 <th>Value</th> 1347 </tr> 1348 <tr> 1349 <td><code>HOME</code></td> 1350 <td> 1351 Set based on the value of <code>USER</code> 1352 </td> 1353 </tr> 1354 <tr> 1355 <td><code>HOSTNAME</code></td> 1356 <td> 1357 The hostname associated with the container 1358 </td> 1359 </tr> 1360 <tr> 1361 <td><code>PATH</code></td> 1362 <td> 1363 Includes popular directories, such as :<br> 1364 <code>/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin</code> 1365 </td> 1366 <tr> 1367 <td><code>TERM</code></td> 1368 <td><code>xterm</code> if the container is allocated a pseudo-TTY</td> 1369 </tr> 1370 </table> 1371 1372 Additionally, the operator can **set any environment variable** in the 1373 container by using one or more `-e` flags, even overriding those mentioned 1374 above, or already defined by the developer with a Dockerfile `ENV`: 1375 1376 $ docker run -e "deep=purple" --rm ubuntu /bin/bash -c export 1377 declare -x HOME="/" 1378 declare -x HOSTNAME="85bc26a0e200" 1379 declare -x OLDPWD 1380 declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" 1381 declare -x PWD="/" 1382 declare -x SHLVL="1" 1383 declare -x deep="purple" 1384 1385 Similarly the operator can set the **hostname** with `-h`. 1386 1387 ### TMPFS (mount tmpfs filesystems) 1388 1389 ```bash 1390 --tmpfs=[]: Create a tmpfs mount with: container-dir[:<options>], 1391 where the options are identical to the Linux 1392 'mount -t tmpfs -o' command. 1393 ``` 1394 1395 The example below mounts an empty tmpfs into the container with the `rw`, 1396 `noexec`, `nosuid`, and `size=65536k` options. 1397 1398 $ docker run -d --tmpfs /run:rw,noexec,nosuid,size=65536k my_image 1399 1400 ### VOLUME (shared filesystems) 1401 1402 -v, --volume=[host-src:]container-dest[:<options>]: Bind mount a volume. 1403 The comma-delimited `options` are [rw|ro], [z|Z], 1404 [[r]shared|[r]slave|[r]private], and [nocopy]. 1405 The 'host-src' is an absolute path or a name value. 1406 1407 If neither 'rw' or 'ro' is specified then the volume is mounted in 1408 read-write mode. 1409 1410 The `nocopy` modes is used to disable automatic copying requested volume 1411 path in the container to the volume storage location. 1412 For named volumes, `copy` is the default mode. Copy modes are not supported 1413 for bind-mounted volumes. 1414 1415 --volumes-from="": Mount all volumes from the given container(s) 1416 1417 > **Note**: 1418 > When using systemd to manage the Docker daemon's start and stop, in the systemd 1419 > unit file there is an option to control mount propagation for the Docker daemon 1420 > itself, called `MountFlags`. The value of this setting may cause Docker to not 1421 > see mount propagation changes made on the mount point. For example, if this value 1422 > is `slave`, you may not be able to use the `shared` or `rshared` propagation on 1423 > a volume. 1424 1425 The volumes commands are complex enough to have their own documentation 1426 in section [*Managing data in 1427 containers*](../userguide/containers/dockervolumes.md). A developer can define 1428 one or more `VOLUME`'s associated with an image, but only the operator 1429 can give access from one container to another (or from a container to a 1430 volume mounted on the host). 1431 1432 The `container-dest` must always be an absolute path such as `/src/docs`. 1433 The `host-src` can either be an absolute path or a `name` value. If you 1434 supply an absolute path for the `host-dir`, Docker bind-mounts to the path 1435 you specify. If you supply a `name`, Docker creates a named volume by that `name`. 1436 1437 A `name` value must start with start with an alphanumeric character, 1438 followed by `a-z0-9`, `_` (underscore), `.` (period) or `-` (hyphen). 1439 An absolute path starts with a `/` (forward slash). 1440 1441 For example, you can specify either `/foo` or `foo` for a `host-src` value. 1442 If you supply the `/foo` value, Docker creates a bind-mount. If you supply 1443 the `foo` specification, Docker creates a named volume. 1444 1445 ### USER 1446 1447 `root` (id = 0) is the default user within a container. The image developer can 1448 create additional users. Those users are accessible by name. When passing a numeric 1449 ID, the user does not have to exist in the container. 1450 1451 The developer can set a default user to run the first process with the 1452 Dockerfile `USER` instruction. When starting a container, the operator can override 1453 the `USER` instruction by passing the `-u` option. 1454 1455 -u="", --user="": Sets the username or UID used and optionally the groupname or GID for the specified command. 1456 1457 The followings examples are all valid: 1458 --user=[ user | user:group | uid | uid:gid | user:gid | uid:group ] 1459 1460 > **Note:** if you pass a numeric uid, it must be in the range of 0-2147483647. 1461 1462 ### WORKDIR 1463 1464 The default working directory for running binaries within a container is the 1465 root directory (`/`), but the developer can set a different default with the 1466 Dockerfile `WORKDIR` command. The operator can override this with: 1467 1468 -w="": Working directory inside the container