Docker overview
How to build Docker images containing artifacts built by Pants
Docker images typically bundle build artifacts, such as PEX files, wheels, loose files, and so on, with other runtime requirements, such as a Python interpreter.
Pants makes it easy to embed the artifacts Pants builds into your Docker images, for easy deployment.
Enabling the Docker backend
To use Pants's Docker support you must enable the appropriate backend:
backend_packages = [
...
"pants.backend.docker",
...
]
Adding docker_image
targets
A Docker image is built from a recipe specified by a Dockerfile. When you build Docker images with Pants, instead of running docker
on the Dockerfile directly, you let Pants do that for you.
Pants uses docker_image
targets to indicate which Dockerfiles you want Pants to know about, and to add any necessary metadata.
You can generate initial BUILD files for your Docker images, using tailor:
❯ ./pants tailor
Created src/docker/app1/BUILD:
- Add docker_image target docker
Created src/docker/app2/BUILD:
- Add docker_image target docker
Or you can add them manually, such as:
docker_image(name="docker")
Alternatively you may provide the Docker build instructions inline in your BUILD file as instructions
on your docker_image
if you don't want to create a Dockerfile
.
docker_image(
name="docker",
instructions=[
"FROM python:3.8",
"RUN ..",
]
)
docker_image
instructions
fieldEach docker_image
uses a Dockerfile
referred to by the source
field, unless you have provided a value to the instructions
field.
When using the instructions
field, make sure that the default value for source
does not match a file, or there will be a conflict about which information to use.
Adding dependencies to your docker_image
targets
A Dockerfile executes in a context - a set of files that the commands in the Dockerfile can reference, e.g., by copying them into the image).
When you run docker
directly, the context is usually a directory within your repo. That directory must contain the Dockerfile (typically at the root of the context) and any files that the build requires. If those files are themselves the product of a build step, or if they are sources from elsewhere in the repo, then you have to copy them into the context.
Pants, however, takes care of assembling the context for you. It does so using the dependencies of the docker_image
target.
A docker_image
can depend on loose files belonging to file
/ files
targets, and on artifacts packaged from a variety of targets, such as pex_binary
, python_distribution
, archive
, or any other target that can be built via the package goal.
The context is assembled as follows:
- The sources of
file
/files
targets are assembled at their relative path from the repo root. - The artifacts of any packageable targets are built, as if by running
./pants package
, and placed in the context using the artifact'soutput_path
field.- The
output_path
defaults to the schemepath.to.directory/tgt_name.ext
, e.g.src.python.helloworld/bin.pex
.
- The
Dependency inference for pex_binary
When you COPY
PEX binaries into your image, the dependency on the pex_binary
target will be inferred, so you don't have to add that explicitly to the list of dependencies
on your docker_image
target.
For example, the pex_binary
target src/python/helloworld/bin.pex
has the default output_path
of src.python.helloworld/bin.pex
. So, Pants can infer a dependecy based on the line COPY src.python.helloworld/bin.pex /bin/helloworld
.
Building a Docker image
You build Docker images using the package
goal:
❯ ./pants package path/to/Dockerfile
Build arguments
To provide values to any build ARG
s in the Dockerfile, you can list them in the [docker].build_args
option, which will apply for all images. You can also list any image-specific build args in the field extra_build_args
for the docker_image
target.
The build args use the same syntax as the docker build --build-arg command line option: VARNAME=VALUE
, where the value is optional, and if left out, the value is taken from the environment instead.
- pants.toml
- example/BUILD
- example/Dockerfile
[docker]
build_args = [
"VAR1=value1",
"VAR2"
]
docker_image(
name="docker",
extra_build_args=["VAR1=my_value", "VAR3"]
)
FROM python:3.8
ARG VAR1
ARG VAR2
ARG VAR3=default
...
Target build stage
When your Dockerfile
is a multi stage build file, you may specify which stage to build with the --docker-build-target-stage
for all images, or provide a per image setting with the docker_image
field target_stage
.
FROM python:3.8 AS base
RUN <install required tools>
FROM base AS img
COPY files /