github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/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 <src>... <dest>** The ADD instruction copies new files, directories 136 or remote file URLs to the filesystem of the container at path <dest>. 137 Mutliple <src> resources may be specified but if they are files or directories 138 then they must be relative to the source directory that is being built 139 (the context of the build). The <dest> is the absolute path, or path relative 140 to `WORKDIR`, into which the source is copied inside the target container. 141 All new files and directories are created with mode 0755 and with the uid 142 and gid of 0. 143 144 **COPY** 145 --**COPY <src> <dest>** The COPY instruction copies new files from <src> and 146 adds them to the filesystem of the container at path <dest>. The <src> must be 147 the path to a file or directory relative to the source directory that is 148 being built (the context of the build) or a remote file URL. The `<dest>` is an 149 absolute path, or a path relative to `WORKDIR`, into which the source will 150 be copied inside the target container. All new files and directories are 151 created with mode 0755 and with the uid and gid of 0. 152 153 **ENTRYPOINT** 154 --**ENTRYPOINT** has two forms: ENTRYPOINT ["executable", "param1", "param2"] 155 (This is like an exec, and is the preferred form.) ENTRYPOINT command param1 156 param2 (This is running as a shell.) An ENTRYPOINT helps you configure a 157 container that can be run as an executable. When you specify an ENTRYPOINT, 158 the whole container runs as if it was only that executable. The ENTRYPOINT 159 instruction adds an entry command that is not overwritten when arguments are 160 passed to docker run. This is different from the behavior of CMD. This allows 161 arguments to be passed to the entrypoint, for instance docker run <image> -d 162 passes the -d argument to the ENTRYPOINT. Specify parameters either in the 163 ENTRYPOINT JSON array (as in the preferred exec form above), or by using a CMD 164 statement. Parameters in the ENTRYPOINT are not overwritten by the docker run 165 arguments. Parameters specifies via CMD are overwritten by docker run 166 arguments. Specify a plain string for the ENTRYPOINT, and it will execute in 167 /bin/sh -c, like a CMD instruction: 168 FROM ubuntu 169 ENTRYPOINT wc -l - 170 This means that the Dockerfile's image always takes stdin as input (that's 171 what "-" means), and prints the number of lines (that's what "-l" means). To 172 make this optional but default, use a CMD: 173 FROM ubuntu 174 CMD ["-l", "-"] 175 ENTRYPOINT ["/usr/bin/wc"] 176 177 **VOLUME** 178 --**VOLUME ["/data"]** 179 The VOLUME instruction creates a mount point with the specified name and marks 180 it as holding externally-mounted volumes from the native host or from other 181 containers. 182 183 **USER** 184 -- **USER daemon** 185 The USER instruction sets the username or UID that is used when running the 186 image. 187 188 **WORKDIR** 189 -- **WORKDIR /path/to/workdir** 190 The WORKDIR instruction sets the working directory for the **RUN**, **CMD**, and **ENTRYPOINT** Dockerfile commands that follow it. 191 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: 192 **WORKDIR /a WORKDIR b WORKDIR c RUN pwd** 193 In the above example, the output of the **pwd** command is **a/b/c**. 194 195 **ONBUILD** 196 -- **ONBUILD [INSTRUCTION]** 197 The ONBUILD instruction adds a trigger instruction to the image, which is 198 executed at a later time, when the image is used as the base for another 199 build. The trigger is executed in the context of the downstream build, as 200 if it had been inserted immediately after the FROM instruction in the 201 downstream Dockerfile. Any build instruction can be registered as a 202 trigger. This is useful if you are building an image to be 203 used as a base for building other images, for example an application build 204 environment or a daemon to be customized with a user-specific 205 configuration. For example, if your image is a reusable python 206 application builder, it requires application source code to be 207 added in a particular directory, and might require a build script 208 to be called after that. You can't just call ADD and RUN now, because 209 you don't yet have access to the application source code, and it 210 is different for each application build. Providing 211 application developers with a boilerplate Dockerfile to copy-paste 212 into their application is inefficient, error-prone, and 213 difficult to update because it mixes with application-specific code. 214 The solution is to use **ONBUILD** to register instructions in advance, to 215 run later, during the next build stage. 216 217 # HISTORY 218 *May 2014, Compiled by Zac Dover (zdover at redhat dot com) based on docker.com Dockerfile documentation.