11---
22title : Dockerfile overview
33weight : 20
4- description : Learn about Dockerfiles and how to use them with Docker Images to build and package your software
5- keywords : build, buildx, buildkit, getting started, dockerfile
4+ description : Learn how to use Dockerfiles to build and package your software into Docker images.
5+ keywords : dockerfile, docker build, buildx, buildkit, container image, getting started, image layers, dockerfile instructions
66aliases :
77- /build/hellobuild/
88- /build/building/packaging/
99---
1010
1111## Dockerfile
1212
13- It all starts with a Dockerfile.
14-
15- Docker builds images by reading the instructions from a Dockerfile. A
16- Dockerfile is a text file containing instructions for building your source
17- code. The Dockerfile instruction syntax is defined by the specification
18- reference in the [ Dockerfile reference] ( /reference/dockerfile.md ) .
13+ Docker builds images by reading instructions from a Dockerfile. A
14+ Dockerfile is a text file that contains instructions for building your source
15+ code. The Dockerfile instruction syntax is defined in the
16+ [ Dockerfile reference] ( /reference/dockerfile.md ) .
1917
2018Here are the most common types of instructions:
2119
22- | Instruction | Description |
23- | ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
24- | [ ` FROM <image> ` ] ( /reference/dockerfile.md#from ) | Defines a base for your image. |
25- | [ ` RUN <command> ` ] ( /reference/dockerfile.md#run ) | Executes any commands in a new layer on top of the current image and commits the result. ` RUN ` also has a shell form for running commands. |
26- | [ ` WORKDIR <directory> ` ] ( /reference/dockerfile.md#workdir ) | Sets the working directory for any ` RUN ` , ` CMD ` , ` ENTRYPOINT ` , ` COPY ` , and ` ADD ` instructions that follow it in the Dockerfile. |
27- | [ ` COPY <src> <dest> ` ] ( /reference/dockerfile.md#copy ) | Copies new files or directories from ` <src> ` and adds them to the filesystem of the container at the path ` <dest> ` . |
28- | [ ` CMD <command> ` ] ( /reference/dockerfile.md#cmd ) | Lets you define the default program that is run once you start the container based on this image. Each Dockerfile only has one ` CMD ` , and only the last ` CMD ` instance is respected when multiple exist. |
20+ | Instruction | Description |
21+ | -----------------------------------------------------------| ----------------------------------------------------------------------------------------------------------------------------------------|
22+ | [ ` FROM <image> ` ] ( /reference/dockerfile.md#from ) | Defines a base for your image. |
23+ | [ ` RUN <command> ` ] ( /reference/dockerfile.md#run ) | Executes commands in a new layer on top of the current image and commits the result. ` RUN ` also has a shell form for running commands. |
24+ | [ ` WORKDIR <directory> ` ] ( /reference/dockerfile.md#workdir ) | Sets the working directory for any ` RUN ` , ` CMD ` , ` ENTRYPOINT ` , ` COPY ` , and ` ADD ` instructions that follow it in the Dockerfile. |
25+ | [ ` COPY <src> <dest> ` ] ( /reference/dockerfile.md#copy ) | Copies new files or directories from ` <src> ` and adds them to the container at the path ` <dest> ` . |
26+ | [ ` CMD <command> ` ] ( /reference/dockerfile.md#cmd ) | Defines the default program that runs when you start the container. Only the last ` CMD ` in the Dockerfile is used if multiple exist. |
2927
30- Dockerfiles are crucial inputs for image builds and can facilitate automated,
31- multi-layer image builds based on your unique configurations . Dockerfiles can
32- start simple and grow with your needs to support more complex scenarios.
28+ Dockerfiles are essential for image builds and can automate multi-layer image builds
29+ based on your configuration . Dockerfiles can start simple and grow to support more
30+ complex scenarios.
3331
3432### Filename
3533
36- The default filename to use for a Dockerfile is ` Dockerfile ` , without a file
37- extension. Using the default name allows you to run the ` docker build ` command
38- without having to specify additional command flags.
34+ The default filename for a Dockerfile is ` Dockerfile ` , without a file extension. Using
35+ the default name lets you run the ` docker build ` command without extra flags.
3936
40- Some projects may need distinct Dockerfiles for specific purposes. A common
37+ Some projects need different Dockerfiles for specific purposes. A common
4138convention is to name these ` <something>.Dockerfile ` . You can specify the
42- Dockerfile filename using the ` --file ` flag for the ` docker build ` command.
43- Refer to the
44- [ ` docker build ` CLI reference] ( /reference/cli/docker/buildx/build.md#file )
45- to learn about the ` --file ` flag.
39+ Dockerfile filename using the ` --file ` flag with the ` docker build ` command. See the
40+ [ ` docker build ` CLI reference] ( /reference/cli/docker/buildx/build.md#file ) for details.
4641
4742> [ !NOTE]
48- >
49- > We recommend using the default (` Dockerfile ` ) for your project's primary
50- > Dockerfile.
43+ > We recommend using the default (` Dockerfile ` ) for your project's main Dockerfile.
5144
5245## Docker images
5346
54- Docker images consist of layers. Each layer is the result of a build
55- instruction in the Dockerfile. Layers are stacked sequentially , and each one is
56- a delta representing the changes applied to the previous layer.
47+ Docker images consist of layers. Each layer results from a build instruction in the
48+ Dockerfile. Layers are stacked in order , and each one represents changes applied to
49+ the previous layer.
5750
5851### Example
5952
60- Here's what a typical workflow for building applications with Docker looks like.
53+ A typical workflow for building applications with Docker:
6154
62- The following example code shows a small "Hello World" application written in
63- Python, using the Flask framework.
55+ The following example shows a small "Hello World" application in Python using Flask.
6456
6557``` python
6658from flask import Flask
@@ -71,15 +63,14 @@ def hello():
7163 return " Hello World!"
7264```
7365
74- In order to ship and deploy this application without Docker Build, you would
75- need to make sure that:
66+ Without Docker Build, you need to:
7667
77- - The required runtime dependencies are installed on the server
78- - The Python code gets uploaded to the server's filesystem
79- - The server starts your application, using the necessary parameters
68+ - Install the required runtime dependencies on the server.
69+ - Upload the Python code to the server's filesystem.
70+ - Start your application on the server with the necessary parameters.
8071
81- The following Dockerfile creates a container image, which has all the
82- dependencies installed and that automatically starts your application.
72+ The following Dockerfile creates a container image with all dependencies installed and
73+ automatically starts your application.
8374
8475``` dockerfile
8576# syntax=docker/dockerfile:1
@@ -98,7 +89,7 @@ EXPOSE 8000
9889CMD ["flask" , "run" , "--host" , "0.0.0.0" , "--port" , "8000" ]
9990```
10091
101- Here's a breakdown of what this Dockerfile does:
92+ This Dockerfile does the following :
10293
10394- [ Dockerfile syntax] ( #dockerfile-syntax )
10495- [ Base image] ( #base-image )
@@ -112,46 +103,40 @@ Here's a breakdown of what this Dockerfile does:
112103
113104### Dockerfile syntax
114105
115- The first line to add to a Dockerfile is a [ ` # syntax ` parser directive] ( /reference/dockerfile.md#syntax ) .
116- While optional, this directive instructs the Docker builder what syntax to use
117- when parsing the Dockerfile, and allows older Docker versions with [ BuildKit enabled] ( ../buildkit/_index.md#getting-started )
118- to use a specific [ Dockerfile frontend] ( ../buildkit/frontend.md ) before
119- starting the build. [ Parser directives] ( /reference/dockerfile.md#parser-directives )
120- must appear before any other comment, whitespace, or Dockerfile instruction in
121- your Dockerfile, and should be the first line in Dockerfiles.
106+ The first line is a [ ` # syntax ` parser directive] ( /reference/dockerfile.md#syntax ) .
107+ This optional directive tells Docker which syntax to use when parsing the Dockerfile.
108+ It lets older Docker versions with [ BuildKit enabled] ( ../buildkit/_index.md#getting-started )
109+ use a specific [ Dockerfile frontend] ( ../buildkit/frontend.md ) before starting the
110+ build. [ Parser directives] ( /reference/dockerfile.md#parser-directives ) must appear
111+ before any other comment, whitespace, or instruction, and should be the first line.
122112
123113``` dockerfile
124114# syntax=docker/dockerfile:1
125115```
126116
127117> [ !TIP]
128- >
129- > We recommend using ` docker/dockerfile:1 ` , which always points to the latest
130- > release of the version 1 syntax. BuildKit automatically checks for updates of
131- > the syntax before building, making sure you are using the most current version.
118+ > Use ` docker/dockerfile:1 ` to always get the latest version 1 syntax. BuildKit
119+ > checks for updates before building, so you use the most current version.
132120
133121### Base image
134122
135- The line following the syntax directive defines what base image to use :
123+ The next line defines the base image:
136124
137125``` dockerfile
138126FROM ubuntu:22.04
139127```
140128
141129The [ ` FROM ` instruction] ( /reference/dockerfile.md#from ) sets your base
142- image to the 22.04 release of Ubuntu. All instructions that follow are executed
143- in this base image: an Ubuntu environment. The notation ` ubuntu:22.04 ` , follows
144- the ` name:tag ` standard for naming Docker images. When you build images, you
145- use this notation to name your images. There are many public images you can
146- leverage in your projects, by importing them into your build steps using the
147- Dockerfile ` FROM ` instruction.
130+ image to Ubuntu 22.04. All following instructions run in this Ubuntu environment. The
131+ ` ubuntu:22.04 ` notation follows the ` name:tag ` standard for Docker images. You can
132+ use many public images in your projects by importing them with the ` FROM ` instruction.
148133
149134[ Docker Hub] ( https://hub.docker.com/search?image_filter=official&q=&type=image )
150- contains a large set of official images that you can use for this purpose .
135+ offers many official images you can use.
151136
152137### Environment setup
153138
154- The following line executes a build command inside the base image.
139+ This line runs a build command inside the base image.
155140
156141``` dockerfile
157142# install app dependencies
@@ -164,23 +149,17 @@ the container.
164149
165150### Comments
166151
167- Note the ` # install app dependencies ` line. This is a comment. Comments in
168- Dockerfiles begin with the ` # ` symbol. As your Dockerfile evolves, comments can
169- be instrumental to document how your Dockerfile works for any future readers
170- and editors of the file, including your future self!
152+ The ` # install app dependencies ` line is a comment. Comments in Dockerfiles start
153+ with ` # ` . Comments help document your Dockerfile for future readers and editors.
171154
172155> [ !NOTE]
173- >
174- > You might've noticed that comments are denoted using the same symbol as the
175- > [ syntax directive] ( #dockerfile-syntax ) on the first line of the file.
176- > The symbol is only interpreted as a directive if the pattern matches a
177- > directive and appears at the beginning of the Dockerfile. Otherwise, it's
178- > treated as a comment.
156+ > Comments use the same symbol as the [ syntax directive] ( #dockerfile-syntax ) on the
157+ > first line. The symbol is only a directive if it matches a directive pattern and is
158+ > at the start of the Dockerfile. Otherwise, it is a comment.
179159
180160### Installing dependencies
181161
182- The second ` RUN ` instruction installs the ` flask ` dependency required by the
183- Python application.
162+ The second ` RUN ` instruction installs the ` flask ` dependency for the Python app.
184163
185164``` dockerfile
186165RUN pip install flask==3.0.*
@@ -194,7 +173,7 @@ use the command to install the flask web framework.
194173
195174The next instruction uses the
196175[ ` COPY ` instruction] ( /reference/dockerfile.md#copy ) to copy the
197- ` hello.py ` file from the local build context into the root directory of our image.
176+ ` hello.py ` file from the local build context into the root directory of our image.
198177
199178``` dockerfile
200179COPY hello.py /
@@ -215,9 +194,8 @@ in your Docker build using the [`ENV` instruction](/reference/dockerfile.md#env)
215194ENV FLASK_APP=hello
216195```
217196
218- This sets a Linux environment variable we'll need later. Flask, the framework
219- used in this example, uses this variable to start the application. Without this,
220- flask wouldn't know where to find our application to be able to run it.
197+ This sets a Linux environment variable needed by Flask to start the app. Without this,
198+ Flask cannot find the app to run it.
221199
222200### Exposed ports
223201
0 commit comments