Browse code

Improve the way we deliver Examples in command line. (Add descriptive titles)

Signed-off-by: Jian Zhang <zhangjian.fnst@cn.fujitsu.com>

Jian Zhang authored on 2015/09/28 10:45:10
Showing 2 changed files
... ...
@@ -128,6 +128,8 @@ See also:
128 128
 
129 129
 ## Examples
130 130
 
131
+### Build with PATH
132
+
131 133
     $ docker build .
132 134
     Uploading context 10240 bytes
133 135
     Step 1 : FROM busybox
... ...
@@ -168,6 +170,31 @@ The transfer of context from the local machine to the Docker daemon is what the
168 168
 If you wish to keep the intermediate containers after the build is complete,
169 169
 you must use `--rm=false`. This does not affect the build cache.
170 170
 
171
+### Build with URL
172
+
173
+    $ docker build github.com/creack/docker-firefox
174
+
175
+This will clone the GitHub repository and use the cloned repository as context.
176
+The Dockerfile at the root of the repository is used as Dockerfile. Note that
177
+you can specify an arbitrary Git repository by using the `git://` or `git@`
178
+schema.
179
+
180
+### Build with -
181
+
182
+    $ docker build - < Dockerfile
183
+
184
+This will read a Dockerfile from `STDIN` without context. Due to the lack of a
185
+context, no contents of any local directory will be sent to the Docker daemon.
186
+Since there is no context, a Dockerfile `ADD` only works if it refers to a
187
+remote URL.
188
+
189
+    $ docker build - < context.tar.gz
190
+
191
+This will build an image for a compressed context read from `STDIN`.  Supported
192
+formats are: bzip2, gzip and xz.
193
+
194
+### Usage of .dockerignore
195
+
171 196
     $ docker build .
172 197
     Uploading context 18.829 MB
173 198
     Uploading context
... ...
@@ -193,29 +220,14 @@ directory from the context. Its effect can be seen in the changed size of the
193 193
 uploaded context. The builder reference contains detailed information on
