github.com/feiyang21687/docker@v1.5.0/docs/man/Dockerfile.5.md (about) 1 % DOCKERFILE(5) Docker User Manuals 2 % Zac Dover 3 % May 2014 4 # NAME 5 6 Dockerfile - automate the steps of creating a Docker image 7 8 # INTRODUCTION 9 The **Dockerfile** is a configuration file that automates the steps of creating 10 a Docker image. It is similar to a Makefile. Docker reads instructions from the 11 **Dockerfile** to automate the steps otherwise performed manually to create an 12 image. To build an image, create a file called **Dockerfile**. The 13 **Dockerfile** describes the steps taken to assemble the image. When the 14 **Dockerfile** has been created, call the **docker build** command, using the 15 path of directory that contains **Dockerfile** as the argument. 16 17 # SYNOPSIS 18 19 INSTRUCTION arguments 20 21 For example: 22 23 FROM image 24 25 # DESCRIPTION 26 27 A Dockerfile is a file that automates the steps of creating a Docker image. 28 A Dockerfile is similar to a Makefile. 29 30 # USAGE 31 32 **sudo docker build .** 33 -- runs the steps and commits them, building a final image 34 The path to the source repository defines where to find the context of the 35 build. The build is run by the docker daemon, not the CLI. The whole 36 context must be transferred to the daemon. The Docker CLI reports 37 "Sending build context to Docker daemon" when the context is sent to the daemon. 38 39 **sudo docker build -t repository/tag .** 40 -- specifies a repository and tag at which to save the new image if the build 41 succeeds. The Docker daemon runs the steps one-by-one, committing the result 42 to a new image if necessary before finally outputting the ID of the new 43 image. The Docker daemon automatically cleans up the context it is given. 44 45 Docker re-uses intermediate images whenever possible. This significantly 46 accelerates the *docker build* process. 47 48 # FORMAT 49 50 **FROM image** 51 or 52 **FROM image:tag** 53 -- The FROM instruction sets the base image for subsequent instructions. A 54 valid Dockerfile must have FROM as its first instruction. The image can be any 55 valid image. It is easy to start by pulling an image from the public 56 repositories. 57 -- FROM must be he first non-comment instruction in Dockerfile. 58 -- FROM may appear multiple times within a single Dockerfile in order to create 59 multiple images. Make a note of the last image id output by the commit before 60 each new FROM command. 61 -- If no tag is given to the FROM instruction, latest is assumed. If the used 62 tag does not exist, an error is returned. 63 64 **MAINTAINER** 65 --The MAINTAINER instruction sets the Author field for the generated images. 66 67 **RUN** 68 --RUN has two forms: 69 **RUN <command>** 70 -- (the command is run in a shell - /bin/sh -c) 71 **RUN ["executable", "param1", "param2"]** 72 --The above is executable form. 73 --The RUN instruction executes any commands in a new layer on top of the 74 current image and commits the results. The committed image is used for the next 75 step in Dockerfile. 76 --Layering RUN instructions and generating commits conforms to the core 77 concepts of Docker where commits are cheap and containers can be created from 78 any point in the history of an image. This is similar to source control. The 79 exec form makes it possible to avoid shell string munging. The exec form makes 80 it possible to RUN commands using a base image that does not contain /bin/sh. 81 82 **CMD** 83 --CMD has three forms: 84 **CMD ["executable", "param1", "param2"]** This is the preferred form, the 85 exec form. 86 **CMD ["param1", "param2"]** This command provides default parameters to 87 ENTRYPOINT) 88 **CMD command param1 param2** This command is run as a shell. 89 --There can be only one CMD in a Dockerfile. If more than one CMD is listed, only 90 the last CMD takes effect. 91 The main purpose of a CMD is to provide defaults for an executing container. 92 These defaults may include an executable, or they can omit the executable. If 93 they omit the executable, an ENTRYPOINT must be specified. 94 When used in the shell or exec formats, the CMD instruction sets the command to 95 be executed when running the image. 96 If you use the shell form of the CMD, the <command> executes in /bin/sh -c: 97 **FROM ubuntu** 98 **CMD echo "This is a test." | wc -** 99 If you run <command> without a shell, then you must express the command as a 100 JSON array and give the full path to the executable. This array form is the 101 preferred form of CMD. All additional parameters must be individually expressed 102 as strings in the array: 103 **FROM ubuntu** 104 **CMD ["/usr/bin/wc","--help"]** 105 To make the container run the same executable every time, use ENTRYPOINT in 106 combination with CMD. 107 If the user specifies arguments to docker run, the specified commands override 108 the default in CMD. 109 Do not confuse **RUN** with **CMD**. RUN runs a command and commits the result. CMD 110 executes nothing at build time, but specifies the intended command for the 111 image. 112 113 **EXPOSE** 114 --**EXPOSE <port> [<port>...]** 115 The **EXPOSE** instruction informs Docker that the container listens on the 116 specified network ports at runtime. Docker uses this information to 117 interconnect containers using links, and to set up port redirection on the host 118 system. 119 120 **ENV** 121 --**ENV <key> <value>** 122 The ENV instruction sets the environment variable <key> to 123 the value <value>. This value is passed to all future 124 RUN, ENTRYPOINT, and CMD instructions. This is 125 functionally equivalent to prefixing the command with **<key>=<value>**. The 126 environment variables that are set with ENV persist when a container is run 127 from the resulting image. Use docker inspect to inspect these values, and 128 change them using docker run **--env <key>=<value>.** 129 130 Note that setting Setting **ENV DEBIAN_FRONTEND noninteractive** may cause 131 unintended consequences, because it will persist when the container is run 132 interactively, as with the following command: **docker run -t -i image bash** 133 134 **ADD** 135 --ADD has two forms: 136 **ADD <src>... <dest>** 137 **ADD ["<src>"... "<dest>"]** This form is required for paths containing 138 whitespace. 139 The ADD instruction copies new files, directories 140 or remote file URLs to the filesystem of the container at path <dest>. 141 Multiple <src> resources may be specified but if they are files or directories 142 then they must be relative to the source directory that is being built 143 (the context of the build). The <dest> is the absolute path, or path relative 144 to `WORKDIR`, into which the source is copied inside the target container. 145 All new files and directories are created with mode 0755 and with the uid 146 and gid of 0. 147 148 **COPY** 149 --COPY has two forms: 150 **COPY <src>... <dest>** 151 **COPY ["<src>"... "<dest>"]** This form is required for paths containing 152 whitespace. 153 The COPY instruction copies new files from <src> and 154 adds them to the filesystem of the container at path <dest>. The <src> must be 155 the path to a file or directory relative to the source directory that is 156 being built (the context of the build) or a remote file URL. The `<dest>` is an 157 absolute path, or a path relative to `WORKDIR`, into which the source will 158 be copied inside the target container. All new files and directories are 159 created with mode 0755 and with the uid and gid of 0. 160 161 **ENTRYPOINT** 162 --**ENTRYPOINT** has two forms: ENTRYPOINT ["executable", "param1", "param2"] 163 (This is like an exec, and is the preferred form.) ENTRYPOINT command param1 164 param2 (This is running as a shell.) An ENTRYPOINT helps you configure a 165 container that can be run as an executable. When you specify an ENTRYPOINT, 166 the whole container runs as if it was only that executable. The ENTRYPOINT 167 instruction adds an entry command that is not overwritten when arguments are 168 passed to docker run. This is different from the behavior of CMD. This allows 169 arguments to be passed to the entrypoint, for instance docker run <image> -d 170 passes the -d argument to the ENTRYPOINT. Specify parameters either in the 171 ENTRYPOINT JSON array (as in the preferred exec form above), or by using a CMD 172 statement. Parameters in the ENTRYPOINT are not overwritten by the docker run 173 arguments. Parameters specifies via CMD are overwritten by docker run 174 arguments. Specify a plain string for the ENTRYPOINT, and it will execute in 175 /bin/sh -c, like a CMD instruction: 176 FROM ubuntu 177 ENTRYPOINT wc -l - 178 This means that the Dockerfile's image always takes stdin as input (that's 179 what "-" means), and prints the number of lines (that's what "-l" means). To 180 make this optional but default, use a CMD: 181 FROM ubuntu 182 CMD ["-l", "-"] 183 ENTRYPOINT ["/usr/bin/wc"] 184 185 **VOLUME** 186 --**VOLUME ["/data"]** 187 The VOLUME instruction creates a mount point with the specified name and marks 188 it as holding externally-mounted volumes from the native host or from other 189 containers. 190 191 **USER** 192 -- **USER daemon** 193 The USER instruction sets the username or UID that is used when running the 194 image. 195 196 **WORKDIR** 197 -- **WORKDIR /path/to/workdir** 198 The WORKDIR instruction sets the working directory for the **RUN**, **CMD**, **ENTRYPOINT**, **COPY** and **ADD** Dockerfile commands that follow it. 199 It can be used multiple times in a single Dockerfile. Relative paths are defined relative to the path of the previous **WORKDIR** instruction. For example: 200 **WORKDIR /a WORKDIR b WORKDIR c RUN pwd** 201 In the above example, the output of the **pwd** command is **a/b/c**. 202 203 **ONBUILD** 204 -- **ONBUILD [INSTRUCTION]** 205 The ONBUILD instruction adds a trigger instruction to the image, which is 206 executed at a later time, when the image is used as the base for another 207 build. The trigger is executed in the context of the downstream build, as 208 if it had been inserted immediately after the FROM instruction in the 209 downstream Dockerfile. Any build instruction can be registered as a 210 trigger. This is useful if you are building an image to be 211 used as a base for building other images, for example an application build 212 environment or a daemon to be customized with a user-specific 213 configuration. For example, if your image is a reusable python 214 application builder, it requires application source code to be 215 added in a particular directory, and might require a build script 216 to be called after that. You can't just call ADD and RUN now, because 217 you don't yet have access to the application source code, and it 218 is different for each application build. Providing 219 application developers with a boilerplate Dockerfile to copy-paste 220 into their application is inefficient, error-prone, and 221 difficult to update because it mixes with application-specific code. 222 The solution is to use **ONBUILD** to register instructions in advance, to 223 run later, during the next build stage. 224 225 # HISTORY 226 *May 2014, Compiled by Zac Dover (zdover at redhat dot com) based on docker.com Dockerfile documentation.