github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/man/Dockerfile.5.md (about) 1 % "DOCKERFILE" "5" "MAY 2014" "Docker Community" "Docker User Manuals" 2 3 # NAME 4 5 Dockerfile - automate the steps of creating a Docker image 6 7 # INTRODUCTION 8 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**. 13 14 The **Dockerfile** describes the steps taken to assemble the image. When the 15 **Dockerfile** has been created, call the `docker build` command, using the 16 path of directory that contains **Dockerfile** as the argument. 17 18 # SYNOPSIS 19 20 INSTRUCTION arguments 21 22 For example: 23 24 FROM image 25 26 # DESCRIPTION 27 28 A Dockerfile is a file that automates the steps of creating a Docker image. 29 A Dockerfile is similar to a Makefile. 30 31 # USAGE 32 33 docker build . 34 35 -- Runs the steps and commits them, building a final image. 36 The path to the source repository defines where to find the context of the 37 build. The build is run by the Docker daemon, not the CLI. The whole 38 context must be transferred to the daemon. The Docker CLI reports 39 `"Sending build context to Docker daemon"` when the context is sent to the 40 daemon. 41 42 ``` 43 docker build -t repository/tag . 44 ``` 45 46 -- specifies a repository and tag at which to save the new image if the build 47 succeeds. The Docker daemon runs the steps one-by-one, committing the result 48 to a new image if necessary, before finally outputting the ID of the new 49 image. The Docker daemon automatically cleans up the context it is given. 50 51 Docker re-uses intermediate images whenever possible. This significantly 52 accelerates the *docker build* process. 53 54 # FORMAT 55 56 `FROM image` 57 58 `FROM image:tag` 59 60 `FROM image@digest` 61 62 -- The **FROM** instruction sets the base image for subsequent instructions. A 63 valid Dockerfile must have **FROM** as its first instruction. The image can be any 64 valid image. It is easy to start by pulling an image from the public 65 repositories. 66 67 -- **FROM** must be the first non-comment instruction in Dockerfile. 68 69 -- **FROM** may appear multiple times within a single Dockerfile in order to create 70 multiple images. Make a note of the last image ID output by the commit before 71 each new **FROM** command. 72 73 -- If no tag is given to the **FROM** instruction, Docker applies the 74 `latest` tag. If the used tag does not exist, an error is returned. 75 76 -- If no digest is given to the **FROM** instruction, Docker applies the 77 `latest` tag. If the used tag does not exist, an error is returned. 78 79 **MAINTAINER** 80 -- **MAINTAINER** sets the Author field for the generated images. 81 Useful for providing users with an email or url for support. 82 83 **RUN** 84 -- **RUN** has two forms: 85 86 ``` 87 # the command is run in a shell - /bin/sh -c 88 RUN <command> 89 90 # Executable form 91 RUN ["executable", "param1", "param2"] 92 ``` 93 94 95 -- The **RUN** instruction executes any commands in a new layer on top of the current 96 image and commits the results. The committed image is used for the next step in 97 Dockerfile. 98 99 -- Layering **RUN** instructions and generating commits conforms to the core 100 concepts of Docker where commits are cheap and containers can be created from 101 any point in the history of an image. This is similar to source control. The 102 exec form makes it possible to avoid shell string munging. The exec form makes 103 it possible to **RUN** commands using a base image that does not contain `/bin/sh`. 104 105 Note that the exec form is parsed as a JSON array, which means that you must 106 use double-quotes (") around words not single-quotes ('). 107 108 **CMD** 109 -- **CMD** has three forms: 110 111 ``` 112 # Executable form 113 CMD ["executable", "param1", "param2"]` 114 115 # Provide default arguments to ENTRYPOINT 116 CMD ["param1", "param2"]` 117 118 # the command is run in a shell - /bin/sh -c 119 CMD command param1 param2 120 ``` 121 122 -- There should be only one **CMD** in a Dockerfile. If more than one **CMD** is listed, only 123 the last **CMD** takes effect. 124 The main purpose of a **CMD** is to provide defaults for an executing container. 125 These defaults may include an executable, or they can omit the executable. If 126 they omit the executable, an **ENTRYPOINT** must be specified. 127 When used in the shell or exec formats, the **CMD** instruction sets the command to 128 be executed when running the image. 129 If you use the shell form of the **CMD**, the `<command>` executes in `/bin/sh -c`: 130 131 Note that the exec form is parsed as a JSON array, which means that you must 132 use double-quotes (") around words not single-quotes ('). 133 134 ``` 135 FROM ubuntu 136 CMD echo "This is a test." | wc - 137 ``` 138 139 -- If you run **command** without a shell, then you must express the command as a 140 JSON array and give the full path to the executable. This array form is the 141 preferred form of **CMD**. All additional parameters must be individually expressed 142 as strings in the array: 143 144 ``` 145 FROM ubuntu 146 CMD ["/usr/bin/wc","--help"] 147 ``` 148 149 -- To make the container run the same executable every time, use **ENTRYPOINT** in 150 combination with **CMD**. 151 If the user specifies arguments to `docker run`, the specified commands 152 override the default in **CMD**. 153 Do not confuse **RUN** with **CMD**. **RUN** runs a command and commits the result. 154 **CMD** executes nothing at build time, but specifies the intended command for 155 the image. 156 157 **LABEL** 158 -- `LABEL <key>=<value> [<key>=<value> ...]`or 159 ``` 160 LABEL <key>[ <value>] 161 LABEL <key>[ <value>] 162 ... 163 ``` 164 The **LABEL** instruction adds metadata to an image. A **LABEL** is a 165 key-value pair. To specify a **LABEL** without a value, simply use an empty 166 string. To include spaces within a **LABEL** value, use quotes and 167 backslashes as you would in command-line parsing. 168 169 ``` 170 LABEL com.example.vendor="ACME Incorporated" 171 LABEL com.example.vendor "ACME Incorporated" 172 LABEL com.example.vendor.is-beta "" 173 LABEL com.example.vendor.is-beta= 174 LABEL com.example.vendor.is-beta="" 175 ``` 176 177 An image can have more than one label. To specify multiple labels, separate 178 each key-value pair by a space. 179 180 Labels are additive including `LABEL`s in `FROM` images. As the system 181 encounters and then applies a new label, new `key`s override any previous 182 labels with identical keys. 183 184 To display an image's labels, use the `docker inspect` command. 185 186 **EXPOSE** 187 -- `EXPOSE <port> [<port>...]` 188 The **EXPOSE** instruction informs Docker that the container listens on the 189 specified network ports at runtime. Docker uses this information to 190 interconnect containers using links and to set up port redirection on the host 191 system. 192 193 **ENV** 194 -- `ENV <key> <value>` 195 The **ENV** instruction sets the environment variable <key> to 196 the value `<value>`. This value is passed to all future 197 **RUN**, **ENTRYPOINT**, and **CMD** instructions. This is 198 functionally equivalent to prefixing the command with `<key>=<value>`. The 199 environment variables that are set with **ENV** persist when a container is run 200 from the resulting image. Use `docker inspect` to inspect these values, and 201 change them using `docker run --env <key>=<value>`. 202 203 Note that setting "`ENV DEBIAN_FRONTEND=noninteractive`" may cause 204 unintended consequences, because it will persist when the container is run 205 interactively, as with the following command: `docker run -t -i image bash` 206 207 **ADD** 208 -- **ADD** has two forms: 209 210 ``` 211 ADD <src> <dest> 212 213 # Required for paths with whitespace 214 ADD ["<src>",... "<dest>"] 215 ``` 216 217 The **ADD** instruction copies new files, directories 218 or remote file URLs to the filesystem of the container at path `<dest>`. 219 Multiple `<src>` resources may be specified but if they are files or directories 220 then they must be relative to the source directory that is being built 221 (the context of the build). The `<dest>` is the absolute path, or path relative 222 to **WORKDIR**, into which the source is copied inside the target container. 223 If the `<src>` argument is a local file in a recognized compression format 224 (tar, gzip, bzip2, etc) then it is unpacked at the specified `<dest>` in the 225 container's filesystem. Note that only local compressed files will be unpacked, 226 i.e., the URL download and archive unpacking features cannot be used together. 227 All new directories are created with mode 0755 and with the uid and gid of **0**. 228 229 **COPY** 230 -- **COPY** has two forms: 231 232 ``` 233 COPY <src> <dest> 234 235 # Required for paths with whitespace 236 COPY ["<src>",... "<dest>"] 237 ``` 238 239 The **COPY** instruction copies new files from `<src>` and 240 adds them to the filesystem of the container at path <dest>. The `<src>` must be 241 the path to a file or directory relative to the source directory that is 242 being built (the context of the build) or a remote file URL. The `<dest>` is an 243 absolute path, or a path relative to **WORKDIR**, into which the source will 244 be copied inside the target container. If you **COPY** an archive file it will 245 land in the container exactly as it appears in the build context without any 246 attempt to unpack it. All new files and directories are created with mode **0755** 247 and with the uid and gid of **0**. 248 249 **ENTRYPOINT** 250 -- **ENTRYPOINT** has two forms: 251 252 ``` 253 # executable form 254 ENTRYPOINT ["executable", "param1", "param2"]` 255 256 # run command in a shell - /bin/sh -c 257 ENTRYPOINT command param1 param2 258 ``` 259 260 -- An **ENTRYPOINT** helps you configure a 261 container that can be run as an executable. When you specify an **ENTRYPOINT**, 262 the whole container runs as if it was only that executable. The **ENTRYPOINT** 263 instruction adds an entry command that is not overwritten when arguments are 264 passed to docker run. This is different from the behavior of **CMD**. This allows 265 arguments to be passed to the entrypoint, for instance `docker run <image> -d` 266 passes the -d argument to the **ENTRYPOINT**. Specify parameters either in the 267 **ENTRYPOINT** JSON array (as in the preferred exec form above), or by using a **CMD** 268 statement. Parameters in the **ENTRYPOINT** are not overwritten by the docker run 269 arguments. Parameters specified via **CMD** are overwritten by docker run 270 arguments. Specify a plain string for the **ENTRYPOINT**, and it will execute in 271 `/bin/sh -c`, like a **CMD** instruction: 272 273 ``` 274 FROM ubuntu 275 ENTRYPOINT wc -l - 276 ``` 277 278 This means that the Dockerfile's image always takes stdin as input (that's 279 what "-" means), and prints the number of lines (that's what "-l" means). To 280 make this optional but default, use a **CMD**: 281 282 ``` 283 FROM ubuntu 284 CMD ["-l", "-"] 285 ENTRYPOINT ["/usr/bin/wc"] 286 ``` 287 288 **VOLUME** 289 -- `VOLUME ["/data"]` 290 The **VOLUME** instruction creates a mount point with the specified name and marks 291 it as holding externally-mounted volumes from the native host or from other 292 containers. 293 294 **USER** 295 -- `USER daemon` 296 Sets the username or UID used for running subsequent commands. 297 298 The **USER** instruction can optionally be used to set the group or GID. The 299 followings examples are all valid: 300 USER [user | user:group | uid | uid:gid | user:gid | uid:group ] 301 302 Until the **USER** instruction is set, instructions will be run as root. The USER 303 instruction can be used any number of times in a Dockerfile, and will only affect 304 subsequent commands. 305 306 **WORKDIR** 307 -- `WORKDIR /path/to/workdir` 308 The **WORKDIR** instruction sets the working directory for the **RUN**, **CMD**, 309 **ENTRYPOINT**, **COPY** and **ADD** Dockerfile commands that follow it. It can 310 be used multiple times in a single Dockerfile. Relative paths are defined 311 relative to the path of the previous **WORKDIR** instruction. For example: 312 313 ``` 314 WORKDIR /a 315 WORKDIR b 316 WORKDIR c 317 RUN pwd 318 ``` 319 320 In the above example, the output of the **pwd** command is **a/b/c**. 321 322 **ARG** 323 -- ARG <name>[=<default value>] 324 325 The `ARG` instruction defines a variable that users can pass at build-time to 326 the builder with the `docker build` command using the `--build-arg 327 <varname>=<value>` flag. If a user specifies a build argument that was not 328 defined in the Dockerfile, the build outputs a warning. 329 330 ``` 331 [Warning] One or more build-args [foo] were not consumed 332 ``` 333 334 The Dockerfile author can define a single variable by specifying `ARG` once or many 335 variables by specifying `ARG` more than once. For example, a valid Dockerfile: 336 337 ``` 338 FROM busybox 339 ARG user1 340 ARG buildno 341 ... 342 ``` 343 344 A Dockerfile author may optionally specify a default value for an `ARG` instruction: 345 346 ``` 347 FROM busybox 348 ARG user1=someuser 349 ARG buildno=1 350 ... 351 ``` 352 353 If an `ARG` value has a default and if there is no value passed at build-time, the 354 builder uses the default. 355 356 An `ARG` variable definition comes into effect from the line on which it is 357 defined in the `Dockerfile` not from the argument's use on the command-line or 358 elsewhere. For example, consider this Dockerfile: 359 360 ``` 361 1 FROM busybox 362 2 USER ${user:-some_user} 363 3 ARG user 364 4 USER $user 365 ... 366 ``` 367 A user builds this file by calling: 368 369 ``` 370 $ docker build --build-arg user=what_user Dockerfile 371 ``` 372 373 The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the 374 subsequent line 3. The `USER` at line 4 evaluates to `what_user` as `user` is 375 defined and the `what_user` value was passed on the command line. Prior to its definition by an 376 `ARG` instruction, any use of a variable results in an empty string. 377 378 > **Warning:** It is not recommended to use build-time variables for 379 > passing secrets like github keys, user credentials etc. Build-time variable 380 > values are visible to any user of the image with the `docker history` command. 381 382 You can use an `ARG` or an `ENV` instruction to specify variables that are 383 available to the `RUN` instruction. Environment variables defined using the 384 `ENV` instruction always override an `ARG` instruction of the same name. Consider 385 this Dockerfile with an `ENV` and `ARG` instruction. 386 387 ``` 388 1 FROM ubuntu 389 2 ARG CONT_IMG_VER 390 3 ENV CONT_IMG_VER=v1.0.0 391 4 RUN echo $CONT_IMG_VER 392 ``` 393 Then, assume this image is built with this command: 394 395 ``` 396 $ docker build --build-arg CONT_IMG_VER=v2.0.1 Dockerfile 397 ``` 398 399 In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting 400 passed by the user:`v2.0.1` This behavior is similar to a shell 401 script where a locally scoped variable overrides the variables passed as 402 arguments or inherited from environment, from its point of definition. 403 404 Using the example above but a different `ENV` specification you can create more 405 useful interactions between `ARG` and `ENV` instructions: 406 407 ``` 408 1 FROM ubuntu 409 2 ARG CONT_IMG_VER 410 3 ENV CONT_IMG_VER=${CONT_IMG_VER:-v1.0.0} 411 4 RUN echo $CONT_IMG_VER 412 ``` 413 414 Unlike an `ARG` instruction, `ENV` values are always persisted in the built 415 image. Consider a docker build without the --build-arg flag: 416 417 ``` 418 $ docker build Dockerfile 419 ``` 420 421 Using this Dockerfile example, `CONT_IMG_VER` is still persisted in the image but 422 its value would be `v1.0.0` as it is the default set in line 3 by the `ENV` instruction. 423 424 The variable expansion technique in this example allows you to pass arguments 425 from the command line and persist them in the final image by leveraging the 426 `ENV` instruction. Variable expansion is only supported for [a limited set of 427 Dockerfile instructions.](#environment-replacement) 428 429 Docker has a set of predefined `ARG` variables that you can use without a 430 corresponding `ARG` instruction in the Dockerfile. 431 432 * `HTTP_PROXY` 433 * `http_proxy` 434 * `HTTPS_PROXY` 435 * `https_proxy` 436 * `FTP_PROXY` 437 * `ftp_proxy` 438 * `NO_PROXY` 439 * `no_proxy` 440 441 To use these, simply pass them on the command line using the `--build-arg 442 <varname>=<value>` flag. 443 444 **ONBUILD** 445 -- `ONBUILD [INSTRUCTION]` 446 The **ONBUILD** instruction adds a trigger instruction to an image. The 447 trigger is executed at a later time, when the image is used as the base for 448 another build. Docker executes the trigger in the context of the downstream 449 build, as if the trigger existed immediately after the **FROM** instruction in 450 the downstream Dockerfile. 451 452 You can register any build instruction as a trigger. A trigger is useful if 453 you are defining an image to use as a base for building other images. For 454 example, if you are defining an application build environment or a daemon that 455 is customized with a user-specific configuration. 456 457 Consider an image intended as a reusable python application builder. It must 458 add application source code to a particular directory, and might need a build 459 script called after that. You can't just call **ADD** and **RUN** now, because 460 you don't yet have access to the application source code, and it is different 461 for each application build. 462 463 -- Providing application developers with a boilerplate Dockerfile to copy-paste 464 into their application is inefficient, error-prone, and 465 difficult to update because it mixes with application-specific code. 466 The solution is to use **ONBUILD** to register instructions in advance, to 467 run later, during the next build stage. 468 469 # HISTORY 470 *May 2014, Compiled by Zac Dover (zdover at redhat dot com) based on docker.com Dockerfile documentation. 471 *Feb 2015, updated by Brian Goff (cpuguy83@gmail.com) for readability 472 *Sept 2015, updated by Sally O'Malley (somalley@redhat.com) 473 *Oct 2016, updated by Addam Hardy (addam.hardy@gmail.com)