194 194
 [creating a .dockerignore file](../builder.md#dockerignore-file)
195 195
 
196
+### Tag image (-t)
197
+
196 198
     $ docker build -t vieux/apache:2.0 .
197 199
 
198 200
 This will build like the previous example, but it will then tag the resulting
199 201
 image. The repository name will be `vieux/apache` and the tag will be `2.0`
200 202
 
201
-    $ docker build - < Dockerfile
202
-
203
-This will read a Dockerfile from `STDIN` without context. Due to the lack of a
204
-context, no contents of any local directory will be sent to the Docker daemon.
205
-Since there is no context, a Dockerfile `ADD` only works if it refers to a
206
-remote URL.
207
-
208
-    $ docker build - < context.tar.gz
209
-
210
-This will build an image for a compressed context read from `STDIN`.  Supported
211
-formats are: bzip2, gzip and xz.
212
-
213
-    $ docker build github.com/creack/docker-firefox
214
-
215
-This will clone the GitHub repository and use the cloned repository as context.
216
-The Dockerfile at the root of the repository is used as Dockerfile. Note that
217
-you can specify an arbitrary Git repository by using the `git://` or `git@`
218
-schema.
203
+### Specify Dockerfile (-f)
219 204
 
220 205
     $ docker build -f Dockerfile.debug .
221 206
 
... ...
@@ -248,14 +260,20 @@ the command line.
248 248
 > repeatable builds on remote Docker hosts. This is also the reason why
249 249
 > `ADD ../file` will not work.
250 250
 
251
+### Optional parent cgroup (--cgroup-parent)
252
+
251 253
 When `docker build` is run with the `--cgroup-parent` option the containers
252 254
 used in the build will be run with the [corresponding `docker run`
253 255
 flag](../run.md#specifying-custom-cgroups).
254 256
 
257
+### Set ulimits in container (--ulimit)
258
+
255 259
 Using the `--ulimit` option with `docker build` will cause each build step's
256 260
 container to be started using those [`--ulimit`
257 261
 flag values](../run.md#setting-ulimits-in-a-container).
258 262
 
263
+### Set build-time variables (--build-arg)
264
+
259 265
 You can use `ENV` instructions in a Dockerfile to define variable
260 266
 values. These values persist in the built image. However, often
261 267
 persistence is not what you want. Users want to specify variables differently
... ...
@@ -263,7 +281,7 @@ depending on which host they build an image on.
263 263
 
264 264
 A good example is `http_proxy` or source versions for pulling intermediate
265 265
 files. The `ARG` instruction lets Dockerfile authors define values that users
266
-can set at build-time using the  `---build-arg` flag:
266
+can set at build-time using the  `--build-arg` flag:
267 267
 
268 268
     $ docker build --build-arg HTTP_PROXY=http://10.20.30.2:1234 .
269 269
 
... ...
@@ -92,6 +92,8 @@ and linking containers.
92 92
 
93 93
 ## Examples
94 94
 
95
+### Assign name and allocate psuedo-TTY (--name, -it)
96
+
95 97
     $ docker run --name test -it debian
96 98
     root@d6c0fe130dba:/# exit 13
97 99
     $ echo $?
... ...
@@ -106,6 +108,8 @@ In the example, the `bash` shell is quit by entering
106 106
 `exit 13`. This exit code is passed on to the caller of
107 107
 `docker run`, and is recorded in the `test` container's metadata.
108 108
 
109
+### Capture container ID (--cidfile)
110
+
109 111
     $ docker run --cidfile /tmp/docker_test.cid ubuntu echo "test"
110 112
 
111 113
 This will create a container and print `test` to the console. The `cidfile`
... ...
@@ -113,6 +117,8 @@ flag makes Docker attempt to create a new file and write the container ID to it.
113 113
 If the file exists already, Docker will return an error. Docker will close this
114 114
 file when `docker run` exits.
115 115
 
116
+### Full container capabilities (--privileged)
117
+
116 118
     $ docker run -t -i --rm ubuntu bash
117 119
     root@bc338942ef20:/# mount -t tmpfs none /mnt
118 120
     mount: permission denied
... ...
@@ -132,11 +138,15 @@ lifts all the limitations enforced by the `device` cgroup controller. In other
132 132
 words, the container can then do almost everything that the host can do. This
133 133
 flag exists to allow special use-cases, like running Docker within Docker.
134 134
 
135
+### Set working directory (-w)
136
+
135 137
     $ docker  run -w /path/to/dir/ -i -t  ubuntu pwd
136 138
 
137 139
 The `-w` lets the command being executed inside directory given, here
138 140
 `/path/to/dir/`. If the path does not exists it is created inside the container.
139 141
 
142
+### Mount volume (-v, --read-only)
143
+
140 144
     $ docker  run  -v `pwd`:`pwd` -w `pwd` -i -t  ubuntu pwd
141 145
 
142 146
 The `-v` flag mounts the current working directory into the container. The `-w`
... ...
@@ -166,6 +176,8 @@ binary (such as that provided by [https://get.docker.com](
166 166
 https://get.docker.com)), you give the container the full access to create and
167 167
 manipulate the host's Docker daemon.
168 168
 
169
+### Publish or expose port (-p, --expose)
170
+
169 171
     $ docker run -p 127.0.0.1:80:8080 ubuntu bash
170 172
 
171 173
 This binds port `8080` of the container to port `80` on `127.0.0.1` of
... ...
@@ -179,6 +191,8 @@ publishing the port to the host system's interfaces. The [Docker User
179 179
 Guide](../../userguide/dockerlinks.md) explains in detail how to manipulate
180 180
 ports in Docker.
181 181
 
182
+### Set environment variables (-e, --env, --env-file)
183
+
182 184
     $ docker run -e MYVAR1 --env MYVAR2=foo --env-file ./env.list ubuntu bash
183 185
 
184 186
 This sets environmental variables in the container. For illustration all three
... ...
@@ -247,7 +261,9 @@ An example of a file passed with `--env-file`
247 247
     123qwe=bar
248 248
     org.spring.config=something
249 249
 
250
-A label is a a `key=value` pair that applies metadata to a container. To label a container with two labels:
250
+### Set metadata on container (-l, --label, --label-file)
251
+
252
+A label is a `key=value` pair that applies metadata to a container. To label a container with two labels:
251 253
 
252 254
     $ docker run -l my-label --label com.example.foo=bar ubuntu bash
253 255
 
... ...
@@ -281,6 +297,8 @@ For additional information on working with labels, see [*Labels - custom
281 281
 metadata in Docker*](../../userguide/labels-custom-metadata.md) in the Docker User
282 282
 Guide.
283 283
 
284
+### Add link to another container (--link)
285
+
284 286
     $ docker run --link /redis:redis --name console ubuntu bash
285 287
 
286 288
 The `--link` flag will link the container named `/redis` into the newly
... ...
@@ -295,6 +313,8 @@ example as:
295 295
 The `--name` flag will assign the name `console` to the newly created
296 296
 container.
297 297
 
298
+### Mount volumes from container (--volumes-from)
299
+
298 300
     $ docker run --volumes-from 777f7dc92da7 --volumes-from ba8c0c54f0f2:ro -i -t ubuntu pwd
299 301
 
300 302
 The `--volumes-from` flag mounts all the defined volumes from the referenced
... ...
@@ -317,6 +337,8 @@ content label. Shared volume labels allow all containers to read/write content.
317 317
 The `Z` option tells Docker to label the content with a private unshared label.
318 318
 Only the current container can use a private volume.
319 319
 
320
+### Attach to STDIN/STDOUT/STDERR (-a)
321
+
320 322
 The `-a` flag tells `docker run` to bind to the container's `STDIN`, `STDOUT`
321 323
 or `STDERR`. This makes it possible to manipulate the output and input as
322 324
 needed.
... ...
@@ -340,6 +362,8 @@ logs could be retrieved using `docker logs`. This is
340 340
 useful if you need to pipe a file or something else into a container and
341 341
 retrieve the container's ID once the container has finished running.
342 342
 
343
+### Add host device to container (--device)
344
+
343 345
     $ docker run --device=/dev/sdc:/dev/xvdc --device=/dev/sdd --device=/dev/zero:/dev/nulo -i -t ubuntu ls -l /dev/{xvdc,sdd,nulo}
344 346
     brw-rw---- 1 root disk 8, 2 Feb  9 16:05 /dev/xvdc
345 347
     brw-rw---- 1 root disk 8, 3 Feb  9 16:05 /dev/sdd
... ...
@@ -375,38 +399,7 @@ flag:
375 375
 > that may be removed should not be added to untrusted containers with
376 376
 > `--device`.
377 377
 
378
-**A complete example:**
379
-
380
-    $ docker run -d --name static static-web-files sh
381
-    $ docker run -d --expose=8098 --name riak riakserver
382
-    $ docker run -d -m 100m -e DEVELOPMENT=1 -e BRANCH=example-code -v $(pwd):/app/bin:ro --name app appserver
383
-    $ 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
384
-    $ docker run -t -i --rm --volumes-from web -w /var/log/httpd busybox tail -f access.log
385
-
386
-This example shows five containers that might be set up to test a web
387
-application change:
388
-
389
-1. Start a pre-prepared volume image `static-web-files` (in the background)
390
-   that has CSS, image and static HTML in it, (with a `VOLUME` instruction in
391
-   the Dockerfile to allow the web server to use those files);
392
-2. Start a pre-prepared `riakserver` image, give the container name `riak` and
393
-   expose port `8098` to any containers that link to it;
394
-3. Start the `appserver` image, restricting its memory usage to 100MB, setting
395
-   two environment variables `DEVELOPMENT` and `BRANCH` and bind-mounting the
396
-   current directory (`$(pwd)`) in the container in read-only mode as `/app/bin`;
397
-4. Start the `webserver`, mapping port `443` in the container to port `1443` on
398
-   the Docker server, setting the DNS server to `10.0.0.1` and DNS search
399
-   domain to `dev.org`, creating a volume to put the log files into (so we can
400
-   access it from another container), then importing the files from the volume
401
-   exposed by the `static` container, and linking to all exposed ports from
402
-   `riak` and `app`. Lastly, we set the hostname to `web.sven.dev.org` so its
403
-   consistent with the pre-generated SSL certificate;
404
-5. Finally, we create a container that runs `tail -f access.log` using the logs
405
-   volume from the `web` container, setting the workdir to `/var/log/httpd`. The
406
-   `--rm` option means that when the container exits, the container's layer is
407
-   removed.
408
-
409
-## Restart policies
378
+### Restart policies (--restart)
410 379
 
411 380
 Use Docker's `--restart` to specify a container's *restart policy*. A restart
412 381
 policy controls whether the Docker daemon restarts a container after exit.
... ...
@@ -468,7 +461,7 @@ More detailed information on restart policies can be found in the
468 468
 [Restart Policies (--restart)](../run.md#restart-policies-restart)
469 469
 section of the Docker run reference page.
470 470
 
471
-## Adding entries to a container hosts file
471
+### Add entries to container hosts file (--add-host)
472 472
 
473 473
 You can add other hosts into a container's `/etc/hosts` file by using one or
474 474
 more `--add-host` flags. This example adds a static address for a host named
... ...
@@ -499,7 +492,7 @@ For IPv6 use the `-6` flag instead of the `-4` flag. For other network
499 499
 devices, replace `eth0` with the correct device name (for example `docker0`
500 500
 for the bridge device).
501 501
 
502
-### Setting ulimits in a container
502
+### Set ulimits in container (--ulimit)
503 503
 
504 504
 Since setting `ulimit` settings in a container requires extra privileges not
505 505
 available in the default container, you can set these using the `--ulimit` flag.
... ...
@@ -519,13 +512,12 @@ available in the default container, you can set these using the `--ulimit` flag.
519 519
 The values are sent to the appropriate `syscall` as they are set.
520 520
 Docker doesn't perform any byte conversion. Take this into account when setting the values.
521 521
 
522
-#### For `nproc` usage:
522
+#### For `nproc` usage
523 523
 
524 524
 Be careful setting `nproc` with the `ulimit` flag as `nproc` is designed by Linux to set the
525 525
 maximum number of processes available to a user, not to a container.  For example, start four
526 526
 containers with `daemon` user:
527 527
 
528
-
529 528
     docker run -d -u daemon --ulimit nproc=3 busybox top
530 529
     docker run -d -u daemon --ulimit nproc=3 busybox top
531 530
     docker run -d -u daemon --ulimit nproc=3 busybox top
... ...
@@ -535,8 +527,39 @@ The 4th container fails and reports "[8] System error: resource temporarily unav
535 535
 This fails because the caller set `nproc=3` resulting in the first three containers using up
536 536
 the three processes quota set for the `daemon` user.
537 537
 
538
-### Stopping a container with a specific signal
538
+### Stop container with signal (--stop-signal)
539 539
 
540 540
 The `--stop-signal` flag sets the system call signal that will be sent to the container to exit.
541 541
 This signal can be a valid unsigned number that matches a position in the kernel's syscall table, for instance 9,
542 542
 or a signal name in the format SIGNAME, for instance SIGKILL.
543
+
544
+### A complete example
545
+
546
+    $ docker run -d --name static static-web-files sh
547
+    $ docker run -d --expose=8098 --name riak riakserver
548
+    $ docker run -d -m 100m -e DEVELOPMENT=1 -e BRANCH=example-code -v $(pwd):/app/bin:ro --name app appserver
549
+    $ 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
550
+    $ docker run -t -i --rm --volumes-from web -w /var/log/httpd busybox tail -f access.log
551
+
552
+This example shows five containers that might be set up to test a web
553
+application change:
554
+
555
+1. Start a pre-prepared volume image `static-web-files` (in the background)
556
+   that has CSS, image and static HTML in it, (with a `VOLUME` instruction in
557
+   the Dockerfile to allow the web server to use those files);
558
+2. Start a pre-prepared `riakserver` image, give the container name `riak` and
559
+   expose port `8098` to any containers that link to it;
560
+3. Start the `appserver` image, restricting its memory usage to 100MB, setting
561
+   two environment variables `DEVELOPMENT` and `BRANCH` and bind-mounting the
562
+   current directory (`$(pwd)`) in the container in read-only mode as `/app/bin`;
563
+4. Start the `webserver`, mapping port `443` in the container to port `1443` on
564
+   the Docker server, setting the DNS server to `10.0.0.1` and DNS search
565
+   domain to `dev.org`, creating a volume to put the log files into (so we can
566
+   access it from another container), then importing the files from the volume
567
+   exposed by the `static` container, and linking to all exposed ports from
568
+   `riak` and `app`. Lastly, we set the hostname to `web.sven.dev.org` so its
569
+   consistent with the pre-generated SSL certificate;
570
+5. Finally, we create a container that runs `tail -f access.log` using the logs
571
+   volume from the `web` container, setting the workdir to `/var/log/httpd`. The
572
+   `--rm` option means that when the container exits, the container's layer is
573
+   removed.
543 574
\ No newline at end of file