We now have instructions in our Unix installs about setting up
docker group to avoid sudo. Also, Mac/Windows shouldn't use
sudo. So, I've removed sudo from our examples and added a
section at the top reminding them that if they have to use
sudo to run docker they can change that.
Signed-off-by: Mary Anthony <mary@docker.com>
| ... | ... |
@@ -24,26 +24,30 @@ other `docker` command. |
| 24 | 24 |
|
| 25 | 25 |
The basic `docker run` command takes this form: |
| 26 | 26 |
|
| 27 |
- $ sudo docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] |
|
| 27 |
+ $ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] |
|
| 28 | 28 |
|
| 29 | 29 |
To learn how to interpret the types of `[OPTIONS]`, |
| 30 | 30 |
see [*Option types*](/reference/commandline/cli/#option-types). |
| 31 | 31 |
|
| 32 |
-The list of `[OPTIONS]` breaks down into two groups: |
|
| 33 |
- |
|
| 34 |
-1. Settings exclusive to operators, including: |
|
| 35 |
- * Detached or Foreground running, |
|
| 36 |
- * Container Identification, |
|
| 37 |
- * Network settings, and |
|
| 38 |
- * Runtime Constraints on CPU and Memory |
|
| 39 |
- * Privileges and LXC Configuration |
|
| 40 |
-2. Settings shared between operators and developers, where operators can |
|
| 41 |
- override defaults developers set in images at build time. |
|
| 42 |
- |
|
| 43 |
-Together, the `docker run [OPTIONS]` give the operator complete control over runtime |
|
| 44 |
-behavior, allowing them to override all defaults set by |
|
| 45 |
-the developer during `docker build` and nearly all the defaults set by |
|
| 46 |
-the Docker runtime itself. |
|
| 32 |
+The `run` options control the image's runtime behavior in a container. These |
|
| 33 |
+settings affect: |
|
| 34 |
+ |
|
| 35 |
+ * detached or foreground running |
|
| 36 |
+ * container identification |
|
| 37 |
+ * network settings |
|
| 38 |
+ * runtime constraints on CPU and memory |
|
| 39 |
+ * privileges and LXC configuration |
|
| 40 |
+ |
|
| 41 |
+An image developer may set defaults for these same settings when they create the |
|
| 42 |
+image using the `docker build` command. Operators, however, can override all |
|
| 43 |
+defaults set by the developer using the `run` options. And, operators can also |
|
| 44 |
+override nearly all the defaults set by the Docker runtime itself. |
|
| 45 |
+ |
|
| 46 |
+Finally, depending on your Docker system configuration, you may be required to |
|
| 47 |
+preface each `docker` command with `sudo`. To avoid having to use `sudo` with |
|
| 48 |
+the `docker` command, your system administrator can create a Unix group called |
|
| 49 |
+`docker` and add users to it. For more information about this configuration, |
|
| 50 |
+refer to the Docker installation documentation for your operating system. |
|
| 47 | 51 |
|
| 48 | 52 |
## Operator exclusive options |
| 49 | 53 |
|
| ... | ... |
@@ -99,13 +103,13 @@ streams]( https://github.com/docker/docker/blob/ |
| 99 | 99 |
specify to which of the three standard streams (`STDIN`, `STDOUT`, |
| 100 | 100 |
`STDERR`) you'd like to connect instead, as in: |
| 101 | 101 |
|
| 102 |
- $ sudo docker run -a stdin -a stdout -i -t ubuntu /bin/bash |
|
| 102 |
+ $ docker run -a stdin -a stdout -i -t ubuntu /bin/bash |
|
| 103 | 103 |
|
| 104 | 104 |
For interactive processes (like a shell), you must use `-i -t` together in |
| 105 | 105 |
order to allocate a tty for the container process. `-i -t` is often written `-it` |
| 106 | 106 |
as you'll see in later examples. Specifying `-t` is forbidden when the client |
| 107 | 107 |
standard output is redirected or piped, such as in: |
| 108 |
-`echo test | sudo docker run -i busybox cat`. |
|
| 108 |
+`echo test | docker run -i busybox cat`. |
|
| 109 | 109 |
|
| 110 | 110 |
## Container identification |
| 111 | 111 |
|
| ... | ... |
@@ -163,7 +167,7 @@ on the system. For example, you could build a container with debugging tools |
| 163 | 163 |
like `strace` or `gdb`, but want to use these tools when debugging processes |
| 164 | 164 |
within the container. |
| 165 | 165 |
|
| 166 |
- $ sudo docker run --pid=host rhel7 strace -p 1234 |
|
| 166 |
+ $ docker run --pid=host rhel7 strace -p 1234 |
|
| 167 | 167 |
|
| 168 | 168 |
This command would allow you to use `strace` inside the container on pid 1234 on |
| 169 | 169 |
the host. |
| ... | ... |
@@ -288,9 +292,9 @@ Example running a Redis container with Redis binding to `localhost` then |
| 288 | 288 |
running the `redis-cli` command and connecting to the Redis server over the |
| 289 | 289 |
`localhost` interface. |
| 290 | 290 |
|
| 291 |
- $ sudo docker run -d --name redis example/redis --bind 127.0.0.1 |
|
| 291 |
+ $ docker run -d --name redis example/redis --bind 127.0.0.1 |
|
| 292 | 292 |
$ # use the redis container's network stack to access localhost |
| 293 |
- $ sudo docker run --rm -it --net container:redis example/redis-cli -h 127.0.0.1 |
|
| 293 |
+ $ docker run --rm -it --net container:redis example/redis-cli -h 127.0.0.1 |
|
| 294 | 294 |
|
| 295 | 295 |
### Managing /etc/hosts |
| 296 | 296 |
|
| ... | ... |
@@ -298,7 +302,7 @@ Your container will have lines in `/etc/hosts` which define the hostname of the |
| 298 | 298 |
container itself as well as `localhost` and a few other common things. The |
| 299 | 299 |
`--add-host` flag can be used to add additional lines to `/etc/hosts`. |
| 300 | 300 |
|
| 301 |
- $ sudo docker run -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts |
|
| 301 |
+ $ docker run -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts |
|
| 302 | 302 |
172.17.0.22 09d03f76bf2c |
| 303 | 303 |
fe00::0 ip6-localnet |
| 304 | 304 |
ff00::0 ip6-mcastprefix |
| ... | ... |
@@ -374,7 +378,7 @@ for a container can be obtained via [`docker inspect`]( |
| 374 | 374 |
/reference/commandline/cli/#inspect). For example, to get the number of restarts |
| 375 | 375 |
for container "my-container"; |
| 376 | 376 |
|
| 377 |
- $ sudo docker inspect -f "{{ .RestartCount }}" my-container
|
|
| 377 |
+ $ docker inspect -f "{{ .RestartCount }}" my-container
|
|
| 378 | 378 |
# 2 |
| 379 | 379 |
|
| 380 | 380 |
Or, to get the last time the container was (re)started; |
| ... | ... |
@@ -388,12 +392,12 @@ results in an error. |
| 388 | 388 |
|
| 389 | 389 |
###Examples |
| 390 | 390 |
|
| 391 |
- $ sudo docker run --restart=always redis |
|
| 391 |
+ $ docker run --restart=always redis |
|
| 392 | 392 |
|
| 393 | 393 |
This will run the `redis` container with a restart policy of **always** |
| 394 | 394 |
so that if the container exits, Docker will restart it. |
| 395 | 395 |
|
| 396 |
- $ sudo docker run --restart=on-failure:10 redis |
|
| 396 |
+ $ docker run --restart=on-failure:10 redis |
|
| 397 | 397 |
|
| 398 | 398 |
This will run the `redis` container with a restart policy of **on-failure** |
| 399 | 399 |
and a maximum restart count of 10. If the `redis` container exits with a |
| ... | ... |
@@ -427,23 +431,23 @@ the `--security-opt` flag. For example, you can specify the MCS/MLS level, a |
| 427 | 427 |
requirement for MLS systems. Specifying the level in the following command |
| 428 | 428 |
allows you to share the same content between containers. |
| 429 | 429 |
|
| 430 |
- $ sudo docker run --security-opt label:level:s0:c100,c200 -i -t fedora bash |
|
| 430 |
+ $ docker run --security-opt label:level:s0:c100,c200 -i -t fedora bash |
|
| 431 | 431 |
|
| 432 | 432 |
An MLS example might be: |
| 433 | 433 |
|
| 434 |
- $ sudo docker run --security-opt label:level:TopSecret -i -t rhel7 bash |
|
| 434 |
+ $ docker run --security-opt label:level:TopSecret -i -t rhel7 bash |
|
| 435 | 435 |
|
| 436 | 436 |
To disable the security labeling for this container versus running with the |
| 437 | 437 |
`--permissive` flag, use the following command: |
| 438 | 438 |
|
| 439 |
- $ sudo docker run --security-opt label:disable -i -t fedora bash |
|
| 439 |
+ $ docker run --security-opt label:disable -i -t fedora bash |
|
| 440 | 440 |
|
| 441 | 441 |
If you want a tighter security policy on the processes within a container, |
| 442 | 442 |
you can specify an alternate type for the container. You could run a container |
| 443 | 443 |
that is only allowed to listen on Apache ports by executing the following |
| 444 | 444 |
command: |
| 445 | 445 |
|
| 446 |
- $ sudo docker run --security-opt label:type:svirt_apache_t -i -t centos bash |
|
| 446 |
+ $ docker run --security-opt label:type:svirt_apache_t -i -t centos bash |
|
| 447 | 447 |
|
| 448 | 448 |
Note: |
| 449 | 449 |
|
| ... | ... |
@@ -511,25 +515,25 @@ We have four ways to set memory usage: |
| 511 | 511 |
|
| 512 | 512 |
Examples: |
| 513 | 513 |
|
| 514 |
- $ sudo docker run -ti ubuntu:14.04 /bin/bash |
|
| 514 |
+ $ docker run -ti ubuntu:14.04 /bin/bash |
|
| 515 | 515 |
|
| 516 | 516 |
We set nothing about memory, this means the processes in the container can use |
| 517 | 517 |
as much memory and swap memory as they need. |
| 518 | 518 |
|
| 519 |
- $ sudo docker run -ti -m 300M --memory-swap -1 ubuntu:14.04 /bin/bash |
|
| 519 |
+ $ docker run -ti -m 300M --memory-swap -1 ubuntu:14.04 /bin/bash |
|
| 520 | 520 |
|
| 521 | 521 |
We set memory limit and disabled swap memory limit, this means the processes in |
| 522 | 522 |
the container can use 300M memory and as much swap memory as they need (if the |
| 523 | 523 |
host supports swap memory). |
| 524 | 524 |
|
| 525 |
- $ sudo docker run -ti -m 300M ubuntu:14.04 /bin/bash |
|
| 525 |
+ $ docker run -ti -m 300M ubuntu:14.04 /bin/bash |
|
| 526 | 526 |
|
| 527 | 527 |
We set memory limit only, this means the processes in the container can use |
| 528 | 528 |
300M memory and 300M swap memory, by default, the total virtual memory size |
| 529 | 529 |
(--memory-swap) will be set as double of memory, in this case, memory + swap |
| 530 | 530 |
would be 2*300M, so processes can use 300M swap memory as well. |
| 531 | 531 |
|
| 532 |
- $ sudo docker run -ti -m 300M --memory-swap 1G ubuntu:14.04 /bin/bash |
|
| 532 |
+ $ docker run -ti -m 300M --memory-swap 1G ubuntu:14.04 /bin/bash |
|
| 533 | 533 |
|
| 534 | 534 |
We set both memory and swap memory, so the processes in the container can use |
| 535 | 535 |
300M memory and 700M swap memory. |
| ... | ... |
@@ -575,11 +579,11 @@ We can set cpus in which to allow execution for containers. |
| 575 | 575 |
|
| 576 | 576 |
Examples: |
| 577 | 577 |
|
| 578 |
- $ sudo docker run -ti --cpuset-cpus="1,3" ubuntu:14.04 /bin/bash |
|
| 578 |
+ $ docker run -ti --cpuset-cpus="1,3" ubuntu:14.04 /bin/bash |
|
| 579 | 579 |
|
| 580 | 580 |
This means processes in container can be executed on cpu 1 and cpu 3. |
| 581 | 581 |
|
| 582 |
- $ sudo docker run -ti --cpuset-cpus="0-2" ubuntu:14.04 /bin/bash |
|
| 582 |
+ $ docker run -ti --cpuset-cpus="0-2" ubuntu:14.04 /bin/bash |
|
| 583 | 583 |
|
| 584 | 584 |
This means processes in container can be executed on cpu 0, cpu 1 and cpu 2. |
| 585 | 585 |
|
| ... | ... |
@@ -610,23 +614,23 @@ If you want to limit access to a specific device or devices you can use |
| 610 | 610 |
the `--device` flag. It allows you to specify one or more devices that |
| 611 | 611 |
will be accessible within the container. |
| 612 | 612 |
|
| 613 |
- $ sudo docker run --device=/dev/snd:/dev/snd ... |
|
| 613 |
+ $ docker run --device=/dev/snd:/dev/snd ... |
|
| 614 | 614 |
|
| 615 | 615 |
By default, the container will be able to `read`, `write`, and `mknod` these devices. |
| 616 | 616 |
This can be overridden using a third `:rwm` set of options to each `--device` flag: |
| 617 | 617 |
|
| 618 |
- $ sudo docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc |
|
| 618 |
+ $ docker run --device=/dev/sda:/dev/xvdc --rm -it ubuntu fdisk /dev/xvdc |
|
| 619 | 619 |
|
| 620 | 620 |
Command (m for help): q |
| 621 |
- $ sudo docker run --device=/dev/sda:/dev/xvdc:r --rm -it ubuntu fdisk /dev/xvdc |
|
| 621 |
+ $ docker run --device=/dev/sda:/dev/xvdc:r --rm -it ubuntu fdisk /dev/xvdc |
|
| 622 | 622 |
You will not be able to write the partition table. |
| 623 | 623 |
|
| 624 | 624 |
Command (m for help): q |
| 625 | 625 |
|
| 626 |
- $ sudo docker run --device=/dev/sda:/dev/xvdc:w --rm -it ubuntu fdisk /dev/xvdc |
|
| 626 |
+ $ docker run --device=/dev/sda:/dev/xvdc:w --rm -it ubuntu fdisk /dev/xvdc |
|
| 627 | 627 |
crash.... |
| 628 | 628 |
|
| 629 |
- $ sudo docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc |
|
| 629 |
+ $ docker run --device=/dev/sda:/dev/xvdc:m --rm -it ubuntu fdisk /dev/xvdc |
|
| 630 | 630 |
fdisk: unable to open /dev/xvdc: Operation not permitted |
| 631 | 631 |
|
| 632 | 632 |
In addition to `--privileged`, the operator can have fine grain control over the |
| ... | ... |
@@ -634,23 +638,23 @@ capabilities using `--cap-add` and `--cap-drop`. By default, Docker has a defaul |
| 634 | 634 |
list of capabilities that are kept. Both flags support the value `all`, so if the |
| 635 | 635 |
operator wants to have all capabilities but `MKNOD` they could use: |
| 636 | 636 |
|
| 637 |
- $ sudo docker run --cap-add=ALL --cap-drop=MKNOD ... |
|
| 637 |
+ $ docker run --cap-add=ALL --cap-drop=MKNOD ... |
|
| 638 | 638 |
|
| 639 | 639 |
For interacting with the network stack, instead of using `--privileged` they |
| 640 | 640 |
should use `--cap-add=NET_ADMIN` to modify the network interfaces. |
| 641 | 641 |
|
| 642 |
- $ sudo docker run -t -i --rm ubuntu:14.04 ip link add dummy0 type dummy |
|
| 642 |
+ $ docker run -t -i --rm ubuntu:14.04 ip link add dummy0 type dummy |
|
| 643 | 643 |
RTNETLINK answers: Operation not permitted |
| 644 |
- $ sudo docker run -t -i --rm --cap-add=NET_ADMIN ubuntu:14.04 ip link add dummy0 type dummy |
|
| 644 |
+ $ docker run -t -i --rm --cap-add=NET_ADMIN ubuntu:14.04 ip link add dummy0 type dummy |
|
| 645 | 645 |
|
| 646 | 646 |
To mount a FUSE based filesystem, you need to combine both `--cap-add` and |
| 647 | 647 |
`--device`: |
| 648 | 648 |
|
| 649 |
- $ sudo docker run --rm -it --cap-add SYS_ADMIN sshfs sshfs sven@10.10.10.20:/home/sven /mnt |
|
| 649 |
+ $ docker run --rm -it --cap-add SYS_ADMIN sshfs sshfs sven@10.10.10.20:/home/sven /mnt |
|
| 650 | 650 |
fuse: failed to open /dev/fuse: Operation not permitted |
| 651 |
- $ sudo docker run --rm -it --device /dev/fuse sshfs sshfs sven@10.10.10.20:/home/sven /mnt |
|
| 651 |
+ $ docker run --rm -it --device /dev/fuse sshfs sshfs sven@10.10.10.20:/home/sven /mnt |
|
| 652 | 652 |
fusermount: mount failed: Operation not permitted |
| 653 |
- $ sudo docker run --rm -it --cap-add SYS_ADMIN --device /dev/fuse sshfs |
|
| 653 |
+ $ docker run --rm -it --cap-add SYS_ADMIN --device /dev/fuse sshfs |
|
| 654 | 654 |
# sshfs sven@10.10.10.20:/home/sven /mnt |
| 655 | 655 |
The authenticity of host '10.10.10.20 (10.10.10.20)' can't be established. |
| 656 | 656 |
ECDSA key fingerprint is 25:34:85:75:25:b0:17:46:05:19:04:93:b5:dd:5f:c6. |
| ... | ... |
@@ -727,7 +731,7 @@ Dockerfile instruction and how the operator can override that setting. |
| 727 | 727 |
Recall the optional `COMMAND` in the Docker |
| 728 | 728 |
commandline: |
| 729 | 729 |
|
| 730 |
- $ sudo docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] |
|
| 730 |
+ $ docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...] |
|
| 731 | 731 |
|
| 732 | 732 |
This command is optional because the person who created the `IMAGE` may |
| 733 | 733 |
have already provided a default `COMMAND` using the Dockerfile `CMD` |
| ... | ... |
@@ -754,12 +758,12 @@ runtime by using a string to specify the new `ENTRYPOINT`. Here is an |
| 754 | 754 |
example of how to run a shell in a container that has been set up to |
| 755 | 755 |
automatically run something else (like `/usr/bin/redis-server`): |
| 756 | 756 |
|
| 757 |
- $ sudo docker run -i -t --entrypoint /bin/bash example/redis |
|
| 757 |
+ $ docker run -i -t --entrypoint /bin/bash example/redis |
|
| 758 | 758 |
|
| 759 | 759 |
or two examples of how to pass more parameters to that ENTRYPOINT: |
| 760 | 760 |
|
| 761 |
- $ sudo docker run -i -t --entrypoint /bin/bash example/redis -c ls -l |
|
| 762 |
- $ sudo docker run -i -t --entrypoint /usr/bin/redis-cli example/redis --help |
|
| 761 |
+ $ docker run -i -t --entrypoint /bin/bash example/redis -c ls -l |
|
| 762 |
+ $ docker run -i -t --entrypoint /usr/bin/redis-cli example/redis --help |
|
| 763 | 763 |
|
| 764 | 764 |
## EXPOSE (incoming ports) |
| 765 | 765 |
|
| ... | ... |
@@ -846,7 +850,7 @@ Additionally, the operator can **set any environment variable** in the |
| 846 | 846 |
container by using one or more `-e` flags, even overriding those mentioned |
| 847 | 847 |
above, or already defined by the developer with a Dockerfile `ENV`: |
| 848 | 848 |
|
| 849 |
- $ sudo docker run -e "deep=purple" --rm ubuntu /bin/bash -c export |
|
| 849 |
+ $ docker run -e "deep=purple" --rm ubuntu /bin/bash -c export |
|
| 850 | 850 |
declare -x HOME="/" |
| 851 | 851 |
declare -x HOSTNAME="85bc26a0e200" |
| 852 | 852 |
declare -x OLDPWD |
| ... | ... |
@@ -864,23 +868,23 @@ information for connecting to the service container. Let's imagine we have a |
| 864 | 864 |
container running Redis: |
| 865 | 865 |
|
| 866 | 866 |
# Start the service container, named redis-name |
| 867 |
- $ sudo docker run -d --name redis-name dockerfiles/redis |
|
| 867 |
+ $ docker run -d --name redis-name dockerfiles/redis |
|
| 868 | 868 |
4241164edf6f5aca5b0e9e4c9eccd899b0b8080c64c0cd26efe02166c73208f3 |
| 869 | 869 |
|
| 870 | 870 |
# The redis-name container exposed port 6379 |
| 871 |
- $ sudo docker ps |
|
| 871 |
+ $ docker ps |
|
| 872 | 872 |
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 873 | 873 |
4241164edf6f $ dockerfiles/redis:latest /redis-stable/src/re 5 seconds ago Up 4 seconds 6379/tcp redis-name |
| 874 | 874 |
|
| 875 | 875 |
# Note that there are no public ports exposed since we didn᾿t use -p or -P |
| 876 |
- $ sudo docker port 4241164edf6f 6379 |
|
| 876 |
+ $ docker port 4241164edf6f 6379 |
|
| 877 | 877 |
2014/01/25 00:55:38 Error: No public port '6379' published for 4241164edf6f |
| 878 | 878 |
|
| 879 | 879 |
Yet we can get information about the Redis container's exposed ports |
| 880 | 880 |
with `--link`. Choose an alias that will form a |
| 881 | 881 |
valid environment variable! |
| 882 | 882 |
|
| 883 |
- $ sudo docker run --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c export |
|
| 883 |
+ $ docker run --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c export |
|
| 884 | 884 |
declare -x HOME="/" |
| 885 | 885 |
declare -x HOSTNAME="acda7f7b1cdc" |
| 886 | 886 |
declare -x OLDPWD |
| ... | ... |
@@ -897,15 +901,15 @@ valid environment variable! |
| 897 | 897 |
|
| 898 | 898 |
And we can use that information to connect from another container as a client: |
| 899 | 899 |
|
| 900 |
- $ sudo docker run -i -t --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c '/redis-stable/src/redis-cli -h $REDIS_ALIAS_PORT_6379_TCP_ADDR -p $REDIS_ALIAS_PORT_6379_TCP_PORT' |
|
| 900 |
+ $ docker run -i -t --rm --link redis-name:redis_alias --entrypoint /bin/bash dockerfiles/redis -c '/redis-stable/src/redis-cli -h $REDIS_ALIAS_PORT_6379_TCP_ADDR -p $REDIS_ALIAS_PORT_6379_TCP_PORT' |
|
| 901 | 901 |
172.17.0.32:6379> |
| 902 | 902 |
|
| 903 | 903 |
Docker will also map the private IP address to the alias of a linked |
| 904 | 904 |
container by inserting an entry into `/etc/hosts`. You can use this |
| 905 | 905 |
mechanism to communicate with a linked container by its alias: |
| 906 | 906 |
|
| 907 |
- $ sudo docker run -d --name servicename busybox sleep 30 |
|
| 908 |
- $ sudo docker run -i -t --link servicename:servicealias busybox ping -c 1 servicealias |
|
| 907 |
+ $ docker run -d --name servicename busybox sleep 30 |
|
| 908 |
+ $ docker run -i -t --link servicename:servicealias busybox ping -c 1 servicealias |
|
| 909 | 909 |
|
| 910 | 910 |
If you restart the source container (`servicename` in this case), the recipient |
| 911 | 911 |
container's `/etc/hosts` entry will be automatically updated. |