github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/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 **STOPSIGNAL** 187 188 -- `STOPSIGNAL <signal>` 189 The **STOPSIGNAL** instruction sets the system call signal that will be sent 190 to the container to exit. This signal can be a signal name in the format 191 **SIG<NAME>**, for instance **SIGKILL**, or an unsigned number that matches a 192 position in the kernel's syscall table, for instance **9**. The default is 193 **SIGTERM** if not defined. 194 195 The image's default stopsignal can be overridden per container, using the 196 **--stop-signal** flag on **docker-run(1)** and **docker-create(1)**. 197 198 **EXPOSE** 199 -- `EXPOSE <port> [<port>...]` 200 The **EXPOSE** instruction informs Docker that the container listens on the 201 specified network ports at runtime. Docker uses this information to 202 interconnect containers using links and to set up port redirection on the host 203 system. 204 205 **ENV** 206 -- `ENV <key> <value>` 207 The **ENV** instruction sets the environment variable <key> to 208 the value `<value>`. This value is passed to all future 209 **RUN**, **ENTRYPOINT**, and **CMD** instructions. This is 210 functionally equivalent to prefixing the command with `<key>=<value>`. The 211 environment variables that are set with **ENV** persist when a container is run 212 from the resulting image. Use `docker inspect` to inspect these values, and 213 change them using `docker run --env <key>=<value>`. 214 215 Note that setting "`ENV DEBIAN_FRONTEND=noninteractive`" may cause 216 unintended consequences, because it will persist when the container is run 217 interactively, as with the following command: `docker run -t -i image bash` 218 219 **ADD** 220 -- **ADD** has two forms: 221 222 ``` 223 ADD <src> <dest> 224 225 # Required for paths with whitespace 226 ADD ["<src>",... "<dest>"] 227 ``` 228 229 The **ADD** instruction copies new files, directories 230 or remote file URLs to the filesystem of the container at path `<dest>`. 231 Multiple `<src>` resources may be specified but if they are files or directories 232 then they must be relative to the source directory that is being built 233 (the context of the build). The `<dest>` is the absolute path, or path relative 234 to **WORKDIR**, into which the source is copied inside the target container. 235 If the `<src>` argument is a local file in a recognized compression format 236 (tar, gzip, bzip2, etc) then it is unpacked at the specified `<dest>` in the 237 container's filesystem. Note that only local compressed files will be unpacked, 238 i.e., the URL download and archive unpacking features cannot be used together. 239 All new directories are created with mode 0755 and with the uid and gid of **0**. 240 241 **COPY** 242 -- **COPY** has two forms: 243 244 ``` 245 COPY <src> <dest> 246 247 # Required for paths with whitespace 248 COPY ["<src>",... "<dest>"] 249 ``` 250 251 The **COPY** instruction copies new files from `<src>` and 252 adds them to the filesystem of the container at path <dest>. The `<src>` must be 253 the path to a file or directory relative to the source directory that is 254 being built (the context of the build) or a remote file URL. The `<dest>` is an 255 absolute path, or a path relative to **WORKDIR**, into which the source will 256 be copied inside the target container. If you **COPY** an archive file it will 257 land in the container exactly as it appears in the build context without any 258 attempt to unpack it. All new files and directories are created with mode **0755** 259 and with the uid and gid of **0**. 260 261 **ENTRYPOINT** 262 -- **ENTRYPOINT** has two forms: 263 264 ``` 265 # executable form 266 ENTRYPOINT ["executable", "param1", "param2"]` 267 268 # run command in a shell - /bin/sh -c 269 ENTRYPOINT command param1 param2 270 ``` 271 272 -- An **ENTRYPOINT** helps you configure a 273 container that can be run as an executable. When you specify an **ENTRYPOINT**, 274 the whole container runs as if it was only that executable. The **ENTRYPOINT** 275 instruction adds an entry command that is not overwritten when arguments are 276 passed to docker run. This is different from the behavior of **CMD**. This allows 277 arguments to be passed to the entrypoint, for instance `docker run <image> -d` 278 passes the -d argument to the **ENTRYPOINT**. Specify parameters either in the 279 **ENTRYPOINT** JSON array (as in the preferred exec form above), or by using a **CMD** 280 statement. Parameters in the **ENTRYPOINT** are not overwritten by the docker run 281 arguments. Parameters specified via **CMD** are overwritten by docker run 282 arguments. Specify a plain string for the **ENTRYPOINT**, and it will execute in 283 `/bin/sh -c`, like a **CMD** instruction: 284 285 ``` 286 FROM ubuntu 287 ENTRYPOINT wc -l - 288 ``` 289 290 This means that the Dockerfile's image always takes stdin as input (that's 291 what "-" means), and prints the number of lines (that's what "-l" means). To 292 make this optional but default, use a **CMD**: 293 294 ``` 295 FROM ubuntu 296 CMD ["-l", "-"] 297 ENTRYPOINT ["/usr/bin/wc"] 298 ``` 299 300 **VOLUME** 301 -- `VOLUME ["/data"]` 302 The **VOLUME** instruction creates a mount point with the specified name and marks 303 it as holding externally-mounted volumes from the native host or from other 304 containers. 305 306 **USER** 307 -- `USER daemon` 308 Sets the username or UID used for running subsequent commands. 309 310 The **USER** instruction can optionally be used to set the group or GID. The 311 followings examples are all valid: 312 USER [user | user:group | uid | uid:gid | user:gid | uid:group ] 313 314 Until the **USER** instruction is set, instructions will be run as root. The USER 315 instruction can be used any number of times in a Dockerfile, and will only affect 316 subsequent commands. 317 318 **WORKDIR** 319 -- `WORKDIR /path/to/workdir` 320 The **WORKDIR** instruction sets the working directory for the **RUN**, **CMD**, 321 **ENTRYPOINT**, **COPY** and **ADD** Dockerfile commands that follow it. It can 322 be used multiple times in a single Dockerfile. Relative paths are defined 323 relative to the path of the previous **WORKDIR** instruction. For example: 324 325 ``` 326 WORKDIR /a 327 WORKDIR b 328 WORKDIR c 329 RUN pwd 330 ``` 331 332 In the above example, the output of the **pwd** command is **a/b/c**. 333 334 **ARG** 335 -- ARG <name>[=<default value>] 336 337 The `ARG` instruction defines a variable that users can pass at build-time to 338 the builder with the `docker build` command using the `--build-arg 339 <varname>=<value>` flag. If a user specifies a build argument that was not 340 defined in the Dockerfile, the build outputs a warning. 341 342 ``` 343 [Warning] One or more build-args [foo] were not consumed 344 ``` 345 346 The Dockerfile author can define a single variable by specifying `ARG` once or many 347 variables by specifying `ARG` more than once. For example, a valid Dockerfile: 348 349 ``` 350 FROM busybox 351 ARG user1 352 ARG buildno 353 ... 354 ``` 355 356 A Dockerfile author may optionally specify a default value for an `ARG` instruction: 357 358 ``` 359 FROM busybox 360 ARG user1=someuser 361 ARG buildno=1 362 ... 363 ``` 364 365 If an `ARG` value has a default and if there is no value passed at build-time, the 366 builder uses the default. 367 368 An `ARG` variable definition comes into effect from the line on which it is 369 defined in the `Dockerfile` not from the argument's use on the command-line or 370 elsewhere. For example, consider this Dockerfile: 371 372 ``` 373 1 FROM busybox 374 2 USER ${user:-some_user} 375 3 ARG user 376 4 USER $user 377 ... 378 ``` 379 A user builds this file by calling: 380 381 ``` 382 $ docker build --build-arg user=what_user Dockerfile 383 ``` 384 385 The `USER` at line 2 evaluates to `some_user` as the `user` variable is defined on the 386 subsequent line 3. The `USER` at line 4 evaluates to `what_user` as `user` is 387 defined and the `what_user` value was passed on the command line. Prior to its definition by an 388 `ARG` instruction, any use of a variable results in an empty string. 389 390 > **Warning:** It is not recommended to use build-time variables for 391 > passing secrets like github keys, user credentials etc. Build-time variable 392 > values are visible to any user of the image with the `docker history` command. 393 394 You can use an `ARG` or an `ENV` instruction to specify variables that are 395 available to the `RUN` instruction. Environment variables defined using the 396 `ENV` instruction always override an `ARG` instruction of the same name. Consider 397 this Dockerfile with an `ENV` and `ARG` instruction. 398 399 ``` 400 1 FROM ubuntu 401 2 ARG CONT_IMG_VER 402 3 ENV CONT_IMG_VER=v1.0.0 403 4 RUN echo $CONT_IMG_VER 404 ``` 405 Then, assume this image is built with this command: 406 407 ``` 408 $ docker build --build-arg CONT_IMG_VER=v2.0.1 Dockerfile 409 ``` 410 411 In this case, the `RUN` instruction uses `v1.0.0` instead of the `ARG` setting 412 passed by the user:`v2.0.1` This behavior is similar to a shell 413 script where a locally scoped variable overrides the variables passed as 414 arguments or inherited from environment, from its point of definition. 415 416 Using the example above but a different `ENV` specification you can create more 417 useful interactions between `ARG` and `ENV` instructions: 418 419 ``` 420 1 FROM ubuntu 421 2 ARG CONT_IMG_VER 422 3 ENV CONT_IMG_VER=${CONT_IMG_VER:-v1.0.0} 423 4 RUN echo $CONT_IMG_VER 424 ``` 425 426 Unlike an `ARG` instruction, `ENV` values are always persisted in the built 427 image. Consider a docker build without the --build-arg flag: 428 429 ``` 430 $ docker build Dockerfile 431 ``` 432 433 Using this Dockerfile example, `CONT_IMG_VER` is still persisted in the image but 434 its value would be `v1.0.0` as it is the default set in line 3 by the `ENV` instruction. 435 436 The variable expansion technique in this example allows you to pass arguments 437 from the command line and persist them in the final image by leveraging the 438 `ENV` instruction. Variable expansion is only supported for [a limited set of 439 Dockerfile instructions.](#environment-replacement) 440 441 Docker has a set of predefined `ARG` variables that you can use without a 442 corresponding `ARG` instruction in the Dockerfile. 443 444 * `HTTP_PROXY` 445 * `http_proxy` 446 * `HTTPS_PROXY` 447 * `https_proxy` 448 * `FTP_PROXY` 449 * `ftp_proxy` 450 * `NO_PROXY` 451 * `no_proxy` 452 453 To use these, pass them on the command line using `--build-arg` flag, for 454 example: 455 456 ``` 457 $ docker build --build-arg HTTPS_PROXY=https://my-proxy.example.com . 458 ``` 459 460 **ONBUILD** 461 -- `ONBUILD [INSTRUCTION]` 462 The **ONBUILD** instruction adds a trigger instruction to an image. The 463 trigger is executed at a later time, when the image is used as the base for 464 another build. Docker executes the trigger in the context of the downstream 465 build, as if the trigger existed immediately after the **FROM** instruction in 466 the downstream Dockerfile. 467 468 You can register any build instruction as a trigger. A trigger is useful if 469 you are defining an image to use as a base for building other images. For 470 example, if you are defining an application build environment or a daemon that 471 is customized with a user-specific configuration. 472 473 Consider an image intended as a reusable python application builder. It must 474 add application source code to a particular directory, and might need a build 475 script called after that. You can't just call **ADD** and **RUN** now, because 476 you don't yet have access to the application source code, and it is different 477 for each application build. 478 479 -- Providing application developers with a boilerplate Dockerfile to copy-paste 480 into their application is inefficient, error-prone, and 481 difficult to update because it mixes with application-specific code. 482 The solution is to use **ONBUILD** to register instructions in advance, to 483 run later, during the next build stage. 484 485 # HISTORY 486 *May 2014, Compiled by Zac Dover (zdover at redhat dot com) based on docker.com Dockerfile documentation. 487 *Feb 2015, updated by Brian Goff (cpuguy83@gmail.com) for readability 488 *Sept 2015, updated by Sally O'Malley (somalley@redhat.com) 489 *Oct 2016, updated by Addam Hardy (addam.hardy@gmail.com)