@@ -4,15 +4,17 @@ page_keywords: builder, docker, Dockerfile, automation, image creation
44
55# Dockerfile Reference
66
7- ** Docker can act as a builder** and read instructions from a text * Dockerfile*
8- to automate the steps you would otherwise take manually to create an image.
9- Executing ` docker build ` will run your steps and commit them along the way,
10- giving you a final image.
7+ ** Docker can build images automatically** by reading the instructions
8+ from a ` Dockerfile ` . A ` Dockerfile ` is a text document that contains all
9+ the commands you would normally execute manually in order to build a
10+ Docker image. By calling ` docker build ` from your terminal, you can have
11+ Docker build your image step by step, executing the instructions
12+ successively.
1113
1214## Usage
1315
1416To [ * build* ] ( ../commandline/cli/#cli-build ) an image from a source repository,
15- create a description file called Dockerfile at the root of your repository.
17+ create a description file called ` Dockerfile ` at the root of your repository.
1618This file will describe the steps to assemble the image.
1719
1820Then call ` docker build ` with the path of your source repository as the argument
@@ -55,22 +57,21 @@ accelerating `docker build` significantly (indicated by `Using cache`):
5557 ---> 1a5ffc17324d
5658 Successfully built 1a5ffc17324d
5759
58- When you're done with your build, you're ready to look into
59- [ * Pushing a repository to its registry* ] (
60- /userguide/dockerrepos/#image-push).
60+ When you're done with your build, you're ready to look into [ * Pushing a
61+ repository to its registry* ] ( /userguide/dockerrepos/#image-push ) .
6162
6263## Format
6364
64- Here is the format of the Dockerfile:
65+ Here is the format of the ` Dockerfile ` :
6566
6667 # Comment
6768 INSTRUCTION arguments
6869
6970The Instruction is not case-sensitive, however convention is for them to
7071be UPPERCASE in order to distinguish them from arguments more easily.
7172
72- Docker evaluates the instructions in a Dockerfile in order. ** The first
73- instruction must be \` FROM\` ** in order to specify the [ * Base
73+ Docker runs the instructions in a ` Dockerfile ` in order. ** The
74+ first instruction must be \` FROM\` ** in order to specify the [ * Base
7475Image* ] ( /terms/image/#base-image-def ) from which you are building.
7576
7677Docker will treat lines that * begin* with ` # ` as a
@@ -80,10 +81,10 @@ be treated as an argument. This allows statements like:
8081 # Comment
8182 RUN echo 'we are running some # of cool things'
8283
83- Here is the set of instructions you can use in a Dockerfile
84- for building images.
84+ Here is the set of instructions you can use in a ` Dockerfile ` for building
85+ images.
8586
86- ## .dockerignore
87+ ## The ` .dockerignore ` file
8788
8889If a file named ` .dockerignore ` exists in the source repository, then it
8990is interpreted as a newline-separated list of exclusion patterns.
124125 FROM <image>:<tag>
125126
126127The ` FROM ` instruction sets the [ * Base Image* ] ( /terms/image/#base-image-def )
127- for subsequent instructions. As such, a valid Dockerfile must have ` FROM ` as
128+ for subsequent instructions. As such, a valid ` Dockerfile ` must have ` FROM ` as
128129its first instruction. The image can be any valid image – it is especially easy
129130to start by ** pulling an image** from the [ * Public Repositories* ] (
130131/userguide/dockerrepos/#using-public-repositories).
131132
132- ` FROM ` must be the first non-comment instruction in the Dockerfile.
133+ ` FROM ` must be the first non-comment instruction in the ` Dockerfile ` .
133134
134- ` FROM ` can appear multiple times within a single Dockerfile in order to create
135- multiple images. Simply make a note of the last image id output by the commit
135+ ` FROM ` can appear multiple times within a single ` Dockerfile ` in order to create
136+ multiple images. Simply make a note of the last image ID output by the commit
136137before each new ` FROM ` command.
137138
138139If no ` tag ` is given to the ` FROM ` instruction, ` latest ` is assumed. If the
@@ -154,7 +155,7 @@ RUN has 2 forms:
154155
155156The ` RUN ` instruction will execute any commands in a new layer on top of the
156157current image and commit the results. The resulting committed image will be
157- used for the next step in the Dockerfile.
158+ used for the next step in the ` Dockerfile ` .
158159
159160Layering ` RUN ` instructions and generating commits conforms to the core
160161concepts of Docker where commits are cheap and containers can be created from
@@ -163,11 +164,11 @@ any point in an image's history, much like source control.
163164The * exec* form makes it possible to avoid shell string munging, and to ` RUN `
164165commands using a base image that does not contain ` /bin/sh ` .
165166
166- The cache for ` RUN ` instructions isn't invalidated automatically during the
167- next build. The cache for an instruction like ` RUN apt-get dist-upgrade -y `
168- will be reused during the next build.
169- The cache for ` RUN ` instructions can be invalidated by using the ` --no-cache `
170- flag, for example ` docker build --no-cache ` .
167+ The cache for ` RUN ` instructions isn't invalidated automatically during
168+ the next build. The cache for an instruction like `RUN apt-get
169+ dist-upgrade -y` will be reused during the next build. The cache for
170+ ` RUN ` instructions can be invalidated by using the ` --no-cache ` flag,
171+ for example ` docker build --no-cache ` .
171172
172173The cache for ` RUN ` instructions can be invalidated by ` ADD ` instructions. See
173174[ below] ( #add ) for details.
@@ -178,36 +179,35 @@ The cache for `RUN` instructions can be invalidated by `ADD` instructions. See
178179 permissions problems that can occur when using the AUFS file system. You
179180 might notice it during an attempt to ` rm ` a file, for example. The issue
180181 describes a workaround.
181- - [ Issue 2424] ( https://github.com/dotcloud/docker/issues/2424 ) Locale will
182- not be set automatically.
183182
184183## CMD
185184
186- CMD has three forms:
185+ The ` CMD ` instruction has three forms:
187186
188187- ` CMD ["executable","param1","param2"] ` (like an * exec* , this is the preferred form)
189188- ` CMD ["param1","param2"] ` (as * default parameters to ENTRYPOINT* )
190189- ` CMD command param1 param2 ` (as a * shell* )
191190
192- There can only be one CMD in a Dockerfile. If you list more than one CMD
193- then only the last CMD will take effect.
191+ There can only be one ` CMD ` instruction in a ` Dockerfile ` . If you list more than one ` CMD `
192+ then only the last ` CMD ` will take effect.
194193
195- ** The main purpose of a CMD is to provide defaults for an executing
194+ ** The main purpose of a ` CMD ` is to provide defaults for an executing
196195container.** These defaults can include an executable, or they can omit
197- the executable, in which case you must specify an ENTRYPOINT as well.
196+ the executable, in which case you must specify an ` ENTRYPOINT `
197+ instruction as well.
198198
199199When used in the shell or exec formats, the ` CMD ` instruction sets the command
200200to be executed when running the image.
201201
202- If you use the * shell* form of the CMD, then the ` <command> ` will execute in
202+ If you use the * shell* form of the ` CMD ` , then the ` <command> ` will execute in
203203` /bin/sh -c ` :
204204
205205 FROM ubuntu
206206 CMD echo "This is a test." | wc -
207207
208208If you want to ** run your** ` <command> ` ** without a shell** then you must
209209express the command as a JSON array and give the full path to the executable.
210- ** This array form is the preferred format of CMD.** Any additional parameters
210+ ** This array form is the preferred format of ` CMD ` .** Any additional parameters
211211must be individually expressed as strings in the array:
212212
213213 FROM ubuntu
@@ -218,7 +218,7 @@ you should consider using `ENTRYPOINT` in combination with `CMD`. See
218218[ * ENTRYPOINT* ] ( #entrypoint ) .
219219
220220If the user specifies arguments to ` docker run ` then they will override the
221- default specified in CMD.
221+ default specified in ` CMD ` .
222222
223223> ** Note** :
224224> don't confuse ` RUN ` with ` CMD ` . ` RUN ` actually runs a command and commits
@@ -264,24 +264,23 @@ being built (also called the *context* of the build) or a remote file URL.
264264` <dest> ` is the absolute path to which the source will be copied inside the
265265destination container.
266266
267- All new files and directories are created with a uid and gid of 0.
267+ All new files and directories are created with a UID and GID of 0.
268268
269- In the case where ` <src> ` is a remote file URL, the destination will have permissions 600.
269+ In the case where ` <src> ` is a remote file URL, the destination will
270+ have permissions of 600.
270271
271272> ** Note** :
272- > If you build by passing a Dockerfile through STDIN (` docker build - < somefile ` ),
273- > there is no build context, so the Dockerfile can only contain a URL
274- > based ADD statement.
275-
276- > You can also pass a compressed archive through STDIN:
277- > (` docker build - < archive.tar.gz ` ), the ` Dockerfile ` at the root of
278- > the archive and the rest of the archive will get used at the context
279- > of the build.
280- >
273+ > If you build by passing a ` Dockerfile ` through STDIN (`docker
274+ > build - < somefile` ), there is no build context, so the ` Dockerfile`
275+ > can only contain a URL based ` ADD ` instruction. You can also pass a
276+ > compressed archive through STDIN: (` docker build - < archive.tar.gz ` ),
277+ > the ` Dockerfile ` at the root of the archive and the rest of the
278+ > archive will get used at the context of the build.
279+
281280> ** Note** :
282- > If your URL files are protected using authentication, you will need to
283- > use ` RUN wget ` , ` RUN curl `
284- > or use another tool from within the container as ADD does not support
281+ > If your URL files are protected using authentication, you
282+ > will need to use ` RUN wget ` , ` RUN curl ` or use another tool from
283+ > within the container as the ` ADD ` instruction does not support
285284> authentication.
286285
287286> ** Note** :
@@ -314,9 +313,9 @@ The copy obeys the following rules:
314313 from * remote* URLs are ** not** decompressed. When a directory is copied or
315314 unpacked, it has the same behavior as ` tar -x ` : the result is the union of:
316315
317- 1 . whatever existed at the destination path and
318- 2 . the contents of the source tree, with conflicts resolved in favor of
319- "2." on a file-by-file basis.
316+ 1 . Whatever existed at the destination path and
317+ 2 . The contents of the source tree, with conflicts resolved in favor
318+ of "2." on a file-by-file basis.
320319
321320- If ` <src> ` is any other kind of file, it is copied individually along with
322321 its metadata. In this case, if ` <dest> ` ends with a trailing slash ` / ` , it
@@ -342,7 +341,7 @@ being built (also called the *context* of the build).
342341` <dest> ` is the absolute path to which the source will be copied inside the
343342destination container.
344343
345- All new files and directories are created with a uid and gid of 0.
344+ All new files and directories are created with a UID and GID of 0.
346345
347346> ** Note** :
348347> If you build using STDIN (` docker build - < somefile ` ), there is no
@@ -431,68 +430,68 @@ instructions via the Docker client, refer to [*Share Directories via Volumes*](
431430
432431 USER daemon
433432
434- The ` USER ` instruction sets the username or UID to use when running the image
433+ The ` USER ` instruction sets the user name or UID to use when running the image
435434and for any following ` RUN ` directives.
436435
437436## WORKDIR
438437
439438 WORKDIR /path/to/workdir
440439
441- The ` WORKDIR ` instruction sets the working directory for the ` RUN ` , ` CMD ` and
442- ` ENTRYPOINT ` Dockerfile commands that follow it.
440+ The ` WORKDIR ` instruction sets the working directory for any ` RUN ` , ` CMD ` and
441+ ` ENTRYPOINT ` instructions that follow it in the ` Dockerfile ` .
443442
444- It can be used multiple times in the one Dockerfile. If a relative path
443+ It can be used multiple times in the one ` Dockerfile ` . If a relative path
445444is provided, it will be relative to the path of the previous ` WORKDIR `
446445instruction. For example:
447446
448447 WORKDIR /a WORKDIR b WORKDIR c RUN pwd
449448
450- The output of the final ` pwd ` command in this
451- Dockerfile would be ` /a/b/c ` .
449+ The output of the final ` pwd ` command in this Dockerfile would be
450+ ` /a/b/c ` .
452451
453452## ONBUILD
454453
455454 ONBUILD [INSTRUCTION]
456455
457- The ` ONBUILD ` instruction adds to the image a
458- "trigger" instruction to be executed at a later time, when the image is
459- used as the base for another build. The trigger will be executed in the
460- context of the downstream build, as if it had been inserted immediately
461- after the * FROM* instruction in the downstream Dockerfile.
456+ The ` ONBUILD ` instruction adds to the image a * trigger * instruction to
457+ be executed at a later time, when the image is used as the base for
458+ another build. The trigger will be executed in the context of the
459+ downstream build, as if it had been inserted immediately after the
460+ ` FROM ` instruction in the downstream ` Dockerfile ` .
462461
463462Any build instruction can be registered as a trigger.
464463
465464This is useful if you are building an image which will be used as a base
466465to build other images, for example an application build environment or a
467466daemon which may be customized with user-specific configuration.
468467
469- For example, if your image is a reusable python application builder, it
468+ For example, if your image is a reusable Python application builder, it
470469will require application source code to be added in a particular
471470directory, and it might require a build script to be called * after*
472- that. You can't just call * ADD* and * RUN* now, because you don't yet
471+ that. You can't just call ` ADD ` and ` RUN ` now, because you don't yet
473472have access to the application source code, and it will be different for
474473each application build. You could simply provide application developers
475- with a boilerplate Dockerfile to copy-paste into their application, but
474+ with a boilerplate ` Dockerfile ` to copy-paste into their application, but
476475that is inefficient, error-prone and difficult to update because it
477476mixes with application-specific code.
478477
479- The solution is to use * ONBUILD* to register in advance instructions to
478+ The solution is to use ` ONBUILD ` to register advance instructions to
480479run later, during the next build stage.
481480
482481Here's how it works:
483482
484- 1 . When it encounters an * ONBUILD* instruction, the builder adds a
483+ 1 . When it encounters an ` ONBUILD ` instruction, the builder adds a
485484 trigger to the metadata of the image being built. The instruction
486485 does not otherwise affect the current build.
4874862 . At the end of the build, a list of all triggers is stored in the
488- image manifest, under the key * OnBuild* . They can be inspected with
489- * docker inspect* .
487+ image manifest, under the key ` OnBuild ` . They can be inspected with
488+ the ` docker inspect ` command .
4904893 . Later the image may be used as a base for a new build, using the
491- * FROM* instruction. As part of processing the * FROM* instruction,
492- the downstream builder looks for * ONBUILD* triggers, and executes
490+ ` FROM ` instruction. As part of processing the ` FROM ` instruction,
491+ the downstream builder looks for ` ONBUILD ` triggers, and executes
493492 them in the same order they were registered. If any of the triggers
494- fail, the * FROM* instruction is aborted which in turn causes the
495- build to fail. If all triggers succeed, the FROM instruction
493+ fail, the ` FROM ` instruction is aborted which in turn causes the
494+ build to fail. If all triggers succeed, the ` FROM ` instruction
496495 completes and the build continues as usual.
4974964 . Triggers are cleared from the final image after being executed. In
498497 other words they are not inherited by "grand-children" builds.
@@ -504,9 +503,9 @@ For example you might add something like this:
504503 ONBUILD RUN /usr/local/bin/python-build --dir /app/src
505504 [...]
506505
507- > ** Warning** : Chaining ONBUILD instructions using ONBUILD ONBUILD isn't allowed.
506+ > ** Warning** : Chaining ` ONBUILD ` instructions using ` ONBUILD ONBUILD ` isn't allowed.
508507
509- > ** Warning** : ONBUILD may not trigger FROM or MAINTAINER instructions.
508+ > ** Warning** : The ` ONBUILD ` instruction may not trigger ` FROM ` or ` MAINTAINER ` instructions.
510509
511510## Dockerfile Examples
512511
@@ -557,3 +556,4 @@ For example you might add something like this:
557556
558557 # You᾿ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
559558 # /oink.
559+
0 commit comments