Packaging your software
It all starts with a Dockerfile.
Docker builds images by reading the instructions from a Dockerfile. A Dockerfile is a text file containing instructions for building your source code. The Dockerfile instruction syntax is defined by the specification reference in the Dockerfile reference.
Here are the most common types of instructions:
|Defines a base for your image.|
|Executes any commands in a new layer on top of the current image and commits the result. |
|Sets the working directory for any |
|Copies new files or directories from |
|Lets you define the default program that is run once you start the container based on this image. Each Dockerfile only has one |
Dockerfiles are crucial inputs for image builds and can facilitate automated, multi-layer image builds based on your unique configurations. Dockerfiles can start simple and grow with your needs to support more complex scenarios.
The default filename to use for a Dockerfile is
Dockerfile, without a file
extension. Using the default name allows you to run the
docker build command
without having to specify additional command flags.
Some projects may need distinct Dockerfiles for specific purposes. A common
convention is to name these
<something>.Dockerfile. You can specify the
Dockerfile filename using the
--file flag for the
docker build command.
Refer to the
docker build CLI reference
to learn about the
We recommend using the default (
Dockerfile) for your project's primary Dockerfile.
Docker images consist of layers. Each layer is the result of a build instruction in the Dockerfile. Layers are stacked sequentially, and each one is a delta representing the changes applied to the previous layer.
Here's what a typical workflow for building applications with Docker looks like.
The following example code shows a small "Hello World" application written in Python, using the Flask framework.
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!"
In order to ship and deploy this application without Docker Build, you would need to make sure that:
- The required runtime dependencies are installed on the server
- The Python code gets uploaded to the server's filesystem
- The server starts your application, using the necessary parameters
The following Dockerfile creates a container image, which has all the dependencies installed and that automatically starts your application.
# syntax=docker/dockerfile:1 FROM ubuntu:22.04 # install app dependencies RUN apt-get update && apt-get install -y python3 python3-pip RUN pip install flask==2.1.* # install app COPY hello.py / # final configuration ENV FLASK_APP=hello EXPOSE 8000 CMD flask run --host 0.0.0.0 --port 8000
Here's a breakdown of what this Dockerfile does:
- Dockerfile syntax
- Base image
- Environment setup
- Installing dependencies
- Copying files
- Setting environment variables
- Exposed ports
- Starting the application
The first line to add to a Dockerfile is a
# syntax parser directive.
While optional, this directive instructs the Docker builder what syntax to use
when parsing the Dockerfile, and allows older Docker versions with BuildKit enabled
to use a specific
Dockerfile frontend before
starting the build.
must appear before any other comment, whitespace, or Dockerfile instruction in
your Dockerfile, and should be the first line in Dockerfiles.
We recommend using
docker/dockerfile:1, which always points to the latest release of the version 1 syntax. BuildKit automatically checks for updates of the syntax before building, making sure you are using the most current version.
The line following the syntax directive defines what base image to use:
FROM instruction sets your base
image to the 22.04 release of Ubuntu. All instructions that follow are executed
in this base image: an Ubuntu environment. The notation
name:tag standard for naming Docker images. When you build images, you
use this notation to name your images. There are many public images you can
leverage in your projects, by importing them into your build steps using the
Docker Hubopen_in_new contains a large set of official images that you can use for this purpose.
The following line executes a build command inside the base image.
# install app dependencies RUN apt-get update && apt-get install -y python3 python3-pip
RUN instruction executes a
shell in Ubuntu that updates the APT package index and installs Python tools in
# install app dependencies line. This is a comment. Comments in
Dockerfiles begin with the
# symbol. As your Dockerfile evolves, comments can
be instrumental to document how your Dockerfile works for any future readers
and editors of the file, including your future self!
You might've noticed that comments are denoted using the same symbol as the syntax directive on the first line of the file. The symbol is only interpreted as a directive if the pattern matches a directive and appears at the beginning of the Dockerfile. Otherwise, it's treated as a comment.
RUN instruction installs the
flask dependency required by the
RUN pip install flask==2.1.*
A prerequisite for this instruction is that
pip is installed into the build
container. The first
RUN command installs
pip, which ensures that we can
use the command to install the flask web framework.
The next instruction uses the
COPY instruction to copy the
hello.py file from the local build context into the root directory of our image.
COPY hello.py /
build context is the set of files that you can access
in Dockerfile instructions such as
COPY instruction, the
hello.py file is added to the filesystem
of the build container.
If your application uses environment variables, you can set environment variables
in your Docker build using the
This sets a Linux environment variable we'll need later. Flask, the framework used in this example, uses this variable to start the application. Without this, flask wouldn't know where to find our application to be able to run it.
EXPOSE instruction marks that
our final image has a service listening on port
This instruction isn't required, but it is a good practice and helps tools and team members understand what this application is doing.
CMD instruction sets the
command that is run when the user starts a container based on this image.
CMD flask run --host 0.0.0.0 --port 8000
In this case we'll start the flask development server listening on all addresses
To build a container image using the Dockerfile example from the
previous section, you use the
docker build command:
$ docker build -t test:latest .
-t test:latest option specifies the name and tag of the image.
The single dot (
.) at the end of the command sets the
build context to the current directory. This means that the
build expects to find the Dockerfile and the
hello.py file in the directory
where the command is invoked. If those files aren't there, the build fails.
After the image has been built, you can run the application as a container with
docker run, specifying the image name:
$ docker run -p 127.0.0.1:8000:8000 test:latest
This publishes the container's port 8000 to
http://localhost:8000 on the
If you are interested in examples in other languages, such as Go, check out our language-specific guides in the Guides section.
For more information about building, including advanced use cases and patterns, refer to the Build with Docker guide